Skip to content

Commit

Permalink
chore: update jsoncons dependency
Browse files Browse the repository at this point in the history
Signed-off-by: iko1 <[email protected]>
  • Loading branch information
iko1 committed Jul 8, 2023
1 parent 47626a0 commit 49ab388
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ set(REFLEX "${THIRD_PARTY_LIB_DIR}/reflex/bin/reflex")

add_third_party(
jsoncons
URL https://github.com/danielaparker/jsoncons/archive/refs/tags/v0.170.2.tar.gz
CMAKE_PASS_FLAGS "-DJSONCONS_BUILD_TESTS=OFF"
URL https://github.com/danielaparker/jsoncons/archive/refs/tags/v0.171.0.tar.gz
CMAKE_PASS_FLAGS "-DJSONCONS_BUILD_TESTS=OFF -DJSONCONS_HAS_POLYMORPHIC_ALLOCATOR=ON"
LIB "none"
)

Expand Down
40 changes: 16 additions & 24 deletions src/core/json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
#include <memory_resource>

#include "base/gtest.h"
#include "base/logging.h"

#include <memory_resource>

namespace dfly {
using namespace std;
using namespace jsoncons;
using namespace jsoncons::literals;

Expand All @@ -22,7 +20,7 @@ class JsonTest : public ::testing::Test {
};

TEST_F(JsonTest, Basic) {
string data = R"(
std::string data = R"(
{
"application": "hiking",
"reputons": [
Expand Down Expand Up @@ -71,7 +69,7 @@ TEST_F(JsonTest, Errors) {
json_decoder<json> decoder;
basic_json_parser<char> parser(basic_json_decode_options<char>{}, cb);

string_view input{"\000bla"};
std::string_view input{"\000bla"};
parser.update(input.data(), input.size());
parser.parse_some(decoder);
parser.finish_parse(decoder);
Expand Down Expand Up @@ -103,29 +101,16 @@ TEST_F(JsonTest, Delete) {
EXPECT_EQ(R"({"c":{"a":1, "b":2}, "d":{"b":2, "c":3}, "e": [1,2]})"_json, j1);
}

struct pmr_sorted_policy : public sorted_policy {
template<class T, class Allocator>
using vector = std::pmr::vector<T>;

template <class KeyT,class Json>
using sorted_json_object = sorted_json_object<KeyT,Json,vector>;

template <class Json>
using array = json_array<Json,vector>;

template <class CharT, class CharTraits, class Allocator>
using string = std::pmr::basic_string<CharT>;
};

TEST_F(JsonTest, CustomMemoryAllocator) {
using custom_json = basic_json<char, pmr_sorted_policy, std::pmr::polymorphic_allocator<char>>;
std::pmr::polymorphic_allocator<char> pa{std::pmr::new_delete_resource()};
TEST_F(JsonTest, JsonWithPolymorhicAllocator) {
char buffer[1024] = {};
std::pmr::monotonic_buffer_resource pool{std::data(buffer), std::size(buffer)};
std::pmr::polymorphic_allocator<char> alloc(&pool);

std::string input = R"(
{ "store": {
"book": [
{ "category": "Roman",
"author": " Felix Lobrecht",
"author": "Felix Lobrecht",
"title": "Sonne und Beton",
"price": 12.99
},
Expand All @@ -139,6 +124,13 @@ TEST_F(JsonTest, CustomMemoryAllocator) {
}
)";

custom_json j;
auto j1 = pmr::json::parse(combine_allocators(alloc), input, json_options{});
EXPECT_EQ("Roman", j1["store"]["book"][0]["category"].as_string());
EXPECT_EQ("Felix Lobrecht", j1["store"]["book"][0]["author"].as_string());
EXPECT_EQ(12.99, j1["store"]["book"][0]["price"].as_double());

EXPECT_EQ("Roman", j1["store"]["book"][1]["category"].as_string());
EXPECT_EQ("Im Westen nichts Neues", j1["store"]["book"][1]["title"].as_string());
EXPECT_EQ(10.00, j1["store"]["book"][1]["price"].as_double());
}
} // namespace dfly
6 changes: 3 additions & 3 deletions src/server/cluster/cluster_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ optional<vector<ClusterConfig::SlotRange>> GetClusterSlotRanges(const JsonType&

vector<ClusterConfig::SlotRange> ranges;

for (const auto& range : slots.array_value()) {
for (const auto& range : slots.array_range()) {
if (!range.is_object()) {
LOG(WARNING) << kInvalidConfigPrefix << "slot_ranges element is not an object " << range;
return nullopt;
Expand Down Expand Up @@ -212,7 +212,7 @@ optional<ClusterConfig::ClusterShards> BuildClusterConfigFromJson(const JsonType
return nullopt;
}

for (const auto& element : json.array_value()) {
for (const auto& element : json.array_range()) {
ClusterConfig::ClusterShard shard;

if (!element.is_object()) {
Expand All @@ -238,7 +238,7 @@ optional<ClusterConfig::ClusterShards> BuildClusterConfigFromJson(const JsonType
return nullopt;
}

for (const auto& replica : replicas.array_value()) {
for (const auto& replica : replicas.array_range()) {
auto node = ParseClusterNode(replica);
if (!node.has_value()) {
return nullopt;
Expand Down
4 changes: 2 additions & 2 deletions src/server/json_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ OpResult<vector<OptSizeT>> OpObjLen(const OpArgs& op_args, string_view key,
vector<OptSizeT> vec;
auto cb = [&vec](const string_view& path, const JsonType& val) {
if (val.is_object()) {
vec.emplace_back(val.object_value().size());
vec.emplace_back(val.size());
} else {
vec.emplace_back(nullopt);
}
Expand All @@ -483,7 +483,7 @@ OpResult<vector<OptSizeT>> OpArrLen(const OpArgs& op_args, string_view key,
vector<OptSizeT> vec;
auto cb = [&vec](const string_view& path, const JsonType& val) {
if (val.is_array()) {
vec.emplace_back(val.array_value().size());
vec.emplace_back(val.size());
} else {
vec.emplace_back(nullopt);
}
Expand Down

0 comments on commit 49ab388

Please sign in to comment.