mirror of
https://github.com/nlohmann/json.git
synced 2024-11-27 16:49:02 +08:00
overworked examples
This commit is contained in:
parent
707732a53e
commit
2468631dc9
@ -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).
|
||||
|
||||
|
@ -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 = .
|
||||
|
@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/WSW3gHHE4UcZ9K3G"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/vsDHoEGWuGiwM0oh"><b>online</b></a>
|
@ -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';
|
||||
|
@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/pX9pR42isGha24Up"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/NTSNuQecVsmv3guC"><b>online</b></a>
|
@ -3,5 +3,6 @@ false
|
||||
false
|
||||
false
|
||||
false
|
||||
false
|
||||
true
|
||||
false
|
||||
|
@ -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';
|
||||
|
@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/yBDoHvFIsRqoo7Ca"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/3ZOet3bImwKg2vMb"><b>online</b></a>
|
@ -5,3 +5,4 @@ false
|
||||
false
|
||||
false
|
||||
false
|
||||
false
|
||||
|
@ -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';
|
||||
|
@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/Fz9sE7wOVCDCxuqw"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/a0c6rKo8VbeOBAmD"><b>online</b></a>
|
@ -5,3 +5,4 @@ false
|
||||
false
|
||||
false
|
||||
false
|
||||
false
|
||||
|
@ -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';
|
||||
|
@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/CwckRKEAALOpqiq7"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/z9pBPy4eSYMCZgEr"><b>online</b></a>
|
@ -5,3 +5,4 @@ false
|
||||
false
|
||||
false
|
||||
false
|
||||
false
|
||||
|
@ -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';
|
||||
|
@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/xVKMzHSvo4XHFyxV"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/1rel3xdea4ADiKds"><b>online</b></a>
|
@ -2,6 +2,7 @@ false
|
||||
false
|
||||
true
|
||||
true
|
||||
true
|
||||
false
|
||||
false
|
||||
false
|
||||
|
@ -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';
|
||||
|
@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/SbPvCBbOwWkIH5RX"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/vEZZNyL8GPbAfAsJ"><b>online</b></a>
|
@ -1,6 +1,7 @@
|
||||
false
|
||||
false
|
||||
false
|
||||
false
|
||||
true
|
||||
false
|
||||
false
|
||||
|
@ -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';
|
||||
|
@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/CVMYqyeIFOKcBZZU"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/bulo75cZB0cPINgL"><b>online</b></a>
|
@ -1,6 +1,7 @@
|
||||
false
|
||||
false
|
||||
true
|
||||
true
|
||||
false
|
||||
false
|
||||
false
|
||||
|
27
doc/examples/is_number_unsigned.cpp
Normal file
27
doc/examples/is_number_unsigned.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include <json.hpp>
|
||||
|
||||
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';
|
||||
}
|
1
doc/examples/is_number_unsigned.link
Normal file
1
doc/examples/is_number_unsigned.link
Normal file
@ -0,0 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/h4s4VitQ3VmCOEXr"><b>online</b></a>
|
8
doc/examples/is_number_unsigned.output
Normal file
8
doc/examples/is_number_unsigned.output
Normal file
@ -0,0 +1,8 @@
|
||||
false
|
||||
false
|
||||
false
|
||||
true
|
||||
false
|
||||
false
|
||||
false
|
||||
false
|
@ -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';
|
||||
|
@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/uXT6peoqwN6DIFAr"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/40lrBaplftujclBr"><b>online</b></a>
|
@ -2,6 +2,7 @@ false
|
||||
false
|
||||
false
|
||||
false
|
||||
false
|
||||
true
|
||||
false
|
||||
false
|
||||
|
@ -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';
|
||||
|
@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/f5AMxY70IoQ4o0wb"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/zZYJHMvzYyYkNoAU"><b>online</b></a>
|
@ -2,6 +2,7 @@ true
|
||||
true
|
||||
true
|
||||
true
|
||||
true
|
||||
false
|
||||
false
|
||||
true
|
||||
|
@ -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';
|
||||
|
@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/ILh5L9NsOSQ29Cg4"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/1x7B5XrHtV8RTROC"><b>online</b></a>
|
@ -4,4 +4,5 @@ false
|
||||
false
|
||||
false
|
||||
false
|
||||
false
|
||||
true
|
||||
|
@ -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';
|
||||
|
@ -1 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/D9WgQUo2eieU7kqs"><b>online</b></a>
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/kXpxIk6MP6OxEvwu"><b>online</b></a>
|
@ -2,6 +2,7 @@ false
|
||||
false
|
||||
false
|
||||
false
|
||||
false
|
||||
true
|
||||
true
|
||||
false
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user