Skip to content

Commit

Permalink
Add LICENSE.txt; some minor cleanups
Browse files Browse the repository at this point in the history
- Add LICENSE.txt
- Add a comment about how we handle numbers
- Add more test code
- Fix a narrowing-conversion issue
- Fix a comment typo
  • Loading branch information
Jacob Potter committed Oct 2, 2013
1 parent fe5e216 commit 979cb4c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
19 changes: 19 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion json11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
15 changes: 14 additions & 1 deletion json11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -118,7 +131,7 @@ class Json final {
// 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;
Expand Down
13 changes: 13 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <sstream>
#include "json11.hpp"
#include <cassert>
#include <list>
#include <set>
#include <unordered_map>

using namespace json11;
using std::string;
Expand Down Expand Up @@ -51,6 +54,16 @@ int main(int argc, char **argv) {
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 obj = Json::object({
{ "k1", "v1" },
Expand Down

0 comments on commit 979cb4c

Please sign in to comment.