From 979cb4ca767718d69ed593bbf836dc40c883636e Mon Sep 17 00:00:00 2001 From: Jacob Potter Date: Tue, 1 Oct 2013 19:04:32 -0700 Subject: [PATCH] Add LICENSE.txt; some minor cleanups - Add LICENSE.txt - Add a comment about how we handle numbers - Add more test code - Fix a narrowing-conversion issue - Fix a comment typo --- LICENSE.txt | 19 +++++++++++++++++++ json11.cpp | 2 +- json11.hpp | 15 ++++++++++++++- test.cpp | 13 +++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 LICENSE.txt diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..691742e --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,19 @@ +Copyright (c) 2013 Dropbox, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/json11.cpp b/json11.cpp index dc88207..9666db8 100644 --- a/json11.cpp +++ b/json11.cpp @@ -300,7 +300,7 @@ static inline string esc(char c) { return string(buf); } -static inline bool in_range (int x, int lower, int upper) { +static inline bool in_range(long x, long lower, long upper) { return (x >= lower && x <= upper); } diff --git a/json11.hpp b/json11.hpp index a2fa5c7..af65cdb 100644 --- a/json11.hpp +++ b/json11.hpp @@ -12,6 +12,19 @@ * * Internally, the various types of Json object are represented by the JsonValue class * hierarchy. + * + * A note on numbers - JSON specifies the syntax of number formatting but not its semantics, + * so some JSON implementations distinguish between integers and floating-point numbers, while + * some don't. In json11, we choose the latter. Because some JSON implementations (namely + * Javascript itself) treat all numbers as the same type, distinguishing the two leads + * to JSON that will be *silently* changed by a round-trip through those implementations. + * Dangerous! To avoid that risk, json11 stores all numbers as double internally, but also + * provides integer helpers. + * + * Fortunately, double-precision IEEE754 ('double') can precisely store any integer in the + * range +/-2^53, which includes every 'int' on most systems. (Timestamps often use int64 + * or long long to avoid the Y2038K problem; a double storing microseconds since some epoch + * will be exact for +/- 275 years.) */ /* Copyright (c) 2013 Dropbox, Inc. @@ -118,7 +131,7 @@ public: // Return the enclosed std::map if this is an object, or an empty map otherwise. const object &object_items() const; - // Return a reference to arr[i] if this is an object, Json() otherwise. + // Return a reference to arr[i] if this is an array, Json() otherwise. const Json & operator[](size_t i) const; // Return a reference to obj[key] if this is an object, Json() otherwise. const Json & operator[](const std::string &key) const; diff --git a/test.cpp b/test.cpp index f34c4a0..3bfe2e0 100644 --- a/test.cpp +++ b/test.cpp @@ -4,6 +4,9 @@ #include #include "json11.hpp" #include +#include +#include +#include using namespace json11; using std::string; @@ -51,6 +54,16 @@ int main(int argc, char **argv) { std::cout << " - " << k.dump() << "\n"; } + std::list l1 { 1, 2, 3 }; + std::vector l2 { 1, 2, 3 }; + std::set l3 { 1, 2, 3 }; + assert(Json(l1) == Json(l2)); + assert(Json(l2) == Json(l3)); + + std::map m1 { { "k1", "v1" }, { "k2", "v2" } }; + std::unordered_map m2 { { "k1", "v1" }, { "k2", "v2" } }; + assert(Json(m1) == Json(m2)); + // Json literals Json obj = Json::object({ { "k1", "v1" },