minor changes

This commit is contained in:
Niels 2014-12-29 18:32:17 +01:00
parent f50fdd08a7
commit 0b8dda1e0c
2 changed files with 11 additions and 7 deletions

View File

@ -176,6 +176,8 @@ int vi = jn.get<int>();
## License
<img style="float: right" src="http://opensource.org/trademarks/opensource/OSI-Approved-License-100x137.png">
The library is licensed under the [MIT License](http://opensource.org/licenses/MIT):
Copyright (c) 2013-2014 Niels Lohmann

View File

@ -11,25 +11,27 @@
| array | `value_type::array ` | `std::array<JSON>` | `JSON::array_t` | `{}` |
| object | `value_type::object` | `std::map<std::string, JSON>` | `JSON::object_t` | `{}` |
## Conversions
## Type conversions
There are only a few type conversions possible:
- An integer number can be translated to a floating point number (e.g., by calling `get<double>()`).
- A floating pointnnumber can be translated to an integer number (e.g., by calling `get<int>()`). Note the number is truncated and not rounded, ceiled or floored.
- Any value but JSON objects can be translated into an array. The result is a singleton array that consists of the value before.
- An integer number can be translated to a floating point number.
- A floating point number can be translated to an integer number. Note the number is truncated and not rounded, ceiled or floored.
- Any value (i.e., boolean, string, number, null) but JSON objects can be translated into an array. The result is a singleton array that consists of the value before.
- Any other conversion will throw a `std::logic_error` exception.
When compatible, `JSON` values **implicitly convert** to `std::string`, `int`, `double`, `JSON::array_t`, and `JSON::object_t`. Furthermore, **explicit type conversion** is possible using the `get<>()` function with the aforementioned types.
## Initialization
JSON values can be created from many literals and variable types:
| JSON type | literal/variable types | examples |
| --------- | ---------------------- | -------- |
| none | null pointer literal, `nullptr_t` type, no value | ```cpp nullptr``` |
| none | null pointer literal, `nullptr_t` type, no value | `nullptr` |
| boolean | boolean literals, `bool` type, `JSON::boolean_t` type | `true`, `false` |
| string | string literal, `char*` type, `std::string` type, `std::string&&` rvalue reference, `JSON::string_t` type | `"Hello"` |
| number (integer) | integer literal, `short int` type, `int` type, `JSON_number_t` type | `42` |
| number (floating point) | floating point literal, `float` type, `double` type, `JSON::number_float_t` type | `3.141529`
| array | initializer list whose elements are `JSON` values (or can be translated into `JSON` values using the rules above), `std::vector<JSON>` type, `JSON::array_t` type | `{1, 2, 3, true, "foo"}` |
| object | initializer list whose elements are pairs of a string literal and a `JSON` value (or can be translated into `JSON` values using the rules above), `std::map<std::string, JSON>` type, `JSON::object_t` type | `{ {"key1", 42}, {"key2", false} }` |
| array | initializer list whose elements are `JSON` values (or can be translated into `JSON` values using the rules above), `std::vector<JSON>` type, `JSON::array_t` type, `JSON::array_t&&` rvalue reference | `{1, 2, 3, true, "foo"}` |
| object | initializer list whose elements are pairs of a string literal and a `JSON` value (or can be translated into `JSON` values using the rules above), `std::map<std::string, JSON>` type, `JSON::object_t` type, `JSON::object_t&&` rvalue reference | `{ {"key1", 42}, {"key2", false} }` |