1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-31 10:24:23 +03:00

Merge branch 'develop' of https://github.com/nlohmann/json into issue2824

This commit is contained in:
Niels Lohmann
2021-08-11 08:06:32 +02:00
12 changed files with 59 additions and 7 deletions

View File

@ -1612,6 +1612,7 @@ The library supports **Unicode input** as follows:
- Invalid surrogates (e.g., incomplete pairs such as `\uDEAD`) will yield parse errors. - Invalid surrogates (e.g., incomplete pairs such as `\uDEAD`) will yield parse errors.
- The strings stored in the library are UTF-8 encoded. When using the default string type (`std::string`), note that its length/size functions return the number of stored bytes rather than the number of characters or glyphs. - The strings stored in the library are UTF-8 encoded. When using the default string type (`std::string`), note that its length/size functions return the number of stored bytes rather than the number of characters or glyphs.
- When you store strings with different encodings in the library, calling [`dump()`](https://nlohmann.github.io/json/api/basic_json/dump/) may throw an exception unless `json::error_handler_t::replace` or `json::error_handler_t::ignore` are used as error handlers. - When you store strings with different encodings in the library, calling [`dump()`](https://nlohmann.github.io/json/api/basic_json/dump/) may throw an exception unless `json::error_handler_t::replace` or `json::error_handler_t::ignore` are used as error handlers.
- To store wide strings (e.g., `std::wstring`), you need to convert them to a a UTF-8 encoded `std::string` before, see [an example](https://json.nlohmann.me/home/faq/#wide-string-handling).
### Comments in JSON ### Comments in JSON

View File

@ -44,7 +44,7 @@ for objects.
!!! question !!! question
- Can you add an option to ignore trailing commas? Can you add an option to ignore trailing commas?
This library does not support any feature which would jeopardize interoperability. This library does not support any feature which would jeopardize interoperability.
@ -70,6 +70,45 @@ The library supports **Unicode input** as follows:
In most cases, the parser is right to complain, because the input is not UTF-8 encoded. This is especially true for Microsoft Windows where Latin-1 or ISO 8859-1 is often the standard encoding. In most cases, the parser is right to complain, because the input is not UTF-8 encoded. This is especially true for Microsoft Windows where Latin-1 or ISO 8859-1 is often the standard encoding.
### Wide string handling
!!! question
Why are wide strings (e.g., `std::wstring`) dumped as arrays of numbers?
As described [above](#parse-errors-reading-non-ascii-characters), the library assumes UTF-8 as encoding. To store a wide string, you need to change the encoding.
!!! example
```cpp
#include <codecvt> // codecvt_utf8
#include <locale> // wstring_convert
// encoding function
std::string to_utf8(std::wstring& wide_string)
{
static std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_conv;
return utf8_conv.to_bytes(wide_string);
}
json j;
std::wstring ws = L"車B1234 こんにちは";
j["original"] = ws;
j["encoded"] = to_utf8(ws);
std::cout << j << std::endl;
```
The result is:
```json
{
"encoded": "車B1234 こんにちは",
"original": [36554, 66, 49, 50, 51, 52, 32, 12371, 12435, 12395, 12385, 12399]
}
```
## Exceptions ## Exceptions
### Parsing without exceptions ### Parsing without exceptions

View File

@ -1066,6 +1066,8 @@ char* to_chars(char* first, const char* last, FloatType value)
*first++ = '-'; *first++ = '-';
} }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
if (value == 0) // +-0 if (value == 0) // +-0
{ {
*first++ = '0'; *first++ = '0';
@ -1074,6 +1076,7 @@ char* to_chars(char* first, const char* last, FloatType value)
*first++ = '0'; *first++ = '0';
return first; return first;
} }
#pragma GCC diagnostic pop
JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10); JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);

View File

@ -1525,6 +1525,8 @@ class binary_writer
void write_compact_float(const number_float_t n, detail::input_format_t format) void write_compact_float(const number_float_t n, detail::input_format_t format)
{ {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) && if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) && static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
static_cast<double>(static_cast<float>(n)) == static_cast<double>(n)) static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
@ -1541,6 +1543,7 @@ class binary_writer
: get_msgpack_float_prefix(n)); : get_msgpack_float_prefix(n));
write_number(n); write_number(n);
} }
#pragma GCC diagnostic pop
} }
public: public:

View File

@ -6278,6 +6278,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
*/ */
friend bool operator==(const_reference lhs, const_reference rhs) noexcept friend bool operator==(const_reference lhs, const_reference rhs) noexcept
{ {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
const auto lhs_type = lhs.type(); const auto lhs_type = lhs.type();
const auto rhs_type = rhs.type(); const auto rhs_type = rhs.type();
@ -6342,6 +6344,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
} }
return false; return false;
#pragma GCC diagnostic pop
} }
/*! /*!

View File

@ -14826,6 +14826,8 @@ class binary_writer
void write_compact_float(const number_float_t n, detail::input_format_t format) void write_compact_float(const number_float_t n, detail::input_format_t format)
{ {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) && if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) && static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
static_cast<double>(static_cast<float>(n)) == static_cast<double>(n)) static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
@ -14842,6 +14844,7 @@ class binary_writer
: get_msgpack_float_prefix(n)); : get_msgpack_float_prefix(n));
write_number(n); write_number(n);
} }
#pragma GCC diagnostic pop
} }
public: public:
@ -15982,6 +15985,8 @@ char* to_chars(char* first, const char* last, FloatType value)
*first++ = '-'; *first++ = '-';
} }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
if (value == 0) // +-0 if (value == 0) // +-0
{ {
*first++ = '0'; *first++ = '0';
@ -15990,6 +15995,7 @@ char* to_chars(char* first, const char* last, FloatType value)
*first++ = '0'; *first++ = '0';
return first; return first;
} }
#pragma GCC diagnostic pop
JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10); JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
@ -23364,6 +23370,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
*/ */
friend bool operator==(const_reference lhs, const_reference rhs) noexcept friend bool operator==(const_reference lhs, const_reference rhs) noexcept
{ {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
const auto lhs_type = lhs.type(); const auto lhs_type = lhs.type();
const auto rhs_type = rhs.type(); const auto rhs_type = rhs.type();
@ -23428,6 +23436,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
} }
return false; return false;
#pragma GCC diagnostic pop
} }
/*! /*!

View File

@ -28,7 +28,6 @@ SOFTWARE.
*/ */
#include "doctest_compatibility.h" #include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using nlohmann::json; using nlohmann::json;

View File

@ -28,7 +28,6 @@ SOFTWARE.
*/ */
#include "doctest_compatibility.h" #include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")
#define JSON_TESTS_PRIVATE #define JSON_TESTS_PRIVATE
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>

View File

@ -28,7 +28,6 @@ SOFTWARE.
*/ */
#include "doctest_compatibility.h" #include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using nlohmann::json; using nlohmann::json;

View File

@ -28,7 +28,6 @@ SOFTWARE.
*/ */
#include "doctest_compatibility.h" #include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using nlohmann::json; using nlohmann::json;

View File

@ -28,7 +28,6 @@ SOFTWARE.
*/ */
#include "doctest_compatibility.h" #include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")
// for some reason including this after the json header leads to linker errors with VS 2017... // for some reason including this after the json header leads to linker errors with VS 2017...
#include <locale> #include <locale>

View File

@ -28,7 +28,6 @@ SOFTWARE.
*/ */
#include "doctest_compatibility.h" #include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")
// for some reason including this after the json header leads to linker errors with VS 2017... // for some reason including this after the json header leads to linker errors with VS 2017...
#include <locale> #include <locale>