Skip to content

Commit fc7545b

Browse files
authored
fix: json pointer enbale_if_v (bytedance#26)
# Main Changes: 1. replace enbale_if_v(c++17) with enable_if(C++11) 2. add delete nullptr_t construct functions
1 parent b47e7dd commit fc7545b

9 files changed

Lines changed: 90 additions & 9 deletions

File tree

example/CMakeLists.txt

Whitespace-only changes.

example/addmember.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ int main() {
1313
node.AddMember(NodeType("ConstKey"), NodeType("CopiedKey", alloc), alloc);
1414
return 0;
1515
}
16-
// g++ -I../include/ -march=haswell --std=c++14 get_and_set.cpp -o get_and_set
16+
// g++ -I../include/ -march=haswell --std=c++11 get_and_set.cpp -o get_and_set

example/check_parse_result.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ int main() {
2020
}
2121
return 0;
2222
}
23-
// g++ -I../include/ -march=haswell --std=c++14 check_parse_result.cpp -o
23+
// g++ -I../include/ -march=haswell --std=c++11 check_parse_result.cpp -o
2424
// check_parse_result

example/get_and_set.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ int main() {
6060
}
6161
return 0;
6262
}
63-
// g++ -I../include/ -march=haswell --std=c++14 get_and_set.cpp -o get_and_set
63+
// g++ -I../include/ -march=haswell --std=c++11 get_and_set.cpp -o get_and_set

example/parse_and_serialize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ int main() {
1919
std::cout << wb.ToString() << std::endl;
2020
return 0;
2121
}
22-
// g++ -I../include/ -march=haswell --std=c++14 parse_and_serialize.cpp -o
22+
// g++ -I../include/ -march=haswell --std=c++11 parse_and_serialize.cpp -o
2323
// parse_and_serialize

example/parseondemand.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "sonic/sonic.h"
2+
3+
std::string json = R"(
4+
{
5+
"a": {
6+
"a0":[0,1,2,3,4,5,6,7,8,9],
7+
"a1": "hi"
8+
},
9+
"b":[
10+
{"b0":1},
11+
{"b1":2}
12+
]
13+
}
14+
)";
15+
16+
int main() {
17+
// The target is exsited in JSON
18+
{
19+
sonic_json::Document doc;
20+
doc.ParseOnDemand(json, {"a", "a0", 8});
21+
if (doc.HasParseError()) {
22+
return -1;
23+
}
24+
uint64_t val = doc.GetUint64();
25+
std::cout << "Parse ondemand result is " << val << std::endl;
26+
// output: Parse ondemand result is 8
27+
}
28+
29+
// The target is not exsited in JSON
30+
{
31+
sonic_json::Document doc;
32+
doc.ParseOnDemand(json, {"a", "a1", "unknown"});
33+
if (doc.HasParseError()) {
34+
sonic_json::SonicError err = doc.GetParseError();
35+
size_t error_position = doc.GetErrorOffset();
36+
std::cout << "Parse Error: " << sonic_json::ErrorMsg(err)
37+
<< ". Error Position At " << error_position << std::endl;
38+
// output: Parse Error: ParseOnDemand: the target type is not matched..
39+
// Error Position At 55
40+
}
41+
}
42+
return 0;
43+
}
44+
// g++ -I../include/ -march=haswell --std=c++11 parseondemand.cpp -o
45+
// parseondemand

include/sonic/dom/json_pointer.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ template <typename StringType = SONIC_JSON_POINTER_NODE_STRING_DEFAULT_TYPE>
3636
class GenericJsonPointerNode {
3737
public:
3838
GenericJsonPointerNode() = delete;
39+
GenericJsonPointerNode(std::nullptr_t) = delete;
3940
GenericJsonPointerNode(StringView str)
4041
: str_(str), num_(0), is_number_(false) {}
4142
GenericJsonPointerNode(const std::string& str)
4243
: str_(str), num_(0), is_number_(false) {}
4344
GenericJsonPointerNode(const char* str)
4445
: str_(str), num_(0), is_number_(false) {}
45-
template <typename T,
46-
std::enable_if_t<std::is_integral<T>::value, bool> = true>
46+
template <typename T, typename std::enable_if<std::is_integral<T>::value,
47+
bool>::type = true>
4748
GenericJsonPointerNode(T i)
4849
: str_(), num_(static_cast<int>(i)), is_number_(true) {}
4950

@@ -159,4 +160,7 @@ using JsonPointer =
159160
using JsonPointerNode =
160161
GenericJsonPointerNode<SONIC_JSON_POINTER_NODE_STRING_DEFAULT_TYPE>;
161162

163+
using JsonPointerView = GenericJsonPointer<StringView>;
164+
using JsonPointerNodeView = GenericJsonPointerNode<StringView>;
165+
162166
} // namespace sonic_json

include/sonic/error.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ enum SonicError {
4242
///< object.
4343
kParseErrorArrIndexOutOfRange, ///< ParseOnDemand: the target array index out
4444
///< of range.
45-
kParseErrorMismatchType, ///< ParseOnDemand: try to find index in object or
46-
///< find key in array.
45+
kParseErrorMismatchType, ///< ParseOnDemand: the target type is not matched.
4746
kSerErrorUnsupportedType, ///< Serialize: DOM has invalid node type.
4847
kSerErrorInfinity, ///< Serialize: DOM has inifinity number node.
4948
kSerErrorInvalidObjKey, ///< Serialize: The type of object's key is not
@@ -77,7 +76,7 @@ inline const char* ErrorMsg(SonicError error) noexcept {
7776
{kParseErrorArrIndexOutOfRange,
7877
"ParseOnDemand: the target array index out of range."},
7978
{kParseErrorMismatchType,
80-
"ParseOnDemand: try to find index in object or find key in array."},
79+
"ParseOnDemand: the target type is not matched."},
8180
{kSerErrorUnsupportedType, "Serialize: DOM has invalid node type."},
8281
{kSerErrorInfinity, "Serialize: DOM has inifinity number node."},
8382
{kSerErrorInvalidObjKey,

tests/json_pointer_test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,39 @@ using JsonPointerTypes =
3737

3838
TYPED_TEST_SUITE(JsonPointerTest, JsonPointerTypes);
3939

40+
TYPED_TEST(JsonPointerTest, NodeConstructor) {
41+
using JsonPointerType = TypeParam;
42+
using JPNodeType = typename JsonPointerType::JsonPointerNodeType;
43+
44+
// string
45+
{ JPNodeType node("hi"); }
46+
{
47+
std::string hi = "hi";
48+
JPNodeType node(hi);
49+
}
50+
{ JPNodeType node(sonic_json::StringView("hi")); }
51+
{
52+
char hi[3] = "hi";
53+
JPNodeType node(hi);
54+
}
55+
56+
// number
57+
#define TEST_INIT_TYPE(type) \
58+
{ JPNodeType node((type)(0)); }
59+
TEST_INIT_TYPE(int);
60+
TEST_INIT_TYPE(unsigned int);
61+
TEST_INIT_TYPE(int64_t);
62+
TEST_INIT_TYPE(uint64_t);
63+
TEST_INIT_TYPE(size_t);
64+
TEST_INIT_TYPE(bool);
65+
66+
// MUST compile failed code here.
67+
// TEST_INIT_TYPE(double);
68+
// TEST_INIT_TYPE(float);
69+
// TEST_INIT_TYPE(std::nullptr_t);
70+
// { uint8_t hi[3] = "hi"; JPNodeType node(hi); }
71+
}
72+
4073
TYPED_TEST(JsonPointerTest, Constructor) {
4174
using JsonPointerType = TypeParam;
4275

0 commit comments

Comments
 (0)