• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 
3 #include <array> // array
4 #include <cstddef> // size_t
5 #include <cstdint> // uint8_t
6 #include <string> // string
7 
8 namespace nlohmann
9 {
10 namespace detail
11 {
12 ///////////////////////////
13 // JSON type enumeration //
14 ///////////////////////////
15 
16 /*!
17 @brief the JSON type enumeration
18 
19 This enumeration collects the different JSON types. It is internally used to
20 distinguish the stored values, and the functions @ref basic_json::is_null(),
21 @ref basic_json::is_object(), @ref basic_json::is_array(),
22 @ref basic_json::is_string(), @ref basic_json::is_boolean(),
23 @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
24 @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
25 @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
26 @ref basic_json::is_structured() rely on it.
27 
28 @note There are three enumeration entries (number_integer, number_unsigned, and
29 number_float), because the library distinguishes these three types for numbers:
30 @ref basic_json::number_unsigned_t is used for unsigned integers,
31 @ref basic_json::number_integer_t is used for signed integers, and
32 @ref basic_json::number_float_t is used for floating-point numbers or to
33 approximate integers which do not fit in the limits of their respective type.
34 
35 @sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON
36 value with the default value for a given type
37 
38 @since version 1.0.0
39 */
40 enum class value_t : std::uint8_t
41 {
42     null,             ///< null value
43     object,           ///< object (unordered set of name/value pairs)
44     array,            ///< array (ordered collection of values)
45     string,           ///< string value
46     boolean,          ///< boolean value
47     number_integer,   ///< number value (signed integer)
48     number_unsigned,  ///< number value (unsigned integer)
49     number_float,     ///< number value (floating-point)
50     binary,           ///< binary array (ordered collection of bytes)
51     discarded         ///< discarded by the parser callback function
52 };
53 
54 /*!
55 @brief comparison operator for JSON types
56 
57 Returns an ordering that is similar to Python:
58 - order: null < boolean < number < object < array < string < binary
59 - furthermore, each type is not smaller than itself
60 - discarded values are not comparable
61 - binary is represented as a b"" string in python and directly comparable to a
62   string; however, making a binary array directly comparable with a string would
63   be surprising behavior in a JSON file.
64 
65 @since version 1.0.0
66 */
operator <(const value_t lhs,const value_t rhs)67 inline bool operator<(const value_t lhs, const value_t rhs) noexcept
68 {
69     static constexpr std::array<std::uint8_t, 9> order = {{
70             0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
71             1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
72             6 /* binary */
73         }
74     };
75 
76     const auto l_index = static_cast<std::size_t>(lhs);
77     const auto r_index = static_cast<std::size_t>(rhs);
78     return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
79 }
80 }  // namespace detail
81 }  // namespace nlohmann
82