mirror of
https://github.com/nlohmann/json.git
synced 2026-09-06 08:17:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd0ca40ea6 |
@@ -71,7 +71,7 @@ class serializer
|
|||||||
, thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
|
, thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
|
||||||
, decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
|
, decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
|
||||||
, indent_char(ichar)
|
, indent_char(ichar)
|
||||||
, indent_string()
|
, indent_string(512, indent_char)
|
||||||
, error_handler(error_handler_)
|
, error_handler(error_handler_)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@@ -126,10 +126,6 @@ class serializer
|
|||||||
|
|
||||||
// variable to hold indentation for recursive calls
|
// variable to hold indentation for recursive calls
|
||||||
const auto new_indent = current_indent + indent_step;
|
const auto new_indent = current_indent + indent_step;
|
||||||
if (JSON_HEDLEY_UNLIKELY(indent_string.empty()))
|
|
||||||
{
|
|
||||||
indent_string.resize(512, indent_char);
|
|
||||||
}
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
|
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
|
||||||
{
|
{
|
||||||
indent_string.resize(indent_string.size() * 2, ' ');
|
indent_string.resize(indent_string.size() * 2, ' ');
|
||||||
@@ -203,10 +199,6 @@ class serializer
|
|||||||
|
|
||||||
// variable to hold indentation for recursive calls
|
// variable to hold indentation for recursive calls
|
||||||
const auto new_indent = current_indent + indent_step;
|
const auto new_indent = current_indent + indent_step;
|
||||||
if (JSON_HEDLEY_UNLIKELY(indent_string.empty()))
|
|
||||||
{
|
|
||||||
indent_string.resize(512, indent_char);
|
|
||||||
}
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
|
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
|
||||||
{
|
{
|
||||||
indent_string.resize(indent_string.size() * 2, ' ');
|
indent_string.resize(indent_string.size() * 2, ' ');
|
||||||
@@ -268,10 +260,6 @@ class serializer
|
|||||||
|
|
||||||
// variable to hold indentation for recursive calls
|
// variable to hold indentation for recursive calls
|
||||||
const auto new_indent = current_indent + indent_step;
|
const auto new_indent = current_indent + indent_step;
|
||||||
if (JSON_HEDLEY_UNLIKELY(indent_string.empty()))
|
|
||||||
{
|
|
||||||
indent_string.resize(512, indent_char);
|
|
||||||
}
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
|
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
|
||||||
{
|
{
|
||||||
indent_string.resize(indent_string.size() * 2, ' ');
|
indent_string.resize(indent_string.size() * 2, ' ');
|
||||||
@@ -1022,7 +1010,7 @@ class serializer
|
|||||||
|
|
||||||
/// the indentation character
|
/// the indentation character
|
||||||
const char indent_char;
|
const char indent_char;
|
||||||
/// the indentation string (lazily allocated on first use by a pretty-print branch)
|
/// the indentation string
|
||||||
string_t indent_string;
|
string_t indent_string;
|
||||||
|
|
||||||
/// error_handler how to react on decoding errors
|
/// error_handler how to react on decoding errors
|
||||||
|
|||||||
@@ -20150,7 +20150,7 @@ class serializer
|
|||||||
, thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
|
, thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
|
||||||
, decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
|
, decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
|
||||||
, indent_char(ichar)
|
, indent_char(ichar)
|
||||||
, indent_string()
|
, indent_string(512, indent_char)
|
||||||
, error_handler(error_handler_)
|
, error_handler(error_handler_)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@@ -20205,10 +20205,6 @@ class serializer
|
|||||||
|
|
||||||
// variable to hold indentation for recursive calls
|
// variable to hold indentation for recursive calls
|
||||||
const auto new_indent = current_indent + indent_step;
|
const auto new_indent = current_indent + indent_step;
|
||||||
if (JSON_HEDLEY_UNLIKELY(indent_string.empty()))
|
|
||||||
{
|
|
||||||
indent_string.resize(512, indent_char);
|
|
||||||
}
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
|
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
|
||||||
{
|
{
|
||||||
indent_string.resize(indent_string.size() * 2, ' ');
|
indent_string.resize(indent_string.size() * 2, ' ');
|
||||||
@@ -20282,10 +20278,6 @@ class serializer
|
|||||||
|
|
||||||
// variable to hold indentation for recursive calls
|
// variable to hold indentation for recursive calls
|
||||||
const auto new_indent = current_indent + indent_step;
|
const auto new_indent = current_indent + indent_step;
|
||||||
if (JSON_HEDLEY_UNLIKELY(indent_string.empty()))
|
|
||||||
{
|
|
||||||
indent_string.resize(512, indent_char);
|
|
||||||
}
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
|
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
|
||||||
{
|
{
|
||||||
indent_string.resize(indent_string.size() * 2, ' ');
|
indent_string.resize(indent_string.size() * 2, ' ');
|
||||||
@@ -20347,10 +20339,6 @@ class serializer
|
|||||||
|
|
||||||
// variable to hold indentation for recursive calls
|
// variable to hold indentation for recursive calls
|
||||||
const auto new_indent = current_indent + indent_step;
|
const auto new_indent = current_indent + indent_step;
|
||||||
if (JSON_HEDLEY_UNLIKELY(indent_string.empty()))
|
|
||||||
{
|
|
||||||
indent_string.resize(512, indent_char);
|
|
||||||
}
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
|
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
|
||||||
{
|
{
|
||||||
indent_string.resize(indent_string.size() * 2, ' ');
|
indent_string.resize(indent_string.size() * 2, ' ');
|
||||||
@@ -21101,7 +21089,7 @@ class serializer
|
|||||||
|
|
||||||
/// the indentation character
|
/// the indentation character
|
||||||
const char indent_char;
|
const char indent_char;
|
||||||
/// the indentation string (lazily allocated on first use by a pretty-print branch)
|
/// the indentation string
|
||||||
string_t indent_string;
|
string_t indent_string;
|
||||||
|
|
||||||
/// error_handler how to react on decoding errors
|
/// error_handler how to react on decoding errors
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
// __ _____ _____ _____
|
||||||
|
// __| | __| | | | JSON for Modern C++ (supporting code)
|
||||||
|
// | | |__ | | | | | | version 3.12.0
|
||||||
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||||
|
//
|
||||||
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
// This file contains the C++17-only part of unit-items.cpp (structured
|
||||||
|
// bindings support for json::items()). It is kept in a separate
|
||||||
|
// translation unit so the (much larger) unit-items.cpp does not need to
|
||||||
|
// be compiled a second time just for this one SECTION.
|
||||||
|
|
||||||
|
#include "doctest_compatibility.h"
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
using nlohmann::json;
|
||||||
|
|
||||||
|
#ifdef JSON_HAS_CPP_17
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
TEST_CASE("items()")
|
||||||
|
{
|
||||||
|
SECTION("object")
|
||||||
|
{
|
||||||
|
SECTION("structured bindings")
|
||||||
|
{
|
||||||
|
json j = { {"A", 1}, {"B", 2} };
|
||||||
|
|
||||||
|
std::map<std::string, int> m;
|
||||||
|
|
||||||
|
for (auto const&[key, value] : j.items())
|
||||||
|
{
|
||||||
|
m.emplace(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(j.get<decltype(m)>() == m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -862,22 +862,6 @@ TEST_CASE("items()")
|
|||||||
|
|
||||||
CHECK(counter == 3);
|
CHECK(counter == 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef JSON_HAS_CPP_17
|
|
||||||
SECTION("structured bindings")
|
|
||||||
{
|
|
||||||
json j = { {"A", 1}, {"B", 2} };
|
|
||||||
|
|
||||||
std::map<std::string, int> m;
|
|
||||||
|
|
||||||
for (auto const&[key, value] : j.items())
|
|
||||||
{
|
|
||||||
m.emplace(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
CHECK(j.get<decltype(m)>() == m);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("const object")
|
SECTION("const object")
|
||||||
|
|||||||
@@ -30,10 +30,6 @@ using nlohmann::json;
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include "make_test_data_available.hpp"
|
#include "make_test_data_available.hpp"
|
||||||
|
|
||||||
#ifdef JSON_HAS_CPP_17
|
|
||||||
#include <variant>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "fifo_map.hpp"
|
#include "fifo_map.hpp"
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
@@ -14,40 +14,6 @@ using nlohmann::json;
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <cstdlib>
|
|
||||||
#include <new>
|
|
||||||
|
|
||||||
namespace
|
|
||||||
{
|
|
||||||
// heap allocation counter used by the regression test for issue #5413
|
|
||||||
// (https://github.com/nlohmann/json/issues/5413); disabled (and thus a
|
|
||||||
// no-op besides the counting) unless explicitly toggled on
|
|
||||||
bool count_heap_allocations = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
||||||
std::size_t heap_allocations = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
void* operator new (std::size_t size) // NOLINT(cppcoreguidelines-owning-memory,misc-new-delete-overloads)
|
|
||||||
{
|
|
||||||
if (count_heap_allocations)
|
|
||||||
{
|
|
||||||
++heap_allocations;
|
|
||||||
}
|
|
||||||
if (void* ptr = std::malloc(size)) // NOLINT(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
|
|
||||||
{
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
throw std::bad_alloc(); // NOLINT(hicpp-exception-baseclass)
|
|
||||||
}
|
|
||||||
|
|
||||||
void operator delete (void* ptr) noexcept // NOLINT(cppcoreguidelines-owning-memory,misc-new-delete-overloads)
|
|
||||||
{
|
|
||||||
std::free(ptr); // NOLINT(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
|
|
||||||
}
|
|
||||||
|
|
||||||
void operator delete (void* ptr, std::size_t /*size*/) noexcept // NOLINT(cppcoreguidelines-owning-memory,misc-new-delete-overloads)
|
|
||||||
{
|
|
||||||
std::free(ptr); // NOLINT(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("serialization")
|
TEST_CASE("serialization")
|
||||||
{
|
{
|
||||||
@@ -416,76 +382,3 @@ TEST_CASE("dump for basic_json with long double number_float_t")
|
|||||||
check_same(100.0L, 100.0);
|
check_same(100.0L, 100.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("regression test for issue #5413 - lazily allocated indent_string")
|
|
||||||
{
|
|
||||||
// the serializer used to unconditionally allocate a 512-byte
|
|
||||||
// indent_string in its constructor, even though it is only ever read
|
|
||||||
// inside the pretty_print branches of dump(). This wasted a heap
|
|
||||||
// allocation (and its matching deallocation) on every single compact
|
|
||||||
// (i.e. non-pretty, the default) dump() call. indent_string is now
|
|
||||||
// allocated lazily, the first time a pretty-print branch actually
|
|
||||||
// needs it -- so a compact dump() must perform strictly fewer heap
|
|
||||||
// allocations than a pretty dump() of the same value.
|
|
||||||
const json j = {{"level", "info"}, {"msg", "hello world"}, {"id", 12345}};
|
|
||||||
|
|
||||||
// warm up anything unrelated to indentation (e.g., one-time locale
|
|
||||||
// lookups) that might otherwise allocate on first use regardless of
|
|
||||||
// pretty-printing, so it does not skew the counts measured below
|
|
||||||
const auto warmup = j.dump();
|
|
||||||
const auto warmup_pretty = j.dump(4);
|
|
||||||
CHECK(!warmup.empty());
|
|
||||||
CHECK(!warmup_pretty.empty());
|
|
||||||
|
|
||||||
SECTION("compact dump() has a stable, minimal allocation count")
|
|
||||||
{
|
|
||||||
count_heap_allocations = true;
|
|
||||||
|
|
||||||
heap_allocations = 0;
|
|
||||||
const auto compact1 = j.dump();
|
|
||||||
const auto allocs_compact1 = heap_allocations;
|
|
||||||
|
|
||||||
heap_allocations = 0;
|
|
||||||
const auto compact2 = j.dump(-1);
|
|
||||||
const auto allocs_compact2 = heap_allocations;
|
|
||||||
|
|
||||||
count_heap_allocations = false;
|
|
||||||
|
|
||||||
CHECK(compact1 == compact2);
|
|
||||||
// dump() and dump(-1) both take the compact code path and must
|
|
||||||
// never touch indent_string, so they allocate identically often
|
|
||||||
CHECK(allocs_compact1 == allocs_compact2);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("first pretty dump() allocates more than a compact dump()")
|
|
||||||
{
|
|
||||||
// use a tiny value whose compact ({"a":1}, 7 bytes) and pretty
|
|
||||||
// ({"a": 1} with 1-space indent, 11 bytes) serializations both stay
|
|
||||||
// well inside every common std::string small-string-optimization
|
|
||||||
// buffer (>= 15 bytes on libstdc++/MSVC STL, >= 22 on libc++), so
|
|
||||||
// building the result string itself causes no heap allocation
|
|
||||||
// either way -- isolating indent_string as the only thing that can
|
|
||||||
// possibly account for a difference in allocation count
|
|
||||||
const json tiny = {{"a", 1}};
|
|
||||||
|
|
||||||
count_heap_allocations = true;
|
|
||||||
|
|
||||||
heap_allocations = 0;
|
|
||||||
const auto compact = tiny.dump();
|
|
||||||
const auto allocs_compact = heap_allocations;
|
|
||||||
|
|
||||||
heap_allocations = 0;
|
|
||||||
const auto pretty = tiny.dump(1);
|
|
||||||
const auto allocs_pretty = heap_allocations;
|
|
||||||
|
|
||||||
count_heap_allocations = false;
|
|
||||||
|
|
||||||
CHECK(compact == "{\"a\":1}");
|
|
||||||
CHECK(pretty == "{\n \"a\": 1\n}");
|
|
||||||
// a fresh serializer is created per dump() call; the pretty branch
|
|
||||||
// lazily allocates indent_string on its first use, so it must
|
|
||||||
// allocate at least once more than the compact branch, which never
|
|
||||||
// touches indent_string at all
|
|
||||||
CHECK(allocs_pretty > allocs_compact);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user