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