/Users/deen/code/yugabyte-db/src/yb/common/jsonb-test.cc
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include <string> |
15 | | #include <type_traits> |
16 | | |
17 | | #include <gtest/gtest.h> |
18 | | #include <rapidjson/prettywriter.h> |
19 | | |
20 | | #include "yb/common/jsonb.h" |
21 | | |
22 | | #include "yb/gutil/dynamic_annotations.h" |
23 | | |
24 | | #include "yb/util/status.h" |
25 | | #include "yb/util/test_macros.h" |
26 | | #include "yb/util/tostring.h" |
27 | | |
28 | | using std::to_string; |
29 | | using std::numeric_limits; |
30 | | |
31 | | namespace yb { |
32 | | namespace common { |
33 | | |
34 | 2 | void ParseJson(const std::string& json, rapidjson::Document* document) { |
35 | 2 | Jsonb jsonb; |
36 | 2 | LOG(INFO) << "Parsing json..."; |
37 | 2 | ASSERT_OK(jsonb.FromString(json)); |
38 | 2 | ASSERT_FALSE(jsonb.SerializedJsonb().empty()); |
39 | | |
40 | 2 | ASSERT_OK(jsonb.ToRapidJson(document)); |
41 | | |
42 | 2 | rapidjson::StringBuffer buffer; |
43 | 2 | rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer); |
44 | 2 | document->Accept(writer); |
45 | 2 | LOG (INFO) << "Deserialized json: " << buffer.GetString(); |
46 | 2 | } |
47 | | |
48 | 2 | void VerifyArray(const rapidjson::Value& document) { |
49 | 2 | ASSERT_TRUE(document.IsArray()); |
50 | 2 | ASSERT_EQ(1, document[0].GetInt()); |
51 | 2 | ASSERT_EQ(2, document[1].GetInt()); |
52 | 2 | ASSERT_DOUBLE_EQ(3.0, document[2].GetFloat()); |
53 | 2 | ASSERT_TRUE(document[3].IsFalse()); |
54 | 2 | ASSERT_TRUE(document[4].IsTrue()); |
55 | 2 | ASSERT_TRUE(document[5].IsObject()); |
56 | 2 | ASSERT_EQ(1, document[5]["k1"].GetInt()); |
57 | 2 | ASSERT_TRUE(document[5]["k2"].IsArray()); |
58 | 2 | ASSERT_EQ(100, document[5]["k2"][0].GetInt()); |
59 | 2 | ASSERT_EQ(200, document[5]["k2"][1].GetInt()); |
60 | 2 | ASSERT_EQ(300, document[5]["k2"][2].GetInt()); |
61 | 2 | ASSERT_TRUE(document[5]["k3"].IsTrue()); |
62 | 2 | } |
63 | | |
64 | 1 | TEST(JsonbTest, TestJsonbSerialization) { |
65 | 1 | rapidjson::Document document; |
66 | 1 | ParseJson(R"#( |
67 | 1 | { |
68 | 1 | "b" : 1, |
69 | 1 | "a1" : [1, 2, 3.0, false, true, { "k1" : 1, "k2" : [100, 200, 300], "k3" : true}], |
70 | 1 | "a" : |
71 | 1 | { |
72 | 1 | "d" : true, |
73 | 1 | "q" : |
74 | 1 | { |
75 | 1 | "p" : 4294967295, |
76 | 1 | "r" : -2147483648, |
77 | 1 | "s" : 2147483647 |
78 | 1 | }, |
79 | 1 | "g" : -100, |
80 | 1 | "c" : false, |
81 | 1 | "f" : "hello", |
82 | 1 | "x" : 2.1, |
83 | 1 | "y" : 9223372036854775807, |
84 | 1 | "z" : -9223372036854775808, |
85 | 1 | "u" : 18446744073709551615, |
86 | 1 | "l" : 2147483647.123123e+75, |
87 | 1 | "e" : null |
88 | 1 | } |
89 | 1 | })#", &document); |
90 | | |
91 | | // Verify the json. |
92 | 1 | ASSERT_TRUE(document.HasMember("a")); |
93 | 1 | ASSERT_TRUE(document["a"].IsObject()); |
94 | | |
95 | 1 | ASSERT_TRUE(document["a"].HasMember("c")); |
96 | 1 | ASSERT_TRUE(document["a"]["c"].IsFalse()); |
97 | 1 | ASSERT_TRUE(document["a"].HasMember("d")); |
98 | 1 | ASSERT_TRUE(document["a"]["d"].IsTrue()); |
99 | 1 | ASSERT_TRUE(document["a"].HasMember("e")); |
100 | 1 | ASSERT_TRUE(document["a"]["e"].IsNull()); |
101 | 1 | ASSERT_TRUE(document["a"].HasMember("f")); |
102 | 1 | ASSERT_TRUE(document["a"]["f"].IsString()); |
103 | 1 | ASSERT_EQ("hello", std::string(document["a"]["f"].GetString())); |
104 | 1 | ASSERT_TRUE(document["a"].HasMember("g")); |
105 | 1 | ASSERT_TRUE(document["a"]["g"].IsInt64()); |
106 | 1 | ASSERT_EQ(-100, document["a"]["g"].GetInt64()); |
107 | | |
108 | 1 | ASSERT_TRUE(document["a"].HasMember("q")); |
109 | 1 | ASSERT_TRUE(document["a"]["q"].IsObject()); |
110 | | |
111 | 1 | ASSERT_TRUE(document["a"]["q"].HasMember("p")); |
112 | 1 | ASSERT_TRUE(document["a"]["q"]["p"].IsUint()); |
113 | 1 | ASSERT_EQ(4294967295, document["a"]["q"]["p"].GetUint()); |
114 | | |
115 | 1 | ASSERT_TRUE(document["a"]["q"].HasMember("r")); |
116 | 1 | ASSERT_TRUE(document["a"]["q"]["r"].IsInt()); |
117 | 1 | ASSERT_EQ(-2147483648, document["a"]["q"]["r"].GetInt()); |
118 | | |
119 | 1 | ASSERT_TRUE(document["a"]["q"].HasMember("s")); |
120 | 1 | ASSERT_TRUE(document["a"]["q"]["s"].IsInt()); |
121 | 1 | ASSERT_EQ(2147483647, document["a"]["q"]["s"].GetInt()); |
122 | | |
123 | 1 | ASSERT_TRUE(document["a"].HasMember("x")); |
124 | 1 | ASSERT_TRUE(document["a"]["x"].IsFloat()); |
125 | 1 | ASSERT_FLOAT_EQ(2.1f, document["a"]["x"].GetFloat()); |
126 | | |
127 | 1 | ASSERT_TRUE(document["a"].HasMember("u")); |
128 | 1 | ASSERT_TRUE(document["a"]["u"].IsUint64()); |
129 | 1 | ASSERT_EQ(18446744073709551615ULL, document["a"]["u"].GetUint64()); |
130 | | |
131 | 1 | ASSERT_TRUE(document["a"].HasMember("y")); |
132 | 1 | ASSERT_TRUE(document["a"]["y"].IsInt64()); |
133 | 1 | ASSERT_EQ(9223372036854775807LL, document["a"]["y"].GetInt64()); |
134 | | |
135 | 1 | ASSERT_TRUE(document["a"].HasMember("z")); |
136 | 1 | ASSERT_TRUE(document["a"]["z"].IsInt64()); |
137 | 1 | ASSERT_EQ(-9223372036854775808ULL, document["a"]["z"].GetInt64()); |
138 | | |
139 | 1 | ASSERT_TRUE(document["a"].HasMember("l")); |
140 | 1 | ASSERT_TRUE(document["a"]["l"].IsDouble()); |
141 | 1 | ASSERT_DOUBLE_EQ(2147483647.123123e+75, document["a"]["l"].GetDouble()); |
142 | | |
143 | 1 | ASSERT_TRUE(document.HasMember("b")); |
144 | 1 | ASSERT_TRUE(document["b"].IsInt64()); |
145 | 1 | ASSERT_EQ(1, document["b"].GetInt64()); |
146 | | |
147 | | // Test array. |
148 | 1 | ASSERT_TRUE(document.HasMember("a1")); |
149 | 1 | ASSERT_TRUE(document["a1"].IsArray()); |
150 | 1 | VerifyArray(document["a1"]); |
151 | 1 | } |
152 | | |
153 | 1 | TEST(JsonbTest, TestArrays) { |
154 | 1 | rapidjson::Document document; |
155 | 1 | ParseJson(R"#( |
156 | 1 | [1, 2, 3.0, false, true, { "k1" : 1, "k2" : [100, 200, 300], "k3" : true}] |
157 | 1 | )#", &document); |
158 | 1 | VerifyArray(document); |
159 | 1 | } |
160 | | |
161 | | } // namespace common |
162 | | } // namespace yb |