Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
18 changes: 17 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,20 @@ libjson.a

Testing

.idea
.idea

json.dir

json.lib

*.sln

*.vcxproj

*.exe*

*.pdb

*.ilk

Win32
19 changes: 16 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
cmake_minimum_required(VERSION 2.8.4)
project(json)

# Enable C++11 and set flags for coverage testing
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -O0 --coverage -fprofile-arcs -ftest-coverage")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_compile_options( -Wall -Wextra -Werror )
add_compile_options( -std=c++11 )
add_compile_options( -g -O0 -fprofile-arcs -ftest-coverage )
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_compile_options( -Wall -Wextra -Werror )
add_compile_options( -std=c++11 )
add_compile_options( -g -O0 --coverage -fprofile-arcs -ftest-coverage )
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_compile_options( /W4 /WX )
add_compile_options( /wd4566 ) # character represented by universal-character cannot be represented in the current code page
add_compile_options( /wd4189 ) # local variable is initialized but not referenced
add_definitions( /DUNICODE /D_UNICODE ) # it is a new millenium
endif()


# Make everything public for testing purposes
add_definitions(-Dprivate=public)
add_definitions(-DJSON_TEST)

# If not specified, use Debug as build type (necessary for coverage testing)
if( NOT CMAKE_BUILD_TYPE )
Expand Down
16 changes: 16 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 0.0.{build}

branches:
# whitelist
only:
- master
- appveyor

install:
- git submodule -q update --init

before_build:
- cmake -G"Visual Studio 12" -T v120 .\

build:
project: json.sln
6 changes: 5 additions & 1 deletion src/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,7 @@ std::string json::parser::codePointToUTF8(unsigned int codePoint) const
{
// Can't be tested without direct access to this private method.
std::string errorMessage = "Invalid codePoint: ";
errorMessage += codePoint;
errorMessage += std::to_string(codePoint);
error(errorMessage);
}
}
Expand Down Expand Up @@ -2558,6 +2558,8 @@ void json::parser::expect(const char c)

}

#ifdef JSON_USE_LITERALS

/*!
This operator implements a user-defined string literal for JSON objects. It can
be used by adding \p "_json" to a string literal and returns a JSON object if
Expand All @@ -2570,3 +2572,5 @@ nlohmann::json operator "" _json(const char* s, std::size_t)
{
return nlohmann::json::parse(s);
}

#endif
35 changes: 34 additions & 1 deletion src/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@
#include <vector> // std::vector
#include <iterator> // std::iterator

#define JSON_NO_RETURN __attribute__((noreturn))
#define JSON_USE_LITERALS

#if defined(_MSC_VER)

#include <cstdint>

#if _MSC_VER < 1900
#define noexcept throw()
#define u8
#undef JSON_USE_LITERALS
#endif

#undef JSON_NO_RETURN
#define JSON_NO_RETURN __declspec(noreturn)

#define or ||
#define and &&
#define not !
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not #include <ciso464> which contains all defines?

http://www.cplusplus.com/reference/ciso646/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried and VS 2013 & 2014 do not appear to implement that header.


#endif

#ifdef JSON_TEST
// Make everything public for testing purposes
#define private public
#endif

namespace nlohmann
{

Expand Down Expand Up @@ -418,7 +445,7 @@ class json
/// read the next character, stripping whitespace
bool next();
/// raise an exception with an error message
inline void error(const std::string&) const __attribute__((noreturn));
inline JSON_NO_RETURN void error(const std::string&) const;
/// parse a quoted string
inline std::string parseString();
/// transforms a unicode codepoint to it's UTF-8 presentation
Expand Down Expand Up @@ -448,5 +475,11 @@ class json

}

#ifdef JSON_USE_LITERALS
/// user-defined literal operator to create JSON objects from strings
nlohmann::json operator "" _json(const char*, std::size_t);
#endif

#ifdef JSON_TEST
#undef private
#endif
45 changes: 31 additions & 14 deletions test/json_unit.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
#define CATCH_CONFIG_MAIN
#define CATCH_CONFIG_CPP11_NULLPTR
#include "catch.hpp"

#include "json.h"

using json = nlohmann::json;

#if defined(_MSC_VER)
#define SKIP_FOR_VS(x)

#if _MSC_VER < 1900
#define LIST_INIT_T(...) json::list_init_t(__VA_ARGS__)
#else
#define LIST_INIT_T(...) __VA_ARGS__
#endif

#else
#define SKIP_FOR_VS(x) x
#define LIST_INIT_T(...) __VA_ARGS__
#endif

TEST_CASE("array")
{
SECTION("Basics")
Expand Down Expand Up @@ -150,7 +165,7 @@ TEST_CASE("array")
json nonarray = 1;
CHECK_THROWS_AS(nonarray.at(0), std::domain_error);
CHECK_THROWS_AS(const int i = nonarray[0], std::domain_error);
CHECK_NOTHROW(j[21]);
SKIP_FOR_VS(CHECK_NOTHROW(j[21]));
CHECK_THROWS_AS(const int i = j.at(21), std::out_of_range);
CHECK_THROWS_AS(nonarray[0] = 10, std::domain_error);
// the next test is remove due to undefined behavior
Expand All @@ -162,7 +177,7 @@ TEST_CASE("array")
const json j_const = j;
CHECK_THROWS_AS(nonarray_const.at(0), std::domain_error);
CHECK_THROWS_AS(const int i = nonarray_const[0], std::domain_error);
CHECK_NOTHROW(j_const[21]);
SKIP_FOR_VS(CHECK_NOTHROW(j_const[21]));
CHECK_THROWS_AS(const int i = j.at(21), std::out_of_range);

{
Expand All @@ -175,11 +190,11 @@ TEST_CASE("array")
}

const json k = j;
CHECK_NOTHROW(k[21]);
SKIP_FOR_VS(CHECK_NOTHROW(k[21]));
CHECK_THROWS_AS(const int i = k.at(21), std::out_of_range);

// add initializer list
j.push_back({"a", "b", "c"});
j.push_back(LIST_INIT_T({"a", "b", "c"}));
CHECK (j.size() == 24);

// clear()
Expand Down Expand Up @@ -495,14 +510,14 @@ TEST_CASE("object")
// add initializer list (of pairs)
{
json je;
je.push_back({ {"one", 1}, {"two", false}, {"three", {1, 2, 3}} });
je.push_back(LIST_INIT_T({ {"one", 1}, {"two", false}, {"three", {1, 2, 3}} }));
CHECK(je["one"].get<int>() == 1);
CHECK(je["two"].get<bool>() == false);
CHECK(je["three"].size() == 3);
}
{
json je;
je += { {"one", 1}, {"two", false}, {"three", {1, 2, 3}} };
je += LIST_INIT_T({ {"one", 1}, {"two", false}, {"three", {1, 2, 3}} });
CHECK(je["one"].get<int>() == 1);
CHECK(je["two"].get<bool>() == false);
CHECK(je["three"].size() == 3);
Expand Down Expand Up @@ -864,7 +879,7 @@ TEST_CASE("string")
SECTION("Dumping")
{
CHECK(json("\"").dump(0) == "\"\\\"\"");
CHECK(json("\\").dump(0) == "\"\\\\\"");
SKIP_FOR_VS(CHECK(json("\\").dump(0) == "\"\\\\\""));
CHECK(json("\n").dump(0) == "\"\\n\"");
CHECK(json("\t").dump(0) == "\"\\t\"");
CHECK(json("\b").dump(0) == "\"\\b\"");
Expand Down Expand Up @@ -1720,17 +1735,17 @@ TEST_CASE("Parser")
// normal forward slash in ASCII range
CHECK(json::parse("\"\\u002F\"") == json("/"));
CHECK(json::parse("\"\\u002f\"") == json("/"));

// german a umlaut
CHECK(json::parse("\"\\u00E4\"") == json(u8"\u00E4"));
CHECK(json::parse("\"\\u00e4\"") == json(u8"\u00E4"));
SKIP_FOR_VS(CHECK(json::parse("\"\\u00E4\"") == json(u8"\u00E4")));
SKIP_FOR_VS(CHECK(json::parse("\"\\u00e4\"") == json(u8"\u00E4")));
// weird d
CHECK(json::parse("\"\\u0111\"") == json(u8"\u0111"));
SKIP_FOR_VS(CHECK(json::parse("\"\\u0111\"") == json(u8"\u0111")));
// unicode arrow left
CHECK(json::parse("\"\\u2190\"") == json(u8"\u2190"));
SKIP_FOR_VS(CHECK(json::parse("\"\\u2190\"") == json(u8"\u2190")));
// pleasing osiris by testing hieroglyph support
CHECK(json::parse("\"\\uD80C\\uDC60\"") == json(u8"\U00013060"));
CHECK(json::parse("\"\\ud80C\\udc60\"") == json(u8"\U00013060"));

SKIP_FOR_VS(CHECK(json::parse("\"\\uD80C\\uDC60\"") == json(u8"\U00013060")));
SKIP_FOR_VS(CHECK(json::parse("\"\\ud80C\\udc60\"") == json(u8"\U00013060")));

// no hex numbers behind the \u
CHECK_THROWS_AS(json::parse("\"\\uD80v\""), std::invalid_argument);
Expand Down Expand Up @@ -1900,6 +1915,7 @@ TEST_CASE("Parser")
CHECK(j["foo"].size() == 3);
}

#ifdef JSON_USE_LITERALS
SECTION("user-defined string literal operator")
{
auto j1 = "[1,2,3]"_json;
Expand Down Expand Up @@ -1928,6 +1944,7 @@ TEST_CASE("Parser")
CHECK(j23.dump(4) ==
"{\n \"a\": null,\n \"b\": true,\n \"c\": [\n 1,\n 2,\n 3\n ],\n \"d\": {\n \"a\": 0\n }\n}");
}
#endif

SECTION("Errors")
{
Expand Down