Skip to content

Commit

Permalink
json_pointer_arg
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 18, 2024
1 parent c212698 commit 88de90a
Show file tree
Hide file tree
Showing 7 changed files with 576 additions and 175 deletions.
508 changes: 354 additions & 154 deletions include/jsoncons/basic_json.hpp

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions include/jsoncons/json_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ namespace jsoncons {
float64 = 5, // 0101
half_float = 6, // 0110
short_str = 7, // 0111
const_json_pointer = 8, // 1000
json_const_pointer = 8, // 1000
json_pointer = 9, // 1001
byte_str = 12, // 1100
object = 13, // 1101
array = 14, // 1110
Expand Down Expand Up @@ -141,7 +142,7 @@ namespace jsoncons {
static constexpr const CharT* array_value = JSONCONS_CSTRING_CONSTANT(CharT, "array");
static constexpr const CharT* empty_object_value = JSONCONS_CSTRING_CONSTANT(CharT, "empty_object");
static constexpr const CharT* object_value = JSONCONS_CSTRING_CONSTANT(CharT, "object");
static constexpr const CharT* const_json_pointer = JSONCONS_CSTRING_CONSTANT(CharT, "const_json_pointer");
static constexpr const CharT* json_const_pointer = JSONCONS_CSTRING_CONSTANT(CharT, "json_const_pointer");

switch (storage)
{
Expand Down Expand Up @@ -205,9 +206,9 @@ namespace jsoncons {
os << object_value;
break;
}
case json_storage_kind::const_json_pointer:
case json_storage_kind::json_const_pointer:
{
os << const_json_pointer;
os << json_const_pointer;
break;
}
}
Expand Down
7 changes: 7 additions & 0 deletions include/jsoncons/tag_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ struct json_const_pointer_arg_t
};

constexpr json_const_pointer_arg_t json_const_pointer_arg{};

struct json_pointer_arg_t
{
explicit json_pointer_arg_t() = default;
};

constexpr json_pointer_arg_t json_pointer_arg{};

enum class semantic_tag : uint8_t
{
Expand Down
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ add_executable(unit_tests
corelib/src/json_bitset_traits_tests.cpp
corelib/src/json_checker_tests.cpp
corelib/src/json_comparator_tests.cpp
corelib/src/json_const_pointer_tests.cpp
corelib/src/json_const_pointer_arg_tests.cpp
corelib/src/json_pointer_arg_tests.cpp
corelib/src/json_assignment_tests.cpp
corelib/src/json_constructor_tests.cpp
corelib/src/json_cursor_tests.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

using namespace jsoncons;

TEST_CASE("const_json_pointer array tests")
TEST_CASE("json_const_pointer array tests")
{
json j = json::parse(R"( ["one", "two", "three"] )");

Expand All @@ -39,23 +39,23 @@ TEST_CASE("const_json_pointer array tests")
SECTION("copy")
{
json v(json_const_pointer_arg, &j);
CHECK(v.storage_kind() == json_storage_kind::const_json_pointer);
CHECK(v.storage_kind() == json_storage_kind::json_const_pointer);

json j2(v);
CHECK(j2.storage_kind() == json_storage_kind::const_json_pointer);
CHECK(j2.storage_kind() == json_storage_kind::json_const_pointer);
}
SECTION("assignment")
{
json v(json_const_pointer_arg, &j);
CHECK(v.storage_kind() == json_storage_kind::const_json_pointer);
CHECK(v.storage_kind() == json_storage_kind::json_const_pointer);

json j2;
j2 = v;
CHECK(j2.storage_kind() == json_storage_kind::const_json_pointer);
CHECK(j2.storage_kind() == json_storage_kind::json_const_pointer);
}
}

TEST_CASE("const_json_pointer object tests")
TEST_CASE("json_const_pointer object tests")
{
json j = json::parse(R"( {"one" : 1, "two" : 2, "three" : 3} )");

Expand Down Expand Up @@ -90,7 +90,7 @@ TEST_CASE("const_json_pointer object tests")
}
}

TEST_CASE("const_json_pointer string tests")
TEST_CASE("json_const_pointer string tests")
{
json j = json("Hello World");

Expand All @@ -104,7 +104,7 @@ TEST_CASE("const_json_pointer string tests")
}
}

TEST_CASE("const_json_pointer byte_string tests")
TEST_CASE("json_const_pointer byte_string tests")
{
std::string data = "abcdefghijk";
json j(byte_string_arg, data);
Expand All @@ -117,7 +117,7 @@ TEST_CASE("const_json_pointer byte_string tests")
}
}

TEST_CASE("const_json_pointer bool tests")
TEST_CASE("json_const_pointer bool tests")
{
json tru(true);
json fal(false);
Expand All @@ -136,7 +136,7 @@ TEST_CASE("const_json_pointer bool tests")
}
}

TEST_CASE("const_json_pointer int64 tests")
TEST_CASE("json_const_pointer int64 tests")
{
json j(-100);

Expand All @@ -148,7 +148,7 @@ TEST_CASE("const_json_pointer int64 tests")
}
}

TEST_CASE("const_json_pointer uint64 tests")
TEST_CASE("json_const_pointer uint64 tests")
{
json j(100);

Expand All @@ -160,7 +160,7 @@ TEST_CASE("const_json_pointer uint64 tests")
}
}

TEST_CASE("const_json_pointer half tests")
TEST_CASE("json_const_pointer half tests")
{
json j(half_arg, 100);

Expand All @@ -172,7 +172,7 @@ TEST_CASE("const_json_pointer half tests")
}
}

TEST_CASE("const_json_pointer double tests")
TEST_CASE("json_const_pointer double tests")
{
json j(123.456);

Expand Down Expand Up @@ -220,7 +220,7 @@ namespace {
}
}

TEST_CASE("const_json_pointer identifier tests")
TEST_CASE("json_const_pointer identifier tests")
{
json source = json::parse(R"(
{"reservations": [{
Expand Down
190 changes: 190 additions & 0 deletions test/corelib/src/json_pointer_arg_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Copyright 2013-2024 Daniel Parker
// Distributed under Boost license

#include <jsoncons/json.hpp>
#include <jsoncons/json_encoder.hpp>
#include <catch/catch.hpp>
#include <sstream>
#include <vector>
#include <utility>
#include <ctime>
#include <map>
#include <iterator>

using namespace jsoncons;

TEST_CASE("json_pointer array tests")
{
json j = json::parse(R"( ["one", "two", "three"] )");

SECTION("size()")
{
json v(json_pointer_arg, &j);
REQUIRE(v.is_array());
CHECK(v.size() == 3);
CHECK_FALSE(v.empty());
}
SECTION("at()")
{
json v(json_pointer_arg, &j);
REQUIRE(v.is_array());
REQUIRE_NOTHROW(v.at(1));
}
SECTION("copy")
{
json v(json_pointer_arg, &j);
CHECK(v.storage_kind() == json_storage_kind::json_pointer);

json j2(v);
CHECK(j2.storage_kind() == json_storage_kind::json_pointer);
}
SECTION("assignment")
{
json v(json_pointer_arg, &j);
CHECK(v.storage_kind() == json_storage_kind::json_pointer);

json j2;
j2 = v;
CHECK(j2.storage_kind() == json_storage_kind::json_pointer);
}
SECTION("push_back")
{
json expected = json::parse(R"( ["one", "two", "three", "four"] )");

json v(json_pointer_arg, &j);
CHECK(v.storage_kind() == json_storage_kind::json_pointer);
j.push_back("four");

CHECK(expected == v);
}
SECTION("emplace_back")
{
json expected = json::parse(R"( ["one", "two", "three", "four"] )");

json v(json_pointer_arg, &j);
CHECK(v.storage_kind() == json_storage_kind::json_pointer);
j.emplace_back("four");

CHECK(expected == v);
}
}

TEST_CASE("json_pointer object tests")
{
json j = json::parse(R"( {"one" : 1, "two" : 2, "three" : 3} )");

SECTION("size()")
{
json v(json_pointer_arg, &j);
REQUIRE(v.is_object());
CHECK(v.size() == 3);
CHECK_FALSE(v.empty());
}
SECTION("at()")
{
json v(json_pointer_arg, &j);
REQUIRE(v.is_object());
REQUIRE_NOTHROW(v.at("two"));
CHECK(v.contains("two"));
CHECK(v.count("two") == 1);

CHECK(v.get_value_or<int>("three", 0) == 3);
CHECK(v.get_value_or<int>("four", 4) == 4);
}
}

TEST_CASE("json_pointer string tests")
{
json j = json("Hello World");

SECTION("is_string()")
{
json v(json_pointer_arg, &j);
REQUIRE(v.is_string());
REQUIRE(v.is_string_view());

CHECK(v.as<std::string>() == j.as<std::string>());
}
}

TEST_CASE("json_pointer byte_string tests")
{
std::string data = "abcdefghijk";
json j(byte_string_arg, data);

SECTION("is_byte_string()")
{
json v(json_pointer_arg, &j);
REQUIRE(v.is_byte_string());
REQUIRE(v.is_byte_string_view());
}
}

TEST_CASE("json_pointer bool tests")
{
json tru(true);
json fal(false);

SECTION("true")
{
json v(json_pointer_arg, &tru);
REQUIRE(v.is_bool());
CHECK(v.as_bool());
}
SECTION("false")
{
json v(json_pointer_arg, &fal);
REQUIRE(v.is_bool());
CHECK_FALSE(v.as_bool());
}
}

TEST_CASE("json_pointer int64 tests")
{
json j(-100);

SECTION("is_int64()")
{
json v(json_pointer_arg, &j);
REQUIRE(v.is_int64());
CHECK(v.as<int64_t>() == -100);
}
}

TEST_CASE("json_pointer uint64 tests")
{
json j(100);

SECTION("is_uint64()")
{
json v(json_pointer_arg, &j);
REQUIRE(v.is_uint64());
CHECK(v.as<uint64_t>() == 100);
}
}

TEST_CASE("json_pointer half tests")
{
json j(half_arg, 100);

SECTION("is_half()")
{
json v(json_pointer_arg, &j);
REQUIRE(v.is_half());
CHECK(v.as<uint16_t>() == 100);
}
}

TEST_CASE("json_pointer double tests")
{
json j(123.456);

SECTION("is_double()")
{
json v(json_pointer_arg, &j);
REQUIRE(v.is_double());

CHECK(v.as_double() == 123.456);
}
}

6 changes: 4 additions & 2 deletions test/corelib/src/json_storage_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ TEST_CASE("test json_storage_kind")
CHECK(is_trivial_storage(json_storage_kind::half_float));
CHECK(is_trivial_storage(json_storage_kind::short_str));
CHECK(is_trivial_storage(json_storage_kind::empty_object));
CHECK(is_trivial_storage(json_storage_kind::const_json_pointer));
CHECK(is_trivial_storage(json_storage_kind::json_const_pointer));
CHECK(is_trivial_storage(json_storage_kind::json_pointer));
CHECK_FALSE(is_trivial_storage(json_storage_kind::long_str));
CHECK_FALSE(is_trivial_storage(json_storage_kind::byte_str));
CHECK_FALSE(is_trivial_storage(json_storage_kind::array));
Expand All @@ -35,7 +36,8 @@ TEST_CASE("test json_storage_kind")
CHECK_FALSE(is_string_storage(json_storage_kind::half_float));
CHECK(is_string_storage(json_storage_kind::short_str));
CHECK_FALSE(is_string_storage(json_storage_kind::empty_object));
CHECK_FALSE(is_string_storage(json_storage_kind::const_json_pointer));
CHECK_FALSE(is_string_storage(json_storage_kind::json_const_pointer));
CHECK_FALSE(is_string_storage(json_storage_kind::json_pointer));
CHECK(is_string_storage(json_storage_kind::long_str));
CHECK_FALSE(is_string_storage(json_storage_kind::byte_str));
CHECK_FALSE(is_string_storage(json_storage_kind::array));
Expand Down

0 comments on commit 88de90a

Please sign in to comment.