Skip to content

Commit

Permalink
improved documentation for #289
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Aug 11, 2016
1 parent 37fdcd8 commit 03de590
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ Thanks a lot for helping out!

## Notes

- The code contains numerous debug **assertions** which can be switched off by defining the preprocessor macro `NDEBUG`, see the [documentation of `assert`](http://en.cppreference.com/w/cpp/error/assert).
- The code contains numerous debug **assertions** which can be switched off by defining the preprocessor macro `NDEBUG`, see the [documentation of `assert`](http://en.cppreference.com/w/cpp/error/assert). In particular, note [`operator[]`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a2e26bd0b0168abb61f67ad5bcd5b9fa1.html#a2e26bd0b0168abb61f67ad5bcd5b9fa1) implements **unchecked access** for const objects: If the given key is not present, the behavior is undefined (think of a dereferenced null pointer) and yields an [assertion failure](https://github.com/nlohmann/json/issues/289) if assertions are switched on. If you are not sure whether an element in an object exists, use checked access with the [`at()` function](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a674de1ee73e6bf4843fc5dc1351fb726.html#a674de1ee73e6bf4843fc5dc1351fb726).
- As the exact type of a number is not defined in the [JSON specification](http://rfc7159.net/rfc7159), this library tries to choose the best fitting C++ number type automatically. As a result, the type `double` may be used to store numbers which may yield [**floating-point exceptions**](https://github.com/nlohmann/json/issues/181) in certain rare situations if floating-point exceptions have been unmasked in the calling code. These exceptions are not caused by the library and need to be fixed in the calling code, such as by re-masking the exceptions prior to calling library functions.


Expand Down
19 changes: 14 additions & 5 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,8 @@ class basic_json
@param[in] first begin of the range to copy from (included)
@param[in] last end of the range to copy from (excluded)
@pre Iterators @a first and @a last must be initialized.
@pre Iterators @a first and @a last must be initialized. **This
precondition is enforced with an assertion.**
@throw std::domain_error if iterators are not compatible; that is, do not
belong to the same JSON value; example: `"iterators are not compatible"`
Expand Down Expand Up @@ -3509,6 +3510,9 @@ class basic_json
@return const reference to the element at key @a key
@pre The element with key @a key must exist. **This precondition is
enforced with an assertion.**
@throw std::domain_error if JSON is not an object; example: `"cannot use
operator[] with null"`
Expand Down Expand Up @@ -3667,6 +3671,9 @@ class basic_json
@return const reference to the element at key @a key
@pre The element with key @a key must exist. **This precondition is
enforced with an assertion.**
@throw std::domain_error if JSON is not an object; example: `"cannot use
operator[] with null"`
Expand Down Expand Up @@ -3867,7 +3874,8 @@ class basic_json
@complexity Constant.
@pre The JSON value must not be `null` (would throw `std::out_of_range`)
or an empty array or object (undefined behavior, guarded by assertions).
or an empty array or object (undefined behavior, **guarded by
assertions**).
@post The JSON value remains unchanged.
@throw std::out_of_range when called on `null` value
Expand Down Expand Up @@ -3909,7 +3917,8 @@ class basic_json
@complexity Constant.
@pre The JSON value must not be `null` (would throw `std::out_of_range`)
or an empty array or object (undefined behavior, guarded by assertions).
or an empty array or object (undefined behavior, **guarded by
assertions**).
@post The JSON value remains unchanged.
@throw std::out_of_range when called on `null` value.
Expand Down Expand Up @@ -6592,8 +6601,8 @@ class basic_json
@note An iterator is called *initialized* when a pointer to a JSON value
has been set (e.g., by a constructor or a copy assignment). If the
iterator is default-constructed, it is *uninitialized* and most
methods are undefined. The library uses assertions to detect calls
on uninitialized iterators.
methods are undefined. **The library uses assertions to detect calls
on uninitialized iterators.**
@requirement The class satisfies the following concept requirements:
- [RandomAccessIterator](http://en.cppreference.com/w/cpp/concept/RandomAccessIterator):
Expand Down
19 changes: 14 additions & 5 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,8 @@ class basic_json
@param[in] first begin of the range to copy from (included)
@param[in] last end of the range to copy from (excluded)

@pre Iterators @a first and @a last must be initialized.
@pre Iterators @a first and @a last must be initialized. **This
precondition is enforced with an assertion.**

@throw std::domain_error if iterators are not compatible; that is, do not
belong to the same JSON value; example: `"iterators are not compatible"`
Expand Down Expand Up @@ -3509,6 +3510,9 @@ class basic_json

@return const reference to the element at key @a key

@pre The element with key @a key must exist. **This precondition is
enforced with an assertion.**

@throw std::domain_error if JSON is not an object; example: `"cannot use
operator[] with null"`

Expand Down Expand Up @@ -3667,6 +3671,9 @@ class basic_json

@return const reference to the element at key @a key

@pre The element with key @a key must exist. **This precondition is
enforced with an assertion.**

@throw std::domain_error if JSON is not an object; example: `"cannot use
operator[] with null"`

Expand Down Expand Up @@ -3867,7 +3874,8 @@ class basic_json
@complexity Constant.

@pre The JSON value must not be `null` (would throw `std::out_of_range`)
or an empty array or object (undefined behavior, guarded by assertions).
or an empty array or object (undefined behavior, **guarded by
assertions**).
@post The JSON value remains unchanged.

@throw std::out_of_range when called on `null` value
Expand Down Expand Up @@ -3909,7 +3917,8 @@ class basic_json
@complexity Constant.

@pre The JSON value must not be `null` (would throw `std::out_of_range`)
or an empty array or object (undefined behavior, guarded by assertions).
or an empty array or object (undefined behavior, **guarded by
assertions**).
@post The JSON value remains unchanged.

@throw std::out_of_range when called on `null` value.
Expand Down Expand Up @@ -6592,8 +6601,8 @@ class basic_json
@note An iterator is called *initialized* when a pointer to a JSON value
has been set (e.g., by a constructor or a copy assignment). If the
iterator is default-constructed, it is *uninitialized* and most
methods are undefined. The library uses assertions to detect calls
on uninitialized iterators.
methods are undefined. **The library uses assertions to detect calls
on uninitialized iterators.**

@requirement The class satisfies the following concept requirements:
- [RandomAccessIterator](http://en.cppreference.com/w/cpp/concept/RandomAccessIterator):
Expand Down

0 comments on commit 03de590

Please sign in to comment.