/* __ _____ _____ _____ __| | __| | | | JSON for Modern C++ | | |__ | | | | | | version 2.0.10 |_____|_____|_____|_|___| https://github.com/nlohmann/json Licensed under the MIT License . Copyright (c) 2013-2017 Niels Lohmann . Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef NLOHMANN_JSON_HPP #define NLOHMANN_JSON_HPP #include // all_of, for_each, transform #include // array #include // assert #include // isdigit #include // and, not, or #include // isfinite, ldexp, signbit #include // nullptr_t, ptrdiff_t, size_t #include // int64_t, uint64_t #include // strtod, strtof, strtold, strtoul #include // strlen #include // function, hash, less #include // initializer_list #include // setw #include // istream, ostream #include // advance, begin, bidirectional_iterator_tag, distance, end, inserter, iterator, iterator_traits, next, random_access_iterator_tag, reverse_iterator #include // numeric_limits #include // locale #include // map #include // addressof, allocator, allocator_traits, unique_ptr #include // accumulate #include // stringstream #include // domain_error, invalid_argument, out_of_range #include // getline, stoi, string, to_string #include // add_pointer, enable_if, is_arithmetic, is_base_of, is_const, is_constructible, is_convertible, is_floating_point, is_integral, is_nothrow_move_assignable, std::is_nothrow_move_constructible, std::is_pointer, std::is_reference, std::is_same, remove_const, remove_pointer, remove_reference #include // declval, forward, make_pair, move, pair, swap #include // vector // exclude unsupported compilers #if defined(__clang__) #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400 #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" #endif #elif defined(__GNUC__) #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40900 #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers" #endif #endif // disable float-equal warnings on GCC/clang #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wfloat-equal" #endif // disable documentation warnings on clang #if defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdocumentation" #endif // allow for portable deprecation warnings #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #define JSON_DEPRECATED __attribute__((deprecated)) #elif defined(_MSC_VER) #define JSON_DEPRECATED __declspec(deprecated) #else #define JSON_DEPRECATED #endif // allow to disable exceptions #if not defined(JSON_NOEXCEPTION) || defined(__EXCEPTIONS) #define JSON_THROW(exception) throw exception #define JSON_TRY try #define JSON_CATCH(exception) catch(exception) #else #define JSON_THROW(exception) std::abort() #define JSON_TRY if(true) #define JSON_CATCH(exception) if(false) #endif /*! @brief namespace for Niels Lohmann @see https://github.com/nlohmann @since version 1.0.0 */ namespace nlohmann { /////////////////////////// // 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 is_null(), @ref is_object(), @ref is_array(), @ref is_string(), @ref is_boolean(), @ref is_number() (with @ref is_number_integer(), @ref is_number_unsigned(), and @ref is_number_float()), @ref is_discarded(), @ref is_primitive(), and @ref 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 number_unsigned_t is used for unsigned integers, @ref number_integer_t is used for signed integers, and @ref 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(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 }; // alias templates to reduce boilerplate template using enable_if_t = typename std::enable_if::type; template using remove_cv_t = typename std::remove_cv::type; template using remove_reference_t = typename std::remove_reference::type; template using uncvref_t = remove_cv_t>; template using conditional_t = typename std::conditional::type; // Taken from http://stackoverflow.com/questions/26936640/how-to-implement-is-enum-class-type-trait template using is_scoped_enum = std::integral_constant::value and std::is_enum::value>; template using is_unscoped_enum = std::integral_constant::value and std::is_enum::value>; // TODO update this doc /*! @brief unnamed namespace with internal helper functions @since version 1.0.0 */ namespace detail { template std::string type_name(Json const &j) { switch (j.m_type) { case value_t::null: return "null"; case value_t::object: return "object"; case value_t::array: return "array"; case value_t::string: return "string"; case value_t::boolean: return "boolean"; case value_t::discarded: return "discarded"; default: return "number"; } } // This is an experiment. I need this to move constructors out of basic_json. // I'm sure there is a better way, but this might need a big basic_json refactoring template struct external_constructor; template <> struct external_constructor { template static void construct(Json &j, typename Json::boolean_t b) noexcept { j.m_type = value_t::boolean; j.m_value = b; j.assert_invariant(); } }; template <> struct external_constructor { template static void construct(Json &j, const typename Json::string_t& s) { j.m_type = value_t::string; j.m_value = s; j.assert_invariant(); } }; // very useful construct against boilerplate (more boilerplate needed than in // C++17: http://en.cppreference.com/w/cpp/types/void_t) template struct make_void { using type = void; }; template using void_t = typename make_void::type; // Implementation of 3 C++17 constructs: conjunction, disjunction, negation. // This is needed to avoid evaluating all the traits in a condition // // For example: not std::is_same::value and has_value_type::value // will not compile when T = void (on MSVC at least) // Whereas conjunction>, has_value_type>::value // will stop evaluating if negation<...>::value == false // // Please note that those constructs must be used with caution, since symbols can // become very long quickly (which can slow down compilation and cause MSVC internal compiler errors) // Only use it when you have too (see example ahead) template struct conjunction : std::true_type {}; template struct conjunction : B1 {}; template struct conjunction : conditional_t, B1> {}; template struct negation : std::integral_constant < bool, !B::value > {}; template struct disjunction : std::false_type {}; template struct disjunction : B1 {}; template struct disjunction : conditional_t> {}; /*! @brief Helper to determine whether there's a key_type for T. This helper is used to tell associative containers apart from other containers such as sequence containers. For instance, `std::map` passes the test as it contains a `mapped_type`, whereas `std::vector` fails the test. @sa http://stackoverflow.com/a/7728728/266378 @since version 1.0.0, overworked in version 2.0.6 */ #define NLOHMANN_JSON_HAS_HELPER(type) \ template struct has_##type { \ private: \ template \ static int detect(U &&); \ static void detect(...); \ public: \ static constexpr bool value = \ std::is_integral()))>::value; \ }; NLOHMANN_JSON_HAS_HELPER(mapped_type); NLOHMANN_JSON_HAS_HELPER(key_type); NLOHMANN_JSON_HAS_HELPER(value_type); NLOHMANN_JSON_HAS_HELPER(iterator); #undef NLOHMANN_JSON_HAS_HELPER template struct is_compatible_object_type_impl : std::false_type {}; template struct is_compatible_object_type_impl { static constexpr auto value = std::is_constructible::value and std::is_constructible::value; }; template struct is_compatible_object_type { // As noted ahead, we need to stop evaluating traits if // `CompatibleObjectType = void`, hence the conjunction static auto constexpr value = is_compatible_object_type_impl < conjunction>, has_mapped_type, has_key_type>::value, RealType, CompatibleObjectType >::value; }; template struct is_compatible_array_type_impl : std::false_type {}; template struct is_compatible_array_type_impl { static constexpr auto value = not std::is_same::value and not std::is_same::value and not std::is_same::value and not std::is_same::value and not std::is_same::value and not std::is_same::value; }; template struct is_compatible_array_type { // the check for CompatibleArrayType = void is done in // `is_compatible_object_type`, but we need the conjunction here as well static auto constexpr value = is_compatible_array_type_impl< conjunction>, negation>, has_value_type, has_iterator>::value, BasicJson, CompatibleArrayType>::value; }; template struct is_compatible_integer_type_impl : std::false_type {}; template struct is_compatible_integer_type_impl { using RealLimits = std::numeric_limits; using CompatibleLimits = std::numeric_limits; static constexpr auto value = std::is_constructible::value and CompatibleLimits::is_integer and RealLimits::is_signed == CompatibleLimits::is_signed; }; template struct is_compatible_integer_type { static constexpr auto value = is_compatible_integer_type_impl < std::is_arithmetic::value and not std::is_same::value, RealIntegerType, CompatibleNumberIntegerType > ::value; }; template struct is_compatible_float_type { static constexpr auto value = std::is_constructible::value and std::is_floating_point::value; }; template struct is_compatible_basic_json_type { static auto constexpr value = is_unscoped_enum::value or std::is_same::value or is_compatible_array_type::value or is_compatible_object_type::value or is_compatible_float_type::value or is_compatible_integer_type::value or is_compatible_integer_type::value; }; template struct is_basic_json_nested_class { static auto constexpr value = std::is_same::value or std::is_same::value or std::is_same::value or std::is_same::value or std::is_same::value or std::is_same::value; }; // This trait checks if JSONSerializer::from_json(json const&, udt&) exists template