mirror of
https://github.com/nlohmann/json.git
synced 2025-07-25 13:41:56 +03:00
Added Support for Structured Bindings
For further details, read https://github.com/nlohmann/json/issues/1388 and https://blog.tartanllama.xyz/structured-bindings/
This commit is contained in:
@ -3,21 +3,17 @@
|
||||
__| | __| | | | JSON for Modern C++ (test suite)
|
||||
| | |__ | | | | | | version 3.4.0
|
||||
|_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||
|
||||
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright (c) 2013-2018 Niels Lohmann <http://nlohmann.me>.
|
||||
|
||||
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
|
||||
@ -32,13 +28,20 @@ SOFTWARE.
|
||||
#include <nlohmann/json.hpp>
|
||||
using nlohmann::json;
|
||||
|
||||
#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
|
||||
#define JSON_HAS_CPP_17
|
||||
#define JSON_HAS_CPP_14
|
||||
#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
|
||||
#define JSON_HAS_CPP_14
|
||||
#endif
|
||||
|
||||
TEST_CASE("iterator_wrapper")
|
||||
{
|
||||
SECTION("object")
|
||||
{
|
||||
SECTION("value")
|
||||
{
|
||||
json j = {{"A", 1}, {"B", 2}};
|
||||
json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (auto i : json::iterator_wrapper(j))
|
||||
@ -71,7 +74,7 @@ TEST_CASE("iterator_wrapper")
|
||||
|
||||
SECTION("reference")
|
||||
{
|
||||
json j = {{"A", 1}, {"B", 2}};
|
||||
json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (auto& i : json::iterator_wrapper(j))
|
||||
@ -110,12 +113,12 @@ TEST_CASE("iterator_wrapper")
|
||||
CHECK(counter == 3);
|
||||
|
||||
// check if values where changed
|
||||
CHECK(j == json({{"A", 11}, {"B", 22}}));
|
||||
CHECK(j == json({ {"A", 11}, {"B", 22} }));
|
||||
}
|
||||
|
||||
SECTION("const value")
|
||||
{
|
||||
json j = {{"A", 1}, {"B", 2}};
|
||||
json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto i : json::iterator_wrapper(j))
|
||||
@ -148,7 +151,7 @@ TEST_CASE("iterator_wrapper")
|
||||
|
||||
SECTION("const reference")
|
||||
{
|
||||
json j = {{"A", 1}, {"B", 2}};
|
||||
json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto& i : json::iterator_wrapper(j))
|
||||
@ -184,7 +187,7 @@ TEST_CASE("iterator_wrapper")
|
||||
{
|
||||
SECTION("value")
|
||||
{
|
||||
const json j = {{"A", 1}, {"B", 2}};
|
||||
const json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (auto i : json::iterator_wrapper(j))
|
||||
@ -217,7 +220,7 @@ TEST_CASE("iterator_wrapper")
|
||||
|
||||
SECTION("reference")
|
||||
{
|
||||
const json j = {{"A", 1}, {"B", 2}};
|
||||
const json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (auto& i : json::iterator_wrapper(j))
|
||||
@ -250,7 +253,7 @@ TEST_CASE("iterator_wrapper")
|
||||
|
||||
SECTION("const value")
|
||||
{
|
||||
const json j = {{"A", 1}, {"B", 2}};
|
||||
const json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto i : json::iterator_wrapper(j))
|
||||
@ -283,7 +286,7 @@ TEST_CASE("iterator_wrapper")
|
||||
|
||||
SECTION("const reference")
|
||||
{
|
||||
const json j = {{"A", 1}, {"B", 2}};
|
||||
const json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto& i : json::iterator_wrapper(j))
|
||||
@ -319,7 +322,7 @@ TEST_CASE("iterator_wrapper")
|
||||
{
|
||||
SECTION("value")
|
||||
{
|
||||
json j = {"A", "B"};
|
||||
json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (auto i : json::iterator_wrapper(j))
|
||||
@ -352,7 +355,7 @@ TEST_CASE("iterator_wrapper")
|
||||
|
||||
SECTION("reference")
|
||||
{
|
||||
json j = {"A", "B"};
|
||||
json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (auto& i : json::iterator_wrapper(j))
|
||||
@ -391,12 +394,12 @@ TEST_CASE("iterator_wrapper")
|
||||
CHECK(counter == 3);
|
||||
|
||||
// check if values where changed
|
||||
CHECK(j == json({"AA", "BB"}));
|
||||
CHECK(j == json({ "AA", "BB" }));
|
||||
}
|
||||
|
||||
SECTION("const value")
|
||||
{
|
||||
json j = {"A", "B"};
|
||||
json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto i : json::iterator_wrapper(j))
|
||||
@ -429,7 +432,7 @@ TEST_CASE("iterator_wrapper")
|
||||
|
||||
SECTION("const reference")
|
||||
{
|
||||
json j = {"A", "B"};
|
||||
json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto& i : json::iterator_wrapper(j))
|
||||
@ -465,7 +468,7 @@ TEST_CASE("iterator_wrapper")
|
||||
{
|
||||
SECTION("value")
|
||||
{
|
||||
const json j = {"A", "B"};
|
||||
const json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (auto i : json::iterator_wrapper(j))
|
||||
@ -498,7 +501,7 @@ TEST_CASE("iterator_wrapper")
|
||||
|
||||
SECTION("reference")
|
||||
{
|
||||
const json j = {"A", "B"};
|
||||
const json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (auto& i : json::iterator_wrapper(j))
|
||||
@ -531,7 +534,7 @@ TEST_CASE("iterator_wrapper")
|
||||
|
||||
SECTION("const value")
|
||||
{
|
||||
const json j = {"A", "B"};
|
||||
const json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto i : json::iterator_wrapper(j))
|
||||
@ -564,7 +567,7 @@ TEST_CASE("iterator_wrapper")
|
||||
|
||||
SECTION("const reference")
|
||||
{
|
||||
const json j = {"A", "B"};
|
||||
const json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto& i : json::iterator_wrapper(j))
|
||||
@ -735,7 +738,7 @@ TEST_CASE("items()")
|
||||
{
|
||||
SECTION("value")
|
||||
{
|
||||
json j = {{"A", 1}, {"B", 2}};
|
||||
json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (auto i : j.items())
|
||||
@ -768,7 +771,7 @@ TEST_CASE("items()")
|
||||
|
||||
SECTION("reference")
|
||||
{
|
||||
json j = {{"A", 1}, {"B", 2}};
|
||||
json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (auto& i : j.items())
|
||||
@ -807,12 +810,12 @@ TEST_CASE("items()")
|
||||
CHECK(counter == 3);
|
||||
|
||||
// check if values where changed
|
||||
CHECK(j == json({{"A", 11}, {"B", 22}}));
|
||||
CHECK(j == json({ {"A", 11}, {"B", 22} }));
|
||||
}
|
||||
|
||||
SECTION("const value")
|
||||
{
|
||||
json j = {{"A", 1}, {"B", 2}};
|
||||
json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto i : j.items())
|
||||
@ -845,7 +848,7 @@ TEST_CASE("items()")
|
||||
|
||||
SECTION("const reference")
|
||||
{
|
||||
json j = {{"A", 1}, {"B", 2}};
|
||||
json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto& i : j.items())
|
||||
@ -875,13 +878,29 @@ TEST_CASE("items()")
|
||||
|
||||
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("value")
|
||||
{
|
||||
const json j = {{"A", 1}, {"B", 2}};
|
||||
const json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (auto i : j.items())
|
||||
@ -914,7 +933,7 @@ TEST_CASE("items()")
|
||||
|
||||
SECTION("reference")
|
||||
{
|
||||
const json j = {{"A", 1}, {"B", 2}};
|
||||
const json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (auto& i : j.items())
|
||||
@ -947,7 +966,7 @@ TEST_CASE("items()")
|
||||
|
||||
SECTION("const value")
|
||||
{
|
||||
const json j = {{"A", 1}, {"B", 2}};
|
||||
const json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto i : j.items())
|
||||
@ -980,7 +999,7 @@ TEST_CASE("items()")
|
||||
|
||||
SECTION("const reference")
|
||||
{
|
||||
const json j = {{"A", 1}, {"B", 2}};
|
||||
const json j = { {"A", 1}, {"B", 2} };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto& i : j.items())
|
||||
@ -1016,7 +1035,7 @@ TEST_CASE("items()")
|
||||
{
|
||||
SECTION("value")
|
||||
{
|
||||
json j = {"A", "B"};
|
||||
json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (auto i : j.items())
|
||||
@ -1049,7 +1068,7 @@ TEST_CASE("items()")
|
||||
|
||||
SECTION("reference")
|
||||
{
|
||||
json j = {"A", "B"};
|
||||
json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (auto& i : j.items())
|
||||
@ -1088,12 +1107,12 @@ TEST_CASE("items()")
|
||||
CHECK(counter == 3);
|
||||
|
||||
// check if values where changed
|
||||
CHECK(j == json({"AA", "BB"}));
|
||||
CHECK(j == json({ "AA", "BB" }));
|
||||
}
|
||||
|
||||
SECTION("const value")
|
||||
{
|
||||
json j = {"A", "B"};
|
||||
json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto i : j.items())
|
||||
@ -1126,7 +1145,7 @@ TEST_CASE("items()")
|
||||
|
||||
SECTION("const reference")
|
||||
{
|
||||
json j = {"A", "B"};
|
||||
json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto& i : j.items())
|
||||
@ -1162,7 +1181,7 @@ TEST_CASE("items()")
|
||||
{
|
||||
SECTION("value")
|
||||
{
|
||||
const json j = {"A", "B"};
|
||||
const json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (auto i : j.items())
|
||||
@ -1195,7 +1214,7 @@ TEST_CASE("items()")
|
||||
|
||||
SECTION("reference")
|
||||
{
|
||||
const json j = {"A", "B"};
|
||||
const json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (auto& i : j.items())
|
||||
@ -1228,7 +1247,7 @@ TEST_CASE("items()")
|
||||
|
||||
SECTION("const value")
|
||||
{
|
||||
const json j = {"A", "B"};
|
||||
const json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto i : j.items())
|
||||
@ -1261,7 +1280,7 @@ TEST_CASE("items()")
|
||||
|
||||
SECTION("const reference")
|
||||
{
|
||||
const json j = {"A", "B"};
|
||||
const json j = { "A", "B" };
|
||||
int counter = 1;
|
||||
|
||||
for (const auto& i : j.items())
|
||||
@ -1424,4 +1443,4 @@ TEST_CASE("items()")
|
||||
CHECK(counter == 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user