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 typomutable-v2
parent
fe5e2167a5
commit
979cb4ca76
|
@ -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.
|
|
@ -300,7 +300,7 @@ static inline string esc(char c) {
|
||||||
return string(buf);
|
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);
|
return (x >= lower && x <= upper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
15
json11.hpp
15
json11.hpp
|
@ -12,6 +12,19 @@
|
||||||
*
|
*
|
||||||
* Internally, the various types of Json object are represented by the JsonValue class
|
* Internally, the various types of Json object are represented by the JsonValue class
|
||||||
* hierarchy.
|
* 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.
|
/* 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.
|
// Return the enclosed std::map if this is an object, or an empty map otherwise.
|
||||||
const object &object_items() const;
|
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;
|
const Json & operator[](size_t i) const;
|
||||||
// Return a reference to obj[key] if this is an object, Json() otherwise.
|
// Return a reference to obj[key] if this is an object, Json() otherwise.
|
||||||
const Json & operator[](const std::string &key) const;
|
const Json & operator[](const std::string &key) const;
|
||||||
|
|
13
test.cpp
13
test.cpp
|
@ -4,6 +4,9 @@
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include "json11.hpp"
|
#include "json11.hpp"
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <list>
|
||||||
|
#include <set>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
using namespace json11;
|
using namespace json11;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
@ -51,6 +54,16 @@ int main(int argc, char **argv) {
|
||||||
std::cout << " - " << k.dump() << "\n";
|
std::cout << " - " << k.dump() << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::list<int> l1 { 1, 2, 3 };
|
||||||
|
std::vector<int> l2 { 1, 2, 3 };
|
||||||
|
std::set<int> l3 { 1, 2, 3 };
|
||||||
|
assert(Json(l1) == Json(l2));
|
||||||
|
assert(Json(l2) == Json(l3));
|
||||||
|
|
||||||
|
std::map<string, string> m1 { { "k1", "v1" }, { "k2", "v2" } };
|
||||||
|
std::unordered_map<string, string> m2 { { "k1", "v1" }, { "k2", "v2" } };
|
||||||
|
assert(Json(m1) == Json(m2));
|
||||||
|
|
||||||
// Json literals
|
// Json literals
|
||||||
Json obj = Json::object({
|
Json obj = Json::object({
|
||||||
{ "k1", "v1" },
|
{ "k1", "v1" },
|
||||||
|
|
Loading…
Reference in New Issue