diff --git a/doc/Makefile b/doc/Makefile index df13ae40..218116d6 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -41,7 +41,7 @@ doxygen: create_output gsed -i 's@< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, AllocatorType >@@g' html/*.html gsed -i 's@< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, AllocatorType >@@g' html/*.html -upload: doxygen +upload: doxygen check_output cd html ; ../git-update-ghpages nlohmann/json rm -fr html open http://nlohmann.github.io/json/ diff --git a/doc/css/mylayout.css b/doc/css/mylayout.css index ef310b03..1b55b49e 100644 --- a/doc/css/mylayout.css +++ b/doc/css/mylayout.css @@ -1,7 +1,14 @@ -.memtemplate { +/* hide lengthy template information */ +.memtemplate, .memTemplParams { display: none; } -.memTemplParams { - display: none; +/* allow compiler information to wrap */ +/* https://css-tricks.com/snippets/css/make-pre-text-wrap/ */ +pre.fragment { + white-space: pre-wrap; /* css-3 */ + white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ + white-space: -pre-wrap; /* Opera 4-6 */ + white-space: -o-pre-wrap; /* Opera 7 */ + word-wrap: break-word; /* Internet Explorer 5.5+ */ } diff --git a/doc/examples/basic_json__CompatibleIntegerNumberType.cpp b/doc/examples/basic_json__CompatibleIntegerNumberType.cpp new file mode 100644 index 00000000..7f6eb45d --- /dev/null +++ b/doc/examples/basic_json__CompatibleIntegerNumberType.cpp @@ -0,0 +1,27 @@ +#include + +using namespace nlohmann; + +int main() +{ + // create values of different integer types + short n42 = 42; + int n23 = 23; + long n1024 = 1024; + int_least32_t n17 = 17; + uint8_t n8 = 8; + + // create JSON numbers + json j42(n42); + json j23(n23); + json j1024(n1024); + json j17(n17); + json j8(n8); + + // serialize the JSON numbers + std::cout << j42 << '\n'; + std::cout << j23 << '\n'; + std::cout << j1024 << '\n'; + std::cout << j17 << '\n'; + std::cout << j8 << '\n'; +} diff --git a/doc/examples/basic_json__CompatibleIntegerNumberType.output b/doc/examples/basic_json__CompatibleIntegerNumberType.output new file mode 100644 index 00000000..c7f24d63 --- /dev/null +++ b/doc/examples/basic_json__CompatibleIntegerNumberType.output @@ -0,0 +1,5 @@ +42 +23 +1024 +17 +8 diff --git a/doc/examples/basic_json__number_float_t.cpp b/doc/examples/basic_json__number_float_t.cpp new file mode 100644 index 00000000..88403231 --- /dev/null +++ b/doc/examples/basic_json__number_float_t.cpp @@ -0,0 +1,21 @@ +#include + +using namespace nlohmann; + +int main() +{ + // create values of different floating-point types + json::number_float_t v_ok = 3.141592653589793; + json::number_float_t v_nan = NAN; + json::number_float_t v_infinity = INFINITY; + + // create JSON numbers + json j_ok(v_ok); + json j_nan(v_nan); + json j_infinity(v_infinity); + + // serialize the JSON numbers + std::cout << j_ok << '\n'; + std::cout << j_nan << '\n'; + std::cout << j_infinity << '\n'; +} diff --git a/doc/examples/basic_json__number_float_t.output b/doc/examples/basic_json__number_float_t.output new file mode 100644 index 00000000..964a7b1f --- /dev/null +++ b/doc/examples/basic_json__number_float_t.output @@ -0,0 +1,3 @@ +3.14159265358979 +null +null diff --git a/doc/examples/basic_json__number_integer_t.cpp b/doc/examples/basic_json__number_integer_t.cpp new file mode 100644 index 00000000..a40dd3e8 --- /dev/null +++ b/doc/examples/basic_json__number_integer_t.cpp @@ -0,0 +1,14 @@ +#include + +using namespace nlohmann; + +int main() +{ + // create a JSON number from number_integer_t + json::number_integer_t value = 42; + + json j(value); + + // serialize the JSON numbers + std::cout << j << '\n'; +} diff --git a/doc/examples/basic_json__number_integer_t.output b/doc/examples/basic_json__number_integer_t.output new file mode 100644 index 00000000..d81cc071 --- /dev/null +++ b/doc/examples/basic_json__number_integer_t.output @@ -0,0 +1 @@ +42 diff --git a/src/json.hpp b/src/json.hpp index 1b3e335f..8cd614c9 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -600,7 +600,7 @@ class basic_json /*! @brief create a string (explicit) - Create an string JSON value with a given content. + Create a string JSON value with a given content. @param[in] value a literal value for the string @@ -636,13 +636,22 @@ class basic_json /*! @brief create an integer number (explicit) - @tparam T helper type to compare number_integer_t and int + Create an interger number JSON value with a given content. + + @tparam T helper type to compare number_integer_t and int (not visible in) + the interface. + @param[in] value an integer to create a JSON number from - This constructor takes care about explicitly passed values of type - number_integer_t. However, this constructor would have the same signature - as the existing one for const int values, so we need to switch this one off - in case number_integer_t is the same as int. + @note This constructor would have the same signature as @ref + basic_json(const int value), so we need to switch this one off in case + number_integer_t is the same as int. This is done via the helper type @a T. + + @complexity Constant. + + @todo Add example. + + @sa basic_json(const int) */ template(value)) {} - /// create an integer number (implicit) - template::value and - std::numeric_limits::is_integer, T>::type + std::is_constructible::value and + std::numeric_limits::is_integer, CompatibleNumberIntegerType>::type = 0> - basic_json(const T value) noexcept + basic_json(const CompatibleNumberIntegerType value) noexcept : m_type(value_t::number_integer), m_value(static_cast(value)) {} - /// create a floating-point number (explicit) + /*! + @brief create a floating-point number (explicit) + + Create a floating-point number JSON value with a given content. + + @param[in] value a floating-point value to create a JSON number from + + @note RFC 7159 , section 6 + disallows NaN values: + > Numeric values that cannot be represented in the grammar below (such + > as Infinity and NaN) are not permitted. + In case the parameter @a value is not a number, a JSON null value is + created instead. + + @complexity Linear. + + @liveexample{The following example creates several floating-point + values.,basic_json__number_float_t} + */ basic_json(const number_float_t value) : m_type(value_t::number_float), m_value(value) { @@ -693,12 +749,12 @@ class basic_json } /// create a floating-point number (implicit) - template::value and - std::is_floating_point::value>::type + std::is_constructible::value and + std::is_floating_point::value>::type > - basic_json(const T value) noexcept + basic_json(const CompatibleNumberFloatType value) noexcept : basic_json(number_float_t(value)) {} diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 7de48dff..8929cc3b 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -600,7 +600,7 @@ class basic_json /*! @brief create a string (explicit) - Create an string JSON value with a given content. + Create a string JSON value with a given content. @param[in] value a literal value for the string @@ -636,13 +636,22 @@ class basic_json /*! @brief create an integer number (explicit) - @tparam T helper type to compare number_integer_t and int + Create an interger number JSON value with a given content. + + @tparam T helper type to compare number_integer_t and int (not visible in) + the interface. + @param[in] value an integer to create a JSON number from - This constructor takes care about explicitly passed values of type - number_integer_t. However, this constructor would have the same signature - as the existing one for const int values, so we need to switch this one off - in case number_integer_t is the same as int. + @note This constructor would have the same signature as @ref + basic_json(const int value), so we need to switch this one off in case + number_integer_t is the same as int. This is done via the helper type @a T. + + @complexity Constant. + + @todo Add example. + + @sa basic_json(const int) */ template(value)) {} - /// create an integer number (implicit) - template::value and - std::numeric_limits::is_integer, T>::type + std::is_constructible::value and + std::numeric_limits::is_integer, CompatibleNumberIntegerType>::type = 0> - basic_json(const T value) noexcept + basic_json(const CompatibleNumberIntegerType value) noexcept : m_type(value_t::number_integer), m_value(static_cast(value)) {} - /// create a floating-point number (explicit) + /*! + @brief create a floating-point number (explicit) + + Create a floating-point number JSON value with a given content. + + @param[in] value a floating-point value to create a JSON number from + + @note RFC 7159 , section 6 + disallows NaN values: + > Numeric values that cannot be represented in the grammar below (such + > as Infinity and NaN) are not permitted. + In case the parameter @a value is not a number, a JSON null value is + created instead. + + @complexity Linear. + + @liveexample{The following example creates several floating-point + values.,basic_json__number_float_t} + */ basic_json(const number_float_t value) : m_type(value_t::number_float), m_value(value) { @@ -693,12 +749,12 @@ class basic_json } /// create a floating-point number (implicit) - template::value and - std::is_floating_point::value>::type + std::is_constructible::value and + std::is_floating_point::value>::type > - basic_json(const T value) noexcept + basic_json(const CompatibleNumberFloatType value) noexcept : basic_json(number_float_t(value)) {}