value_t.hpp 2.7 KB

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