From 2468631dc9407ce0be46e96e720d628e1b38b967 Mon Sep 17 00:00:00 2001 From: Niels Date: Tue, 26 Jan 2016 20:07:03 +0100 Subject: [PATCH] overworked examples --- README.md | 2 +- doc/Doxyfile | 2 +- doc/examples/README.link | 2 +- doc/examples/is_array.cpp | 2 ++ doc/examples/is_array.link | 2 +- doc/examples/is_array.output | 1 + doc/examples/is_boolean.cpp | 2 ++ doc/examples/is_boolean.link | 2 +- doc/examples/is_boolean.output | 1 + doc/examples/is_discarded.cpp | 2 ++ doc/examples/is_discarded.link | 2 +- doc/examples/is_discarded.output | 1 + doc/examples/is_null.cpp | 2 ++ doc/examples/is_null.link | 2 +- doc/examples/is_null.output | 1 + doc/examples/is_number.cpp | 2 ++ doc/examples/is_number.link | 2 +- doc/examples/is_number.output | 1 + doc/examples/is_number_float.cpp | 2 ++ doc/examples/is_number_float.link | 2 +- doc/examples/is_number_float.output | 1 + doc/examples/is_number_integer.cpp | 2 ++ doc/examples/is_number_integer.link | 2 +- doc/examples/is_number_integer.output | 1 + doc/examples/is_number_unsigned.cpp | 27 ++++++++++++++++++++++++++ doc/examples/is_number_unsigned.link | 1 + doc/examples/is_number_unsigned.output | 8 ++++++++ doc/examples/is_object.cpp | 2 ++ doc/examples/is_object.link | 2 +- doc/examples/is_object.output | 1 + doc/examples/is_primitive.cpp | 2 ++ doc/examples/is_primitive.link | 2 +- doc/examples/is_primitive.output | 1 + doc/examples/is_string.cpp | 2 ++ doc/examples/is_string.link | 2 +- doc/examples/is_string.output | 1 + doc/examples/is_structured.cpp | 2 ++ doc/examples/is_structured.link | 2 +- doc/examples/is_structured.output | 1 + src/json.hpp | 5 ++++- src/json.hpp.re2c | 5 ++++- 41 files changed, 91 insertions(+), 16 deletions(-) create mode 100644 doc/examples/is_number_unsigned.cpp create mode 100644 doc/examples/is_number_unsigned.link create mode 100644 doc/examples/is_number_unsigned.output diff --git a/README.md b/README.md index 8d180c28..cc6ff3be 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ There are myriads of [JSON](http://json.org) libraries out there, and each may e Other aspects were not so important to us: -- **Memory efficiency**. Each JSON object has an overhead of one pointer (the maximal size of a union) and one enumeration element (1 byte). The default generalization uses the following C++ data types: `std::string` for strings, `int64_t` or `double` for numbers, `std::map` for objects, `std::vector` for arrays, and `bool` for Booleans. However, you can template the generalized class `basic_json` to your needs. +- **Memory efficiency**. Each JSON object has an overhead of one pointer (the maximal size of a union) and one enumeration element (1 byte). The default generalization uses the following C++ data types: `std::string` for strings, `int64_t`, `uint64_t` or `double` for numbers, `std::map` for objects, `std::vector` for arrays, and `bool` for Booleans. However, you can template the generalized class `basic_json` to your needs. - **Speed**. We currently implement the parser as naive [recursive descent parser](http://en.wikipedia.org/wiki/Recursive_descent_parser) with hand coded string handling. It is fast enough, but a [LALR-parser](http://en.wikipedia.org/wiki/LALR_parser) with a decent regular expression processor should be even faster (but would consist of more files which makes the integration harder). diff --git a/doc/Doxyfile b/doc/Doxyfile index a97cb5ad..f1b3b367 100644 --- a/doc/Doxyfile +++ b/doc/Doxyfile @@ -5,7 +5,7 @@ #--------------------------------------------------------------------------- DOXYFILE_ENCODING = UTF-8 PROJECT_NAME = "JSON for Modern C++" -PROJECT_NUMBER = 1.1.0 +PROJECT_NUMBER = 2.0.0 PROJECT_BRIEF = PROJECT_LOGO = OUTPUT_DIRECTORY = . diff --git a/doc/examples/README.link b/doc/examples/README.link index 96f5ad8a..dab9343f 100644 --- a/doc/examples/README.link +++ b/doc/examples/README.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/is_array.cpp b/doc/examples/is_array.cpp index a5a544f0..c436f38f 100644 --- a/doc/examples/is_array.cpp +++ b/doc/examples/is_array.cpp @@ -8,6 +8,7 @@ int main() json j_null; json j_boolean = true; json j_number_integer = 17; + json j_number_unsigned_integer = 12345678987654321u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; @@ -18,6 +19,7 @@ int main() std::cout << j_null.is_array() << '\n'; std::cout << j_boolean.is_array() << '\n'; std::cout << j_number_integer.is_array() << '\n'; + std::cout << j_number_unsigned_integer.is_array() << '\n'; std::cout << j_number_float.is_array() << '\n'; std::cout << j_object.is_array() << '\n'; std::cout << j_array.is_array() << '\n'; diff --git a/doc/examples/is_array.link b/doc/examples/is_array.link index e1aa234e..4d5fdb07 100644 --- a/doc/examples/is_array.link +++ b/doc/examples/is_array.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/is_array.output b/doc/examples/is_array.output index 65ffc69e..5b8cc40c 100644 --- a/doc/examples/is_array.output +++ b/doc/examples/is_array.output @@ -3,5 +3,6 @@ false false false false +false true false diff --git a/doc/examples/is_boolean.cpp b/doc/examples/is_boolean.cpp index 0143b0bf..24511a2f 100644 --- a/doc/examples/is_boolean.cpp +++ b/doc/examples/is_boolean.cpp @@ -8,6 +8,7 @@ int main() json j_null; json j_boolean = true; json j_number_integer = 17; + json j_number_unsigned_integer = 12345678987654321u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; @@ -18,6 +19,7 @@ int main() std::cout << j_null.is_boolean() << '\n'; std::cout << j_boolean.is_boolean() << '\n'; std::cout << j_number_integer.is_boolean() << '\n'; + std::cout << j_number_unsigned_integer.is_boolean() << '\n'; std::cout << j_number_float.is_boolean() << '\n'; std::cout << j_object.is_boolean() << '\n'; std::cout << j_array.is_boolean() << '\n'; diff --git a/doc/examples/is_boolean.link b/doc/examples/is_boolean.link index 3972af75..5fda3d0a 100644 --- a/doc/examples/is_boolean.link +++ b/doc/examples/is_boolean.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/is_boolean.output b/doc/examples/is_boolean.output index 54f16b97..721b3a5e 100644 --- a/doc/examples/is_boolean.output +++ b/doc/examples/is_boolean.output @@ -5,3 +5,4 @@ false false false false +false diff --git a/doc/examples/is_discarded.cpp b/doc/examples/is_discarded.cpp index c71bf901..7f6c8c07 100644 --- a/doc/examples/is_discarded.cpp +++ b/doc/examples/is_discarded.cpp @@ -8,6 +8,7 @@ int main() json j_null; json j_boolean = true; json j_number_integer = 17; + json j_number_unsigned_integer = 12345678987654321u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; @@ -18,6 +19,7 @@ int main() std::cout << j_null.is_discarded() << '\n'; std::cout << j_boolean.is_discarded() << '\n'; std::cout << j_number_integer.is_discarded() << '\n'; + std::cout << j_number_unsigned_integer.is_discarded() << '\n'; std::cout << j_number_float.is_discarded() << '\n'; std::cout << j_object.is_discarded() << '\n'; std::cout << j_array.is_discarded() << '\n'; diff --git a/doc/examples/is_discarded.link b/doc/examples/is_discarded.link index 389f90bb..5f58643d 100644 --- a/doc/examples/is_discarded.link +++ b/doc/examples/is_discarded.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/is_discarded.output b/doc/examples/is_discarded.output index 28f6ac44..485b1696 100644 --- a/doc/examples/is_discarded.output +++ b/doc/examples/is_discarded.output @@ -5,3 +5,4 @@ false false false false +false diff --git a/doc/examples/is_null.cpp b/doc/examples/is_null.cpp index d107f730..d6a6de4c 100644 --- a/doc/examples/is_null.cpp +++ b/doc/examples/is_null.cpp @@ -8,6 +8,7 @@ int main() json j_null; json j_boolean = true; json j_number_integer = 17; + json j_number_unsigned_integer = 12345678987654321u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; @@ -18,6 +19,7 @@ int main() std::cout << j_null.is_null() << '\n'; std::cout << j_boolean.is_null() << '\n'; std::cout << j_number_integer.is_null() << '\n'; + std::cout << j_number_unsigned_integer.is_null() << '\n'; std::cout << j_number_float.is_null() << '\n'; std::cout << j_object.is_null() << '\n'; std::cout << j_array.is_null() << '\n'; diff --git a/doc/examples/is_null.link b/doc/examples/is_null.link index a2fb1407..39c5d261 100644 --- a/doc/examples/is_null.link +++ b/doc/examples/is_null.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/is_null.output b/doc/examples/is_null.output index 828ea891..4cc64628 100644 --- a/doc/examples/is_null.output +++ b/doc/examples/is_null.output @@ -5,3 +5,4 @@ false false false false +false diff --git a/doc/examples/is_number.cpp b/doc/examples/is_number.cpp index 992ec183..29c21ffb 100644 --- a/doc/examples/is_number.cpp +++ b/doc/examples/is_number.cpp @@ -8,6 +8,7 @@ int main() json j_null; json j_boolean = true; json j_number_integer = 17; + json j_number_unsigned_integer = 12345678987654321u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; @@ -18,6 +19,7 @@ int main() std::cout << j_null.is_number() << '\n'; std::cout << j_boolean.is_number() << '\n'; std::cout << j_number_integer.is_number() << '\n'; + std::cout << j_number_unsigned_integer.is_number() << '\n'; std::cout << j_number_float.is_number() << '\n'; std::cout << j_object.is_number() << '\n'; std::cout << j_array.is_number() << '\n'; diff --git a/doc/examples/is_number.link b/doc/examples/is_number.link index 1fc0fc86..090b5ca9 100644 --- a/doc/examples/is_number.link +++ b/doc/examples/is_number.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/is_number.output b/doc/examples/is_number.output index b031a141..06dbc282 100644 --- a/doc/examples/is_number.output +++ b/doc/examples/is_number.output @@ -2,6 +2,7 @@ false false true true +true false false false diff --git a/doc/examples/is_number_float.cpp b/doc/examples/is_number_float.cpp index 3ede378f..289428c3 100644 --- a/doc/examples/is_number_float.cpp +++ b/doc/examples/is_number_float.cpp @@ -8,6 +8,7 @@ int main() json j_null; json j_boolean = true; json j_number_integer = 17; + json j_number_unsigned_integer = 12345678987654321u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; @@ -18,6 +19,7 @@ int main() std::cout << j_null.is_number_float() << '\n'; std::cout << j_boolean.is_number_float() << '\n'; std::cout << j_number_integer.is_number_float() << '\n'; + std::cout << j_number_unsigned_integer.is_number_float() << '\n'; std::cout << j_number_float.is_number_float() << '\n'; std::cout << j_object.is_number_float() << '\n'; std::cout << j_array.is_number_float() << '\n'; diff --git a/doc/examples/is_number_float.link b/doc/examples/is_number_float.link index ef3aaf33..7ed76760 100644 --- a/doc/examples/is_number_float.link +++ b/doc/examples/is_number_float.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/is_number_float.output b/doc/examples/is_number_float.output index c2470282..09afae4c 100644 --- a/doc/examples/is_number_float.output +++ b/doc/examples/is_number_float.output @@ -1,6 +1,7 @@ false false false +false true false false diff --git a/doc/examples/is_number_integer.cpp b/doc/examples/is_number_integer.cpp index 281bf432..d7a38b6a 100644 --- a/doc/examples/is_number_integer.cpp +++ b/doc/examples/is_number_integer.cpp @@ -8,6 +8,7 @@ int main() json j_null; json j_boolean = true; json j_number_integer = 17; + json j_number_unsigned_integer = 12345678987654321u; json j_number_float = 23.42; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; @@ -18,6 +19,7 @@ int main() std::cout << j_null.is_number_integer() << '\n'; std::cout << j_boolean.is_number_integer() << '\n'; std::cout << j_number_integer.is_number_integer() << '\n'; + std::cout << j_number_unsigned_integer.is_number_integer() << '\n'; std::cout << j_number_float.is_number_integer() << '\n'; std::cout << j_object.is_number_integer() << '\n'; std::cout << j_array.is_number_integer() << '\n'; diff --git a/doc/examples/is_number_integer.link b/doc/examples/is_number_integer.link index bbd368f0..fae1ef43 100644 --- a/doc/examples/is_number_integer.link +++ b/doc/examples/is_number_integer.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/is_number_integer.output b/doc/examples/is_number_integer.output index 26051afd..be0f7393 100644 --- a/doc/examples/is_number_integer.output +++ b/doc/examples/is_number_integer.output @@ -1,6 +1,7 @@ false false true +true false false false diff --git a/doc/examples/is_number_unsigned.cpp b/doc/examples/is_number_unsigned.cpp new file mode 100644 index 00000000..185d93de --- /dev/null +++ b/doc/examples/is_number_unsigned.cpp @@ -0,0 +1,27 @@ +#include + +using namespace nlohmann; + +int main() +{ + // create JSON values + json j_null; + json j_boolean = true; + json j_number_integer = 17; + json j_number_unsigned_integer = 12345678987654321u; + json j_number_float = 23.42; + json j_object = {{"one", 1}, {"two", 2}}; + json j_array = {1, 2, 4, 8, 16}; + json j_string = "Hello, world"; + + // call is_number_unsigned() + std::cout << std::boolalpha; + std::cout << j_null.is_number_unsigned() << '\n'; + std::cout << j_boolean.is_number_unsigned() << '\n'; + std::cout << j_number_integer.is_number_unsigned() << '\n'; + std::cout << j_number_unsigned_integer.is_number_unsigned() << '\n'; + std::cout << j_number_float.is_number_unsigned() << '\n'; + std::cout << j_object.is_number_unsigned() << '\n'; + std::cout << j_array.is_number_unsigned() << '\n'; + std::cout << j_string.is_number_unsigned() << '\n'; +} diff --git a/doc/examples/is_number_unsigned.link b/doc/examples/is_number_unsigned.link new file mode 100644 index 00000000..1877ced9 --- /dev/null +++ b/doc/examples/is_number_unsigned.link @@ -0,0 +1 @@ +online \ No newline at end of file diff --git a/doc/examples/is_number_unsigned.output b/doc/examples/is_number_unsigned.output new file mode 100644 index 00000000..fdf264e0 --- /dev/null +++ b/doc/examples/is_number_unsigned.output @@ -0,0 +1,8 @@ +false +false +false +true +false +false +false +false diff --git a/doc/examples/is_object.cpp b/doc/examples/is_object.cpp index 268c0224..b85253a3 100644 --- a/doc/examples/is_object.cpp +++ b/doc/examples/is_object.cpp @@ -9,6 +9,7 @@ int main() json j_boolean = true; json j_number_integer = 17; json j_number_float = 23.42; + json j_number_unsigned_integer = 12345678987654321u; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; @@ -18,6 +19,7 @@ int main() std::cout << j_null.is_object() << '\n'; std::cout << j_boolean.is_object() << '\n'; std::cout << j_number_integer.is_object() << '\n'; + std::cout << j_number_unsigned_integer.is_object() << '\n'; std::cout << j_number_float.is_object() << '\n'; std::cout << j_object.is_object() << '\n'; std::cout << j_array.is_object() << '\n'; diff --git a/doc/examples/is_object.link b/doc/examples/is_object.link index 5ab6384f..549bc0c0 100644 --- a/doc/examples/is_object.link +++ b/doc/examples/is_object.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/is_object.output b/doc/examples/is_object.output index 8416200c..e041e392 100644 --- a/doc/examples/is_object.output +++ b/doc/examples/is_object.output @@ -2,6 +2,7 @@ false false false false +false true false false diff --git a/doc/examples/is_primitive.cpp b/doc/examples/is_primitive.cpp index ed5d8b0b..3f45b95a 100644 --- a/doc/examples/is_primitive.cpp +++ b/doc/examples/is_primitive.cpp @@ -9,6 +9,7 @@ int main() json j_boolean = true; json j_number_integer = 17; json j_number_float = 23.42; + json j_number_unsigned_integer = 12345678987654321u; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; @@ -18,6 +19,7 @@ int main() std::cout << j_null.is_primitive() << '\n'; std::cout << j_boolean.is_primitive() << '\n'; std::cout << j_number_integer.is_primitive() << '\n'; + std::cout << j_number_unsigned_integer.is_primitive() << '\n'; std::cout << j_number_float.is_primitive() << '\n'; std::cout << j_object.is_primitive() << '\n'; std::cout << j_array.is_primitive() << '\n'; diff --git a/doc/examples/is_primitive.link b/doc/examples/is_primitive.link index f6918a96..27167a3a 100644 --- a/doc/examples/is_primitive.link +++ b/doc/examples/is_primitive.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/is_primitive.output b/doc/examples/is_primitive.output index 2c1bd252..635db6e3 100644 --- a/doc/examples/is_primitive.output +++ b/doc/examples/is_primitive.output @@ -2,6 +2,7 @@ true true true true +true false false true diff --git a/doc/examples/is_string.cpp b/doc/examples/is_string.cpp index 6bcbcec5..6020fb66 100644 --- a/doc/examples/is_string.cpp +++ b/doc/examples/is_string.cpp @@ -9,6 +9,7 @@ int main() json j_boolean = true; json j_number_integer = 17; json j_number_float = 23.42; + json j_number_unsigned_integer = 12345678987654321u; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; @@ -18,6 +19,7 @@ int main() std::cout << j_null.is_string() << '\n'; std::cout << j_boolean.is_string() << '\n'; std::cout << j_number_integer.is_string() << '\n'; + std::cout << j_number_unsigned_integer.is_string() << '\n'; std::cout << j_number_float.is_string() << '\n'; std::cout << j_object.is_string() << '\n'; std::cout << j_array.is_string() << '\n'; diff --git a/doc/examples/is_string.link b/doc/examples/is_string.link index 7ba95c36..1e8d54fa 100644 --- a/doc/examples/is_string.link +++ b/doc/examples/is_string.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/is_string.output b/doc/examples/is_string.output index 2cead02b..672eb438 100644 --- a/doc/examples/is_string.output +++ b/doc/examples/is_string.output @@ -4,4 +4,5 @@ false false false false +false true diff --git a/doc/examples/is_structured.cpp b/doc/examples/is_structured.cpp index 13c183e8..40a7cb33 100644 --- a/doc/examples/is_structured.cpp +++ b/doc/examples/is_structured.cpp @@ -9,6 +9,7 @@ int main() json j_boolean = true; json j_number_integer = 17; json j_number_float = 23.42; + json j_number_unsigned_integer = 12345678987654321u; json j_object = {{"one", 1}, {"two", 2}}; json j_array = {1, 2, 4, 8, 16}; json j_string = "Hello, world"; @@ -18,6 +19,7 @@ int main() std::cout << j_null.is_structured() << '\n'; std::cout << j_boolean.is_structured() << '\n'; std::cout << j_number_integer.is_structured() << '\n'; + std::cout << j_number_unsigned_integer.is_structured() << '\n'; std::cout << j_number_float.is_structured() << '\n'; std::cout << j_object.is_structured() << '\n'; std::cout << j_array.is_structured() << '\n'; diff --git a/doc/examples/is_structured.link b/doc/examples/is_structured.link index 9b5abfdf..6cc3c9bb 100644 --- a/doc/examples/is_structured.link +++ b/doc/examples/is_structured.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/is_structured.output b/doc/examples/is_structured.output index d251407d..e1186dd8 100644 --- a/doc/examples/is_structured.output +++ b/doc/examples/is_structured.output @@ -2,6 +2,7 @@ false false false false +false true true false diff --git a/src/json.hpp b/src/json.hpp index 8b8ed337..0a2c6fde 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -32,7 +32,7 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation. @author [Niels Lohmann](http://nlohmann.me) @see https://github.com/nlohmann/json to download the source code -@version 1.1.0 +@version 2.0.0 */ #ifndef NLOHMANN_JSON_HPP @@ -2232,6 +2232,9 @@ class basic_json @complexity Constant. + @liveexample{The following code exemplifies @ref is_number_unsigned for all + JSON types.,is_number_unsigned} + @sa @ref is_number() -- check if value is a number @sa @ref is_number_integer() -- check if value is an integer or unsigned integer number diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 52bdc161..c80a2bea 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -32,7 +32,7 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation. @author [Niels Lohmann](http://nlohmann.me) @see https://github.com/nlohmann/json to download the source code -@version 1.1.0 +@version 2.0.0 */ #ifndef NLOHMANN_JSON_HPP @@ -2232,6 +2232,9 @@ class basic_json @complexity Constant. + @liveexample{The following code exemplifies @ref is_number_unsigned for all + JSON types.,is_number_unsigned} + @sa @ref is_number() -- check if value is a number @sa @ref is_number_integer() -- check if value is an integer or unsigned integer number