Skip to content

Commit

Permalink
💚 added regression tests for #972 and #977
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Mar 6, 2018
1 parent 6203061 commit 476b2e0
Show file tree
Hide file tree
Showing 8 changed files with 643 additions and 15 deletions.
11 changes: 5 additions & 6 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,8 @@ class basic_json
@brief create a JSON value from an existing one
This is a constructor for existing @ref basic_json types.
It does not hijack copy/move constructors, since the parameter has different template arguments than the current ones.
It does not hijack copy/move constructors, since the parameter has different
template arguments than the current ones.
The constructor tries to convert the internal @ref m_value of the parameter.
Expand Down Expand Up @@ -2489,7 +2490,8 @@ class basic_json
/*!
@brief get special-case overload
This overloads converts the current @ref basic_json in a different @ref basic_json type
This overloads converts the current @ref basic_json in a different
@ref basic_json type
@tparam BasicJsonType == @ref basic_json
Expand All @@ -2502,15 +2504,12 @@ class basic_json
*/
template<typename BasicJsonType, detail::enable_if_t<
not std::is_same<BasicJsonType, basic_json>::value and
detail::is_basic_json<BasicJsonType>::value
,
int> = 0>
detail::is_basic_json<BasicJsonType>::value, int> = 0>
BasicJsonType get() const
{
return *this;
}


/*!
@brief get a value (explicit)
Expand Down
11 changes: 5 additions & 6 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10845,7 +10845,8 @@ class basic_json
@brief create a JSON value from an existing one

This is a constructor for existing @ref basic_json types.
It does not hijack copy/move constructors, since the parameter has different template arguments than the current ones.
It does not hijack copy/move constructors, since the parameter has different
template arguments than the current ones.

The constructor tries to convert the internal @ref m_value of the parameter.

Expand Down Expand Up @@ -12087,7 +12088,8 @@ class basic_json
/*!
@brief get special-case overload

This overloads converts the current @ref basic_json in a different @ref basic_json type
This overloads converts the current @ref basic_json in a different
@ref basic_json type

@tparam BasicJsonType == @ref basic_json

Expand All @@ -12100,15 +12102,12 @@ class basic_json
*/
template<typename BasicJsonType, detail::enable_if_t<
not std::is_same<BasicJsonType, basic_json>::value and
detail::is_basic_json<BasicJsonType>::value
,
int> = 0>
detail::is_basic_json<BasicJsonType>::value, int> = 0>
BasicJsonType get() const
{
return *this;
}


/*!
@brief get a value (explicit)

Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ foreach(file ${files})

target_compile_definitions(${testcase} PRIVATE CATCH_CONFIG_FAST_COMPILE)
target_include_directories(${testcase} PRIVATE "thirdparty/catch")
target_include_directories(${testcase} PRIVATE "thirdparty/fifo_map")
target_include_directories(${testcase} PRIVATE ${NLOHMANN_JSON_INCLUDE_BUILD_DIR})
target_link_libraries(${testcase} ${NLOHMANN_JSON_TARGET_NAME})

Expand Down
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# additional flags
CXXFLAGS += -std=c++11 -Wall -Wextra -pedantic -Wcast-align -Wcast-qual -Wno-ctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch -Wundef -Wno-unused -Wnon-virtual-dtor -Wreorder -Wdeprecated -Wno-float-equal
CPPFLAGS += -I ../single_include -I . -I thirdparty/catch -DCATCH_CONFIG_FAST_COMPILE
CPPFLAGS += -I ../single_include -I . -I thirdparty/catch -I thirdparty/fifo_map -DCATCH_CONFIG_FAST_COMPILE

SOURCES = src/unit.cpp \
src/unit-algorithms.cpp \
Expand Down
4 changes: 2 additions & 2 deletions test/src/unit-inspection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ TEST_CASE("object inspection")
SECTION("round trips")
{
for (const auto& s :
{"3.141592653589793", "1000000000000000010E5"
})
{"3.141592653589793", "1000000000000000010E5"
})
{
json j1 = json::parse(s);
std::string s1 = j1.dump();
Expand Down
78 changes: 78 additions & 0 deletions test/src/unit-regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,72 @@ SOFTWARE.
#include <nlohmann/json.hpp>
using nlohmann::json;

#include "fifo_map.hpp"

#include <fstream>
#include <list>
#include <cstdio>

/////////////////////////////////////////////////////////////////////
// for #972
/////////////////////////////////////////////////////////////////////

template<class K, class V, class dummy_compare, class A>
using my_workaround_fifo_map = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
using my_json = nlohmann::basic_json<my_workaround_fifo_map>;

/////////////////////////////////////////////////////////////////////
// for #977
/////////////////////////////////////////////////////////////////////

namespace ns
{
struct foo
{
int x;
};

template <typename, typename SFINAE = void>
struct foo_serializer;

template<typename T>
struct foo_serializer<T, typename std::enable_if<std::is_same<foo, T>::value>::type>
{
template <typename BasicJsonType>
static void to_json(BasicJsonType& j, const T& value)
{
j = BasicJsonType{{"x", value.x}};
}
template <typename BasicJsonType>
static void from_json(const BasicJsonType& j, T& value) // !!!
{
nlohmann::from_json(j.at("x"), value.x);
}
};

template<typename T>
struct foo_serializer < T, typename std::enable_if < !std::is_same<foo, T>::value >::type >
{
template <typename BasicJsonType>
static void to_json(BasicJsonType& j, const T& value) noexcept
{
::nlohmann::to_json(j, value);
}
template <typename BasicJsonType>
static void from_json(const BasicJsonType& j, T& value) //!!!
{
::nlohmann::from_json(j, value);
}
};
}

using foo_json = nlohmann::basic_json<std::map, std::vector, std::string, bool, std::int64_t,
std::uint64_t, double, std::allocator, ns::foo_serializer>;

/////////////////////////////////////////////////////////////////////
// for #805
/////////////////////////////////////////////////////////////////////

namespace
{
struct nocopy
Expand Down Expand Up @@ -1436,4 +1498,20 @@ TEST_CASE("regression tests")
//CHECK_THROWS_WITH(json::from_ubjson(v_ubjson),
// "[json.exception.out_of_range.408] excessive object size: 8658170730974374167");
}

SECTION("issue #972 - Segmentation fault on G++ when trying to assign json string literal to custom json type")
{
my_json foo = R"([1, 2, 3])"_json;
}

SECTION("issue #977 - Assigning between different json types")
{
foo_json lj = ns::foo{3};
ns::foo ff = lj;
CHECK(lj.is_object());
CHECK(lj.size() == 1);
CHECK(lj["x"] == 3);
CHECK(ff.x == 3);
nlohmann::json nj = lj; // This line works as expected
}
}
21 changes: 21 additions & 0 deletions test/thirdparty/fifo_map/LICENSE.MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2015-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.
Loading

0 comments on commit 476b2e0

Please sign in to comment.