value_t.hpp 3.1 KB

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