- added functions is_structured() and is_primitive()
- updated documentation
- updated test cases
This commit is contained in:
Niels 2015-06-27 18:43:11 +02:00
parent 3ffedea5c4
commit c58c5aa8c9
11 changed files with 396 additions and 98 deletions

View file

@ -0,0 +1,25 @@
#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_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_primitive()
std::cout << std::boolalpha;
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_float.is_primitive() << '\n';
std::cout << j_object.is_primitive() << '\n';
std::cout << j_array.is_primitive() << '\n';
std::cout << j_string.is_primitive() << '\n';
}

View file

@ -0,0 +1,7 @@
true
true
true
true
false
false
true

View file

@ -0,0 +1,25 @@
#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_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_structured()
std::cout << std::boolalpha;
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_float.is_structured() << '\n';
std::cout << j_object.is_structured() << '\n';
std::cout << j_array.is_structured() << '\n';
std::cout << j_string.is_structured() << '\n';
}

View file

@ -0,0 +1,7 @@
false
false
false
false
true
true
false

View file

@ -0,0 +1,34 @@
#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_float = 23.42;
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
// call operator value_t()
json::value_t t_null = j_null;
json::value_t t_boolean = j_boolean;
json::value_t t_number_integer = j_number_integer;
json::value_t t_number_float = j_number_float;
json::value_t t_object = j_object;
json::value_t t_array = j_array;
json::value_t t_string = j_string;
// print types
std::cout << std::boolalpha;
std::cout << (t_null == json::value_t::null) << '\n';
std::cout << (t_boolean == json::value_t::boolean) << '\n';
std::cout << (t_number_integer == json::value_t::number_integer) << '\n';
std::cout << (t_number_float == json::value_t::number_float) << '\n';
std::cout << (t_object == json::value_t::object) << '\n';
std::cout << (t_array == json::value_t::array) << '\n';
std::cout << (t_string == json::value_t::string) << '\n';
}

View file

@ -0,0 +1,7 @@
true
true
true
true
true
true
true

25
doc/examples/type.cpp Normal file
View file

@ -0,0 +1,25 @@
#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_float = 23.42;
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
// call type()
std::cout << std::boolalpha;
std::cout << (j_null.type() == json::value_t::null) << '\n';
std::cout << (j_boolean.type() == json::value_t::boolean) << '\n';
std::cout << (j_number_integer.type() == json::value_t::number_integer) << '\n';
std::cout << (j_number_float.type() == json::value_t::number_float) << '\n';
std::cout << (j_object.type() == json::value_t::object) << '\n';
std::cout << (j_array.type() == json::value_t::array) << '\n';
std::cout << (j_string.type() == json::value_t::string) << '\n';
}

7
doc/examples/type.output Normal file
View file

@ -0,0 +1,7 @@
true
true
true
true
true
true
true

View file

@ -203,17 +203,16 @@ class basic_json
/// @} /// @}
///////////////////////////////// ///////////////////////////
// JSON value type enumeration // // JSON type enumeration //
///////////////////////////////// ///////////////////////////
/*! /*!
@brief the JSON value type enumeration @brief the JSON type enumeration
This enumeration collects the different JSON value types. It is internally This enumeration collects the different JSON types. It is internally used
used to distinguish the stored values, and the functions is_null, to distinguish the stored values, and the functions is_null, is_object,
is_object, is_array, is_string, is_boolean, is_number, and is_discarded is_array, is_string, is_boolean, is_number, and is_discarded rely on it.
rely on it.
*/ */
enum class value_t : uint8_t enum class value_t : uint8_t
{ {
@ -416,7 +415,7 @@ class basic_json
int depth, parse_event_t event, basic_json& parsed)>; int depth, parse_event_t event, basic_json& parsed)>;
/*! /*!
@brief comparison operator for JSON value types @brief comparison operator for JSON types
Returns an ordering that is similar to Python: Returns an ordering that is similar to Python:
- order: null < boolean < number < object < array < string - order: null < boolean < number < object < array < string
@ -1093,12 +1092,12 @@ class basic_json
Constructs the JSON value with the contents of the range `[first, last)`. Constructs the JSON value with the contents of the range `[first, last)`.
The semantics depends on the different types a JSON value can have: The semantics depends on the different types a JSON value can have:
- In case of atomic value types (number, boolean, or string), @a first must - In case of primitive types (number, boolean, or string), @a first must
be `begin()` and @a last must be `end()`. In this case, the value is be `begin()` and @a last must be `end()`. In this case, the value is
copied. Otherwise, std::out_of_range is thrown. copied. Otherwise, std::out_of_range is thrown.
- In case of compound value types (array, object), the constructor behaves - In case of structured types (array, object), the constructor behaves
as similar versions for `std::vector`. as similar versions for `std::vector`.
- In case of a null value type, std::domain_error is thrown. - In case of a null type, std::domain_error is thrown.
@tparam InputIT an input iterator type (@ref iterator or @ref @tparam InputIT an input iterator type (@ref iterator or @ref
const_iterator) const_iterator)
@ -1108,7 +1107,7 @@ class basic_json
@throw std::domain_error if iterators are not compatible; that is, do not @throw std::domain_error if iterators are not compatible; that is, do not
belong to the same JSON value belong to the same JSON value
@throw std::out_of_range if iterators are for an atomic value type (number, @throw std::out_of_range if iterators are for a primitive type (number,
boolean, or string) where an out of range error can be detected easily boolean, or string) where an out of range error can be detected easily
@throw std::bad_alloc if allocation for object, array, or string fails @throw std::bad_alloc if allocation for object, array, or string fails
@throw std::domain_error if called with a null value @throw std::domain_error if called with a null value
@ -1132,7 +1131,7 @@ class basic_json
throw std::domain_error("iterators are not compatible"); throw std::domain_error("iterators are not compatible");
} }
// check if iterator range is complete for atomic values // check if iterator range is complete for primitive values
switch (m_type) switch (m_type)
{ {
case value_t::number_integer: case value_t::number_integer:
@ -1439,23 +1438,65 @@ class basic_json
enumeration. enumeration.
@return the type of the JSON value @return the type of the JSON value
@complexity Constant.
@liveexample{The following code exemplifies @ref type() for all JSON
types.,type}
*/ */
value_t type() const noexcept value_t type() const noexcept
{ {
return m_type; return m_type;
} }
/*!
@brief return whether type is primitive
This function returns true iff the JSON type is primitive (string, number,
boolean, or null).
@return `true` if type is primitive (string, number, boolean, or null),
`false` otherwise.
@complexity Constant.
@liveexample{The following code exemplifies @ref is_primitive for all JSON
types.,is_primitive}
*/
bool is_primitive() const noexcept
{
return is_null() or is_string() or is_boolean() or is_number();
}
/*!
@brief return whether type is structured
This function returns true iff the JSON type is structured (array or
object).
@return `true` if type is structured (array or object), `false` otherwise.
@complexity Constant.
@liveexample{The following code exemplifies @ref is_structured for all JSON
types.,is_structured}
*/
bool is_structured() const noexcept
{
return is_array() or is_object();
}
/*! /*!
@brief return whether value is null @brief return whether value is null
This function returns true iff the JSON value is null. This function returns true iff the JSON value is null.
@return `true` if value type is null, `false` otherwise. @return `true` if type is null, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_null for all JSON @liveexample{The following code exemplifies @ref is_null for all JSON
value types.,is_null} types.,is_null}
*/ */
bool is_null() const noexcept bool is_null() const noexcept
{ {
@ -1467,12 +1508,12 @@ class basic_json
This function returns true iff the JSON value is a boolean. This function returns true iff the JSON value is a boolean.
@return `true` if value type is boolean, `false` otherwise. @return `true` if type is boolean, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_boolean for all JSON @liveexample{The following code exemplifies @ref is_boolean for all JSON
value types.,is_boolean} types.,is_boolean}
*/ */
bool is_boolean() const noexcept bool is_boolean() const noexcept
{ {
@ -1485,16 +1526,16 @@ class basic_json
This function returns true iff the JSON value is a number. This includes This function returns true iff the JSON value is a number. This includes
both integer and floating-point values. both integer and floating-point values.
@return `true` if value type is number, `false` otherwise. @return `true` if type is number, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_number for all JSON @liveexample{The following code exemplifies @ref is_number for all JSON
value types.,is_number} types.,is_number}
*/ */
bool is_number() const noexcept bool is_number() const noexcept
{ {
return (m_type == value_t::number_integer) or (m_type == value_t::number_float); return is_number_integer() or is_number_float();
} }
/*! /*!
@ -1503,12 +1544,12 @@ class basic_json
This function returns true iff the JSON value is an integer number. This This function returns true iff the JSON value is an integer number. This
excludes floating-point values. excludes floating-point values.
@return `true` if value type is an integer number, `false` otherwise. @return `true` if type is an integer number, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_number_integer for all @liveexample{The following code exemplifies @ref is_number_integer for all
JSON value types.,is_number_integer} JSON types.,is_number_integer}
*/ */
bool is_number_integer() const noexcept bool is_number_integer() const noexcept
{ {
@ -1521,12 +1562,12 @@ class basic_json
This function returns true iff the JSON value is a floating-point number. This function returns true iff the JSON value is a floating-point number.
This excludes integer values. This excludes integer values.
@return `true` if value type is a floating-point number, `false` otherwise. @return `true` if type is a floating-point number, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_number_float for all @liveexample{The following code exemplifies @ref is_number_float for all
JSON value types.,is_number_float} JSON types.,is_number_float}
*/ */
bool is_number_float() const noexcept bool is_number_float() const noexcept
{ {
@ -1538,12 +1579,12 @@ class basic_json
This function returns true iff the JSON value is an object. This function returns true iff the JSON value is an object.
@return `true` if value type is object, `false` otherwise. @return `true` if type is object, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_object for all JSON @liveexample{The following code exemplifies @ref is_object for all JSON
value types.,is_object} types.,is_object}
*/ */
bool is_object() const noexcept bool is_object() const noexcept
{ {
@ -1555,12 +1596,12 @@ class basic_json
This function returns true iff the JSON value is an array. This function returns true iff the JSON value is an array.
@return `true` if value type is array, `false` otherwise. @return `true` if type is array, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_array for all JSON @liveexample{The following code exemplifies @ref is_array for all JSON
value types.,is_array} types.,is_array}
*/ */
bool is_array() const noexcept bool is_array() const noexcept
{ {
@ -1572,25 +1613,48 @@ class basic_json
This function returns true iff the JSON value is a string. This function returns true iff the JSON value is a string.
@return `true` if value type is string, `false` otherwise. @return `true` if type is string, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_string for all JSON @liveexample{The following code exemplifies @ref is_string for all JSON
value types.,is_string} types.,is_string}
*/ */
bool is_string() const noexcept bool is_string() const noexcept
{ {
return m_type == value_t::string; return m_type == value_t::string;
} }
// return whether value is discarded /*!
@brief return whether value is discarded
This function returns true iff the JSON value was discarded during parsing
with a callback function (see @ref parser_callback_t).
@return `true` if type is discarded, `false` otherwise.
@complexity Constant.
@todo Add example.
*/
bool is_discarded() const noexcept bool is_discarded() const noexcept
{ {
return m_type == value_t::discarded; return m_type == value_t::discarded;
} }
/// return the type of the object (implicit) /*!
@brief return the type of the JSON value (implicit)
Implicitly return the type of the JSON value as a value from the @ref
value_t enumeration.
@return the type of the JSON value
@complexity Constant.
@liveexample{The following code exemplifies the value_t operator for all
JSON types.,operator__value_t}
*/
operator value_t() const noexcept operator value_t() const noexcept
{ {
return m_type; return m_type;
@ -1619,7 +1683,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be object, but is " + type_name()); throw std::domain_error("type must be object, but is " + type_name());
} }
} }
} }
@ -1635,7 +1699,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be object, but is " + type_name()); throw std::domain_error("type must be object, but is " + type_name());
} }
} }
} }
@ -1665,7 +1729,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be array, but is " + type_name()); throw std::domain_error("type must be array, but is " + type_name());
} }
} }
} }
@ -1693,7 +1757,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be array, but is " + type_name()); throw std::domain_error("type must be array, but is " + type_name());
} }
} }
} }
@ -1714,11 +1778,12 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be array, but is " + type_name()); throw std::domain_error("type must be array, but is " + type_name());
} }
} }
} }
/// get an array (explicit)
array_t get_impl(array_t*) const array_t get_impl(array_t*) const
{ {
switch (m_type) switch (m_type)
@ -1729,7 +1794,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be array, but is " + type_name()); throw std::domain_error("type must be array, but is " + type_name());
} }
} }
} }
@ -1749,7 +1814,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be string, but is " + type_name()); throw std::domain_error("type must be string, but is " + type_name());
} }
} }
} }
@ -1773,7 +1838,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be number, but is " + type_name()); throw std::domain_error("type must be number, but is " + type_name());
} }
} }
} }
@ -1789,7 +1854,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be boolean, but is " + type_name()); throw std::domain_error("type must be boolean, but is " + type_name());
} }
} }
} }
@ -2330,7 +2395,7 @@ class basic_json
Returns a reference to the first element in the container. For a JSON Returns a reference to the first element in the container. For a JSON
container `c`, the expression `c.front()` is equivalent to `*c.begin()`. container `c`, the expression `c.front()` is equivalent to `*c.begin()`.
@return In case of a compound value (array or object), a reference to the @return In case of a structured type (array or object), a reference to the
first element is returned. In cast of number, string, or boolean values, a first element is returned. In cast of number, string, or boolean values, a
reference to the value is returned. reference to the value is returned.
@ -2362,7 +2427,7 @@ class basic_json
container `c`, the expression `c.back()` is equivalent to `{ auto tmp = container `c`, the expression `c.back()` is equivalent to `{ auto tmp =
c.end(); --tmp; return *tmp; }`. c.end(); --tmp; return *tmp; }`.
@return In case of a compound value (array or object), a reference to the @return In case of a structured type (array or object), a reference to the
last element is returned. In cast of number, string, or boolean values, a last element is returned. In cast of number, string, or boolean values, a
reference to the value is returned. reference to the value is returned.
@ -2830,7 +2895,7 @@ class basic_json
Checks if a JSON value has no elements. Checks if a JSON value has no elements.
@return The return value depends on the different value types and is @return The return value depends on the different types and is
defined as follows: defined as follows:
Value type | return value Value type | return value
----------- | ------------- ----------- | -------------
@ -2886,7 +2951,7 @@ class basic_json
Returns the number of elements in a JSON value. Returns the number of elements in a JSON value.
@return The return value depends on the different value types and is @return The return value depends on the different types and is
defined as follows: defined as follows:
Value type | return value Value type | return value
----------- | ------------- ----------- | -------------
@ -2944,7 +3009,7 @@ class basic_json
system or library implementation limitations, i.e. `std::distance(begin(), system or library implementation limitations, i.e. `std::distance(begin(),
end())` for the JSON value. end())` for the JSON value.
@return The return value depends on the different value types and is @return The return value depends on the different types and is
defined as follows: defined as follows:
Value type | return value Value type | return value
----------- | ------------- ----------- | -------------
@ -3027,7 +3092,7 @@ class basic_json
@complexity Linear in the size of the JSON value. @complexity Linear in the size of the JSON value.
@liveexample{The example below shows the effect of @ref clear to different @liveexample{The example below shows the effect of @ref clear to different
JSON value types.,clear} JSON types.,clear}
*/ */
void clear() noexcept void clear() noexcept
{ {
@ -3919,7 +3984,7 @@ class basic_json
typename object_t::iterator object_iterator; typename object_t::iterator object_iterator;
/// iterator for JSON arrays /// iterator for JSON arrays
typename array_t::iterator array_iterator; typename array_t::iterator array_iterator;
/// generic iterator for all other value types /// generic iterator for all other types
difference_type generic_iterator; difference_type generic_iterator;
/// default constructor /// default constructor

View file

@ -203,17 +203,16 @@ class basic_json
/// @} /// @}
///////////////////////////////// ///////////////////////////
// JSON value type enumeration // // JSON type enumeration //
///////////////////////////////// ///////////////////////////
/*! /*!
@brief the JSON value type enumeration @brief the JSON type enumeration
This enumeration collects the different JSON value types. It is internally This enumeration collects the different JSON types. It is internally used
used to distinguish the stored values, and the functions is_null, to distinguish the stored values, and the functions is_null, is_object,
is_object, is_array, is_string, is_boolean, is_number, and is_discarded is_array, is_string, is_boolean, is_number, and is_discarded rely on it.
rely on it.
*/ */
enum class value_t : uint8_t enum class value_t : uint8_t
{ {
@ -416,7 +415,7 @@ class basic_json
int depth, parse_event_t event, basic_json& parsed)>; int depth, parse_event_t event, basic_json& parsed)>;
/*! /*!
@brief comparison operator for JSON value types @brief comparison operator for JSON types
Returns an ordering that is similar to Python: Returns an ordering that is similar to Python:
- order: null < boolean < number < object < array < string - order: null < boolean < number < object < array < string
@ -1093,12 +1092,12 @@ class basic_json
Constructs the JSON value with the contents of the range `[first, last)`. Constructs the JSON value with the contents of the range `[first, last)`.
The semantics depends on the different types a JSON value can have: The semantics depends on the different types a JSON value can have:
- In case of atomic value types (number, boolean, or string), @a first must - In case of primitive types (number, boolean, or string), @a first must
be `begin()` and @a last must be `end()`. In this case, the value is be `begin()` and @a last must be `end()`. In this case, the value is
copied. Otherwise, std::out_of_range is thrown. copied. Otherwise, std::out_of_range is thrown.
- In case of compound value types (array, object), the constructor behaves - In case of structured types (array, object), the constructor behaves
as similar versions for `std::vector`. as similar versions for `std::vector`.
- In case of a null value type, std::domain_error is thrown. - In case of a null type, std::domain_error is thrown.
@tparam InputIT an input iterator type (@ref iterator or @ref @tparam InputIT an input iterator type (@ref iterator or @ref
const_iterator) const_iterator)
@ -1108,7 +1107,7 @@ class basic_json
@throw std::domain_error if iterators are not compatible; that is, do not @throw std::domain_error if iterators are not compatible; that is, do not
belong to the same JSON value belong to the same JSON value
@throw std::out_of_range if iterators are for an atomic value type (number, @throw std::out_of_range if iterators are for a primitive type (number,
boolean, or string) where an out of range error can be detected easily boolean, or string) where an out of range error can be detected easily
@throw std::bad_alloc if allocation for object, array, or string fails @throw std::bad_alloc if allocation for object, array, or string fails
@throw std::domain_error if called with a null value @throw std::domain_error if called with a null value
@ -1132,7 +1131,7 @@ class basic_json
throw std::domain_error("iterators are not compatible"); throw std::domain_error("iterators are not compatible");
} }
// check if iterator range is complete for atomic values // check if iterator range is complete for primitive values
switch (m_type) switch (m_type)
{ {
case value_t::number_integer: case value_t::number_integer:
@ -1439,23 +1438,65 @@ class basic_json
enumeration. enumeration.
@return the type of the JSON value @return the type of the JSON value
@complexity Constant.
@liveexample{The following code exemplifies @ref type() for all JSON
types.,type}
*/ */
value_t type() const noexcept value_t type() const noexcept
{ {
return m_type; return m_type;
} }
/*!
@brief return whether type is primitive
This function returns true iff the JSON type is primitive (string, number,
boolean, or null).
@return `true` if type is primitive (string, number, boolean, or null),
`false` otherwise.
@complexity Constant.
@liveexample{The following code exemplifies @ref is_primitive for all JSON
types.,is_primitive}
*/
bool is_primitive() const noexcept
{
return is_null() or is_string() or is_boolean() or is_number();
}
/*!
@brief return whether type is structured
This function returns true iff the JSON type is structured (array or
object).
@return `true` if type is structured (array or object), `false` otherwise.
@complexity Constant.
@liveexample{The following code exemplifies @ref is_structured for all JSON
types.,is_structured}
*/
bool is_structured() const noexcept
{
return is_array() or is_object();
}
/*! /*!
@brief return whether value is null @brief return whether value is null
This function returns true iff the JSON value is null. This function returns true iff the JSON value is null.
@return `true` if value type is null, `false` otherwise. @return `true` if type is null, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_null for all JSON @liveexample{The following code exemplifies @ref is_null for all JSON
value types.,is_null} types.,is_null}
*/ */
bool is_null() const noexcept bool is_null() const noexcept
{ {
@ -1467,12 +1508,12 @@ class basic_json
This function returns true iff the JSON value is a boolean. This function returns true iff the JSON value is a boolean.
@return `true` if value type is boolean, `false` otherwise. @return `true` if type is boolean, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_boolean for all JSON @liveexample{The following code exemplifies @ref is_boolean for all JSON
value types.,is_boolean} types.,is_boolean}
*/ */
bool is_boolean() const noexcept bool is_boolean() const noexcept
{ {
@ -1485,16 +1526,16 @@ class basic_json
This function returns true iff the JSON value is a number. This includes This function returns true iff the JSON value is a number. This includes
both integer and floating-point values. both integer and floating-point values.
@return `true` if value type is number, `false` otherwise. @return `true` if type is number, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_number for all JSON @liveexample{The following code exemplifies @ref is_number for all JSON
value types.,is_number} types.,is_number}
*/ */
bool is_number() const noexcept bool is_number() const noexcept
{ {
return (m_type == value_t::number_integer) or (m_type == value_t::number_float); return is_number_integer() or is_number_float();
} }
/*! /*!
@ -1503,12 +1544,12 @@ class basic_json
This function returns true iff the JSON value is an integer number. This This function returns true iff the JSON value is an integer number. This
excludes floating-point values. excludes floating-point values.
@return `true` if value type is an integer number, `false` otherwise. @return `true` if type is an integer number, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_number_integer for all @liveexample{The following code exemplifies @ref is_number_integer for all
JSON value types.,is_number_integer} JSON types.,is_number_integer}
*/ */
bool is_number_integer() const noexcept bool is_number_integer() const noexcept
{ {
@ -1521,12 +1562,12 @@ class basic_json
This function returns true iff the JSON value is a floating-point number. This function returns true iff the JSON value is a floating-point number.
This excludes integer values. This excludes integer values.
@return `true` if value type is a floating-point number, `false` otherwise. @return `true` if type is a floating-point number, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_number_float for all @liveexample{The following code exemplifies @ref is_number_float for all
JSON value types.,is_number_float} JSON types.,is_number_float}
*/ */
bool is_number_float() const noexcept bool is_number_float() const noexcept
{ {
@ -1538,12 +1579,12 @@ class basic_json
This function returns true iff the JSON value is an object. This function returns true iff the JSON value is an object.
@return `true` if value type is object, `false` otherwise. @return `true` if type is object, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_object for all JSON @liveexample{The following code exemplifies @ref is_object for all JSON
value types.,is_object} types.,is_object}
*/ */
bool is_object() const noexcept bool is_object() const noexcept
{ {
@ -1555,12 +1596,12 @@ class basic_json
This function returns true iff the JSON value is an array. This function returns true iff the JSON value is an array.
@return `true` if value type is array, `false` otherwise. @return `true` if type is array, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_array for all JSON @liveexample{The following code exemplifies @ref is_array for all JSON
value types.,is_array} types.,is_array}
*/ */
bool is_array() const noexcept bool is_array() const noexcept
{ {
@ -1572,25 +1613,48 @@ class basic_json
This function returns true iff the JSON value is a string. This function returns true iff the JSON value is a string.
@return `true` if value type is string, `false` otherwise. @return `true` if type is string, `false` otherwise.
@complexity Constant. @complexity Constant.
@liveexample{The following code exemplifies @ref is_string for all JSON @liveexample{The following code exemplifies @ref is_string for all JSON
value types.,is_string} types.,is_string}
*/ */
bool is_string() const noexcept bool is_string() const noexcept
{ {
return m_type == value_t::string; return m_type == value_t::string;
} }
// return whether value is discarded /*!
@brief return whether value is discarded
This function returns true iff the JSON value was discarded during parsing
with a callback function (see @ref parser_callback_t).
@return `true` if type is discarded, `false` otherwise.
@complexity Constant.
@todo Add example.
*/
bool is_discarded() const noexcept bool is_discarded() const noexcept
{ {
return m_type == value_t::discarded; return m_type == value_t::discarded;
} }
/// return the type of the object (implicit) /*!
@brief return the type of the JSON value (implicit)
Implicitly return the type of the JSON value as a value from the @ref
value_t enumeration.
@return the type of the JSON value
@complexity Constant.
@liveexample{The following code exemplifies the value_t operator for all
JSON types.,operator__value_t}
*/
operator value_t() const noexcept operator value_t() const noexcept
{ {
return m_type; return m_type;
@ -1619,7 +1683,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be object, but is " + type_name()); throw std::domain_error("type must be object, but is " + type_name());
} }
} }
} }
@ -1635,7 +1699,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be object, but is " + type_name()); throw std::domain_error("type must be object, but is " + type_name());
} }
} }
} }
@ -1665,7 +1729,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be array, but is " + type_name()); throw std::domain_error("type must be array, but is " + type_name());
} }
} }
} }
@ -1693,7 +1757,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be array, but is " + type_name()); throw std::domain_error("type must be array, but is " + type_name());
} }
} }
} }
@ -1714,11 +1778,12 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be array, but is " + type_name()); throw std::domain_error("type must be array, but is " + type_name());
} }
} }
} }
/// get an array (explicit)
array_t get_impl(array_t*) const array_t get_impl(array_t*) const
{ {
switch (m_type) switch (m_type)
@ -1729,7 +1794,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be array, but is " + type_name()); throw std::domain_error("type must be array, but is " + type_name());
} }
} }
} }
@ -1749,7 +1814,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be string, but is " + type_name()); throw std::domain_error("type must be string, but is " + type_name());
} }
} }
} }
@ -1773,7 +1838,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be number, but is " + type_name()); throw std::domain_error("type must be number, but is " + type_name());
} }
} }
} }
@ -1789,7 +1854,7 @@ class basic_json
} }
default: default:
{ {
throw std::domain_error("value type must be boolean, but is " + type_name()); throw std::domain_error("type must be boolean, but is " + type_name());
} }
} }
} }
@ -2330,7 +2395,7 @@ class basic_json
Returns a reference to the first element in the container. For a JSON Returns a reference to the first element in the container. For a JSON
container `c`, the expression `c.front()` is equivalent to `*c.begin()`. container `c`, the expression `c.front()` is equivalent to `*c.begin()`.
@return In case of a compound value (array or object), a reference to the @return In case of a structured type (array or object), a reference to the
first element is returned. In cast of number, string, or boolean values, a first element is returned. In cast of number, string, or boolean values, a
reference to the value is returned. reference to the value is returned.
@ -2362,7 +2427,7 @@ class basic_json
container `c`, the expression `c.back()` is equivalent to `{ auto tmp = container `c`, the expression `c.back()` is equivalent to `{ auto tmp =
c.end(); --tmp; return *tmp; }`. c.end(); --tmp; return *tmp; }`.
@return In case of a compound value (array or object), a reference to the @return In case of a structured type (array or object), a reference to the
last element is returned. In cast of number, string, or boolean values, a last element is returned. In cast of number, string, or boolean values, a
reference to the value is returned. reference to the value is returned.
@ -2830,7 +2895,7 @@ class basic_json
Checks if a JSON value has no elements. Checks if a JSON value has no elements.
@return The return value depends on the different value types and is @return The return value depends on the different types and is
defined as follows: defined as follows:
Value type | return value Value type | return value
----------- | ------------- ----------- | -------------
@ -2886,7 +2951,7 @@ class basic_json
Returns the number of elements in a JSON value. Returns the number of elements in a JSON value.
@return The return value depends on the different value types and is @return The return value depends on the different types and is
defined as follows: defined as follows:
Value type | return value Value type | return value
----------- | ------------- ----------- | -------------
@ -2944,7 +3009,7 @@ class basic_json
system or library implementation limitations, i.e. `std::distance(begin(), system or library implementation limitations, i.e. `std::distance(begin(),
end())` for the JSON value. end())` for the JSON value.
@return The return value depends on the different value types and is @return The return value depends on the different types and is
defined as follows: defined as follows:
Value type | return value Value type | return value
----------- | ------------- ----------- | -------------
@ -3027,7 +3092,7 @@ class basic_json
@complexity Linear in the size of the JSON value. @complexity Linear in the size of the JSON value.
@liveexample{The example below shows the effect of @ref clear to different @liveexample{The example below shows the effect of @ref clear to different
JSON value types.,clear} JSON types.,clear}
*/ */
void clear() noexcept void clear() noexcept
{ {
@ -3919,7 +3984,7 @@ class basic_json
typename object_t::iterator object_iterator; typename object_t::iterator object_iterator;
/// iterator for JSON arrays /// iterator for JSON arrays
typename array_t::iterator array_iterator; typename array_t::iterator array_iterator;
/// generic iterator for all other value types /// generic iterator for all other types
difference_type generic_iterator; difference_type generic_iterator;
/// default constructor /// default constructor

View file

@ -1334,6 +1334,8 @@ TEST_CASE("object inspection")
CHECK(not j.is_array()); CHECK(not j.is_array());
CHECK(not j.is_string()); CHECK(not j.is_string());
CHECK(not j.is_discarded()); CHECK(not j.is_discarded());
CHECK(not j.is_primitive());
CHECK(j.is_structured());
} }
SECTION("array") SECTION("array")
@ -1348,6 +1350,8 @@ TEST_CASE("object inspection")
CHECK(j.is_array()); CHECK(j.is_array());
CHECK(not j.is_string()); CHECK(not j.is_string());
CHECK(not j.is_discarded()); CHECK(not j.is_discarded());
CHECK(not j.is_primitive());
CHECK(j.is_structured());
} }
SECTION("null") SECTION("null")
@ -1362,6 +1366,8 @@ TEST_CASE("object inspection")
CHECK(not j.is_array()); CHECK(not j.is_array());
CHECK(not j.is_string()); CHECK(not j.is_string());
CHECK(not j.is_discarded()); CHECK(not j.is_discarded());
CHECK(j.is_primitive());
CHECK(not j.is_structured());
} }
SECTION("boolean") SECTION("boolean")
@ -1376,6 +1382,8 @@ TEST_CASE("object inspection")
CHECK(not j.is_array()); CHECK(not j.is_array());
CHECK(not j.is_string()); CHECK(not j.is_string());
CHECK(not j.is_discarded()); CHECK(not j.is_discarded());
CHECK(j.is_primitive());
CHECK(not j.is_structured());
} }
SECTION("string") SECTION("string")
@ -1390,6 +1398,8 @@ TEST_CASE("object inspection")
CHECK(not j.is_array()); CHECK(not j.is_array());
CHECK(j.is_string()); CHECK(j.is_string());
CHECK(not j.is_discarded()); CHECK(not j.is_discarded());
CHECK(j.is_primitive());
CHECK(not j.is_structured());
} }
SECTION("number (integer)") SECTION("number (integer)")
@ -1404,6 +1414,8 @@ TEST_CASE("object inspection")
CHECK(not j.is_array()); CHECK(not j.is_array());
CHECK(not j.is_string()); CHECK(not j.is_string());
CHECK(not j.is_discarded()); CHECK(not j.is_discarded());
CHECK(j.is_primitive());
CHECK(not j.is_structured());
} }
SECTION("number (floating-point)") SECTION("number (floating-point)")
@ -1418,6 +1430,8 @@ TEST_CASE("object inspection")
CHECK(not j.is_array()); CHECK(not j.is_array());
CHECK(not j.is_string()); CHECK(not j.is_string());
CHECK(not j.is_discarded()); CHECK(not j.is_discarded());
CHECK(j.is_primitive());
CHECK(not j.is_structured());
} }
SECTION("discarded") SECTION("discarded")
@ -1432,6 +1446,8 @@ TEST_CASE("object inspection")
CHECK(not j.is_array()); CHECK(not j.is_array());
CHECK(not j.is_string()); CHECK(not j.is_string());
CHECK(j.is_discarded()); CHECK(j.is_discarded());
CHECK(not j.is_primitive());
CHECK(not j.is_structured());
} }
} }
@ -7670,6 +7686,20 @@ TEST_CASE("parser class")
CHECK(json::parser("-0E1").parse() == json(-0e1)); CHECK(json::parser("-0E1").parse() == json(-0e1));
CHECK(json::parser("-0E123").parse() == json(-0e123)); CHECK(json::parser("-0E123").parse() == json(-0e123));
} }
SECTION("edge cases")
{
// From RFC7159, Section 6:
// Note that when such software is used, numbers that are
// integers and are in the range [-(2**53)+1, (2**53)-1]
// are interoperable in the sense that implementations will
// agree exactly on their numeric values.
// -(2**53)+1
CHECK(json::parser("-9007199254740991").parse().get<int64_t>() == -9007199254740991);
// (2**53)-1
CHECK(json::parser("9007199254740991").parse().get<int64_t>() == 9007199254740991);
}
} }
SECTION("floating-point") SECTION("floating-point")
@ -8961,6 +8991,7 @@ TEST_CASE("RFC 7159 examples")
{ {
CHECK(json::parse("\"\\u005C\"") == json("\\")); CHECK(json::parse("\"\\u005C\"") == json("\\"));
CHECK(json::parse("\"\\uD834\\uDD1E\"") == json("𝄞")); CHECK(json::parse("\"\\uD834\\uDD1E\"") == json("𝄞"));
CHECK(json::parse("\"𝄞\"") == json("𝄞"));
} }
SECTION("8.3 String Comparison") SECTION("8.3 String Comparison")