add detail/value_t.hpp
This commit is contained in:
parent
8c555db970
commit
7056b375c4
3 changed files with 81 additions and 65 deletions
3
Makefile
3
Makefile
|
@ -6,7 +6,8 @@ SRCS = ${SRCDIR}/json.hpp \
|
||||||
${SRCDIR}/detail/macro_scope.hpp \
|
${SRCDIR}/detail/macro_scope.hpp \
|
||||||
${SRCDIR}/detail/macro_unscope.hpp \
|
${SRCDIR}/detail/macro_unscope.hpp \
|
||||||
${SRCDIR}/detail/meta.hpp \
|
${SRCDIR}/detail/meta.hpp \
|
||||||
${SRCDIR}/detail/exceptions.hpp
|
${SRCDIR}/detail/exceptions.hpp \
|
||||||
|
${SRCDIR}/detail/value_t.hpp
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
78
src/detail/value_t.hpp
Normal file
78
src/detail/value_t.hpp
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
#ifndef NLOHMANN_JSON_DETAIL_VALUE_T_HPP
|
||||||
|
#define NLOHMANN_JSON_DETAIL_VALUE_T_HPP
|
||||||
|
|
||||||
|
#include <ciso646> // and
|
||||||
|
#include <cstddef> // size_t
|
||||||
|
#include <cstdint> // uint8_t
|
||||||
|
|
||||||
|
namespace nlohmann
|
||||||
|
{
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
///////////////////////////
|
||||||
|
// JSON type enumeration //
|
||||||
|
///////////////////////////
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief the JSON type enumeration
|
||||||
|
|
||||||
|
This enumeration collects the different JSON types. It is internally used to
|
||||||
|
distinguish the stored values, and the functions @ref basic_json::is_null(),
|
||||||
|
@ref basic_json::is_object(), @ref basic_json::is_array(),
|
||||||
|
@ref basic_json::is_string(), @ref basic_json::is_boolean(),
|
||||||
|
@ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
|
||||||
|
@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
|
||||||
|
@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
|
||||||
|
@ref basic_json::is_structured() rely on it.
|
||||||
|
|
||||||
|
@note There are three enumeration entries (number_integer, number_unsigned, and
|
||||||
|
number_float), because the library distinguishes these three types for numbers:
|
||||||
|
@ref basic_json::number_unsigned_t is used for unsigned integers,
|
||||||
|
@ref basic_json::number_integer_t is used for signed integers, and
|
||||||
|
@ref basic_json::number_float_t is used for floating-point numbers or to
|
||||||
|
approximate integers which do not fit in the limits of their respective type.
|
||||||
|
|
||||||
|
@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON
|
||||||
|
value with the default value for a given type
|
||||||
|
|
||||||
|
@since version 1.0.0
|
||||||
|
*/
|
||||||
|
enum class value_t : uint8_t
|
||||||
|
{
|
||||||
|
null, ///< null value
|
||||||
|
object, ///< object (unordered set of name/value pairs)
|
||||||
|
array, ///< array (ordered collection of values)
|
||||||
|
string, ///< string value
|
||||||
|
boolean, ///< boolean value
|
||||||
|
number_integer, ///< number value (signed integer)
|
||||||
|
number_unsigned, ///< number value (unsigned integer)
|
||||||
|
number_float, ///< number value (floating-point)
|
||||||
|
discarded ///< discarded by the the parser callback function
|
||||||
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief comparison operator for JSON types
|
||||||
|
|
||||||
|
Returns an ordering that is similar to Python:
|
||||||
|
- order: null < boolean < number < object < array < string
|
||||||
|
- furthermore, each type is not smaller than itself
|
||||||
|
- discarded values are not comparable
|
||||||
|
|
||||||
|
@since version 1.0.0
|
||||||
|
*/
|
||||||
|
inline bool operator<(const value_t lhs, const value_t rhs) noexcept
|
||||||
|
{
|
||||||
|
static constexpr std::array<uint8_t, 8> order = {{
|
||||||
|
0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
|
||||||
|
1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto l_index = static_cast<std::size_t>(lhs);
|
||||||
|
const auto r_index = static_cast<std::size_t>(rhs);
|
||||||
|
return l_index < order.size() and r_index < order.size() and order[l_index] < order[r_index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
65
src/json.hpp
65
src/json.hpp
|
@ -55,6 +55,7 @@ SOFTWARE.
|
||||||
#include "detail/macro_scope.hpp"
|
#include "detail/macro_scope.hpp"
|
||||||
#include "detail/meta.hpp"
|
#include "detail/meta.hpp"
|
||||||
#include "detail/exceptions.hpp"
|
#include "detail/exceptions.hpp"
|
||||||
|
#include "detail/value_t.hpp"
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief namespace for Niels Lohmann
|
@brief namespace for Niels Lohmann
|
||||||
|
@ -65,70 +66,6 @@ namespace nlohmann
|
||||||
{
|
{
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
///////////////////////////
|
|
||||||
// JSON type enumeration //
|
|
||||||
///////////////////////////
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief the JSON type enumeration
|
|
||||||
|
|
||||||
This enumeration collects the different JSON types. It is internally used to
|
|
||||||
distinguish the stored values, and the functions @ref basic_json::is_null(),
|
|
||||||
@ref basic_json::is_object(), @ref basic_json::is_array(),
|
|
||||||
@ref basic_json::is_string(), @ref basic_json::is_boolean(),
|
|
||||||
@ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
|
|
||||||
@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
|
|
||||||
@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
|
|
||||||
@ref basic_json::is_structured() rely on it.
|
|
||||||
|
|
||||||
@note There are three enumeration entries (number_integer, number_unsigned, and
|
|
||||||
number_float), because the library distinguishes these three types for numbers:
|
|
||||||
@ref basic_json::number_unsigned_t is used for unsigned integers,
|
|
||||||
@ref basic_json::number_integer_t is used for signed integers, and
|
|
||||||
@ref basic_json::number_float_t is used for floating-point numbers or to
|
|
||||||
approximate integers which do not fit in the limits of their respective type.
|
|
||||||
|
|
||||||
@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON
|
|
||||||
value with the default value for a given type
|
|
||||||
|
|
||||||
@since version 1.0.0
|
|
||||||
*/
|
|
||||||
enum class value_t : uint8_t
|
|
||||||
{
|
|
||||||
null, ///< null value
|
|
||||||
object, ///< object (unordered set of name/value pairs)
|
|
||||||
array, ///< array (ordered collection of values)
|
|
||||||
string, ///< string value
|
|
||||||
boolean, ///< boolean value
|
|
||||||
number_integer, ///< number value (signed integer)
|
|
||||||
number_unsigned, ///< number value (unsigned integer)
|
|
||||||
number_float, ///< number value (floating-point)
|
|
||||||
discarded ///< discarded by the the parser callback function
|
|
||||||
};
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief comparison operator for JSON types
|
|
||||||
|
|
||||||
Returns an ordering that is similar to Python:
|
|
||||||
- order: null < boolean < number < object < array < string
|
|
||||||
- furthermore, each type is not smaller than itself
|
|
||||||
- discarded values are not comparable
|
|
||||||
|
|
||||||
@since version 1.0.0
|
|
||||||
*/
|
|
||||||
inline bool operator<(const value_t lhs, const value_t rhs) noexcept
|
|
||||||
{
|
|
||||||
static constexpr std::array<uint8_t, 8> order = {{
|
|
||||||
0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
|
|
||||||
1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const auto l_index = static_cast<std::size_t>(lhs);
|
|
||||||
const auto r_index = static_cast<std::size_t>(rhs);
|
|
||||||
return l_index < order.size() and r_index < order.size() and order[l_index] < order[r_index];
|
|
||||||
}
|
|
||||||
|
|
||||||
//////////////////
|
//////////////////
|
||||||
// constructors //
|
// constructors //
|
||||||
//////////////////
|
//////////////////
|
||||||
|
|
Loading…
Reference in a new issue