1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <cstdio> 17 18 #include "utils/json_parser.h" 19 #include <gtest/gtest.h> 20 21 namespace panda::json_parser::test { 22 HWTEST(JsonParser, ParsePrimitive, testing::ext::TestSize.Level0) 23 { 24 auto str = R"( 25 { 26 "key_0" : "key_0.value", 27 "key_1" : "\"key_1\"\\.\u0020value\n" 28 } 29 )"; 30 31 JsonObject obj(str); 32 ASSERT_TRUE(obj.IsValid()); 33 34 ASSERT_NE(obj.GetValue<JsonObject::StringT>("key_0"), nullptr); 35 ASSERT_EQ(*obj.GetValue<JsonObject::StringT>("key_0"), "key_0.value"); 36 37 ASSERT_NE(obj.GetValue<JsonObject::StringT>("key_1"), nullptr); 38 ASSERT_EQ(*obj.GetValue<JsonObject::StringT>("key_1"), "\"key_1\"\\. value\n"); 39 40 ASSERT_EQ(obj.GetValue<JsonObject::StringT>("key_2"), nullptr); 41 } 42 43 HWTEST(JsonParser, Arrays, testing::ext::TestSize.Level0) 44 { 45 auto str = R"( 46 { 47 "key_0" : 48 [ 49 "elem0", 50 [ "elem1.0", "elem1.1" ], 51 "elem2" 52 ], 53 "key_1": [] 54 } 55 )"; 56 57 JsonObject obj(str); 58 ASSERT_TRUE(obj.IsValid()); 59 60 ASSERT_NE(obj.GetValue<JsonObject::ArrayT>("key_0"), nullptr); 61 auto &main_array = *obj.GetValue<JsonObject::ArrayT>("key_0"); 62 63 // Check [0]: 64 ASSERT_NE(main_array[0].Get<JsonObject::StringT>(), nullptr); 65 ASSERT_EQ(*main_array[0].Get<JsonObject::StringT>(), "elem0"); 66 67 // Check [1]: 68 ASSERT_NE(main_array[1].Get<JsonObject::ArrayT>(), nullptr); 69 auto &inner_array = *main_array[1].Get<JsonObject::ArrayT>(); 70 71 ASSERT_NE(inner_array[0].Get<JsonObject::StringT>(), nullptr); 72 ASSERT_EQ(*inner_array[0].Get<JsonObject::StringT>(), "elem1.0"); 73 74 ASSERT_NE(inner_array[1].Get<JsonObject::StringT>(), nullptr); 75 ASSERT_EQ(*inner_array[1].Get<JsonObject::StringT>(), "elem1.1"); 76 77 // Check [2]: 78 ASSERT_NE(main_array[2].Get<JsonObject::StringT>(), nullptr); 79 ASSERT_EQ(*main_array[2].Get<JsonObject::StringT>(), "elem2"); 80 81 ASSERT_NE(obj.GetValue<JsonObject::ArrayT>("key_1"), nullptr); 82 auto &empty_array = *obj.GetValue<JsonObject::ArrayT>("key_1"); 83 84 // Check [3]: 85 ASSERT_EQ(empty_array.size(), 0U); 86 } 87 88 HWTEST(JsonParser, NestedObject, testing::ext::TestSize.Level0) 89 { 90 auto str = R"( 91 { 92 "key_0" : "key_0.value", 93 "repeated_key_1" : "repeated_key_1.value0", 94 "key_1" : 95 { 96 "key_0.0" : "key_0.0.value", 97 "repeated_key_1" : "repeated_key_1.value1", 98 "repeated_key_2" : "repeated_key_2.value0" 99 }, 100 "repeated_key_2" : "repeated_key_2.value1", 101 "key_2" : {} 102 } 103 )"; 104 105 JsonObject obj(str); 106 ASSERT_TRUE(obj.IsValid()); 107 108 // Check key_0: 109 ASSERT_NE(obj.GetValue<JsonObject::StringT>("key_0"), nullptr); 110 ASSERT_EQ(*obj.GetValue<JsonObject::StringT>("key_0"), "key_0.value"); 111 112 // Check repeated_key_1 (in main obj): 113 ASSERT_NE(obj.GetValue<JsonObject::StringT>("repeated_key_1"), nullptr); 114 ASSERT_EQ(*obj.GetValue<JsonObject::StringT>("repeated_key_1"), "repeated_key_1.value0"); 115 116 // Inner object: 117 ASSERT_NE(obj.GetValue<JsonObject::JsonObjPointer>("key_1"), nullptr); 118 const auto *inner_obj = obj.GetValue<JsonObject::JsonObjPointer>("key_1")->get(); 119 ASSERT_NE(inner_obj, nullptr); 120 ASSERT_TRUE(inner_obj->IsValid()); 121 122 // Check key_0.0: 123 ASSERT_NE(inner_obj->GetValue<JsonObject::StringT>("key_0.0"), nullptr); 124 ASSERT_EQ(*inner_obj->GetValue<JsonObject::StringT>("key_0.0"), "key_0.0.value"); 125 126 // Check repeated_key_1: 127 ASSERT_NE(inner_obj->GetValue<JsonObject::StringT>("repeated_key_1"), nullptr); 128 ASSERT_EQ(*inner_obj->GetValue<JsonObject::StringT>("repeated_key_1"), "repeated_key_1.value1"); 129 130 // Check repeated_key_2: 131 ASSERT_NE(inner_obj->GetValue<JsonObject::StringT>("repeated_key_2"), nullptr); 132 ASSERT_EQ(*inner_obj->GetValue<JsonObject::StringT>("repeated_key_2"), "repeated_key_2.value0"); 133 134 // Check repeated_key_2 (in main obj): 135 ASSERT_NE(obj.GetValue<JsonObject::StringT>("repeated_key_2"), nullptr); 136 ASSERT_EQ(*obj.GetValue<JsonObject::StringT>("repeated_key_2"), "repeated_key_2.value1"); 137 138 // Check empty inner object (key_2): 139 ASSERT_NE(obj.GetValue<JsonObject::JsonObjPointer>("key_2"), nullptr); 140 const auto *empty_obj = obj.GetValue<JsonObject::JsonObjPointer>("key_2")->get(); 141 ASSERT_NE(empty_obj, nullptr); 142 ASSERT_TRUE(empty_obj->IsValid()); 143 ASSERT_EQ(empty_obj->GetSize(), 0U); 144 } 145 146 HWTEST(JsonParser, Numbers, testing::ext::TestSize.Level0) 147 { 148 auto str = R"( 149 { 150 "key_0" : 0, 151 "key_1" : 128, 152 "key_2" : -256, 153 "key_3" : .512, 154 "key_4" : 1.024, 155 "key_5" : -204.8 156 } 157 )"; 158 159 JsonObject obj(str); 160 ASSERT_TRUE(obj.IsValid()); 161 162 ASSERT_NE(obj.GetValue<JsonObject::NumT>("key_0"), nullptr); 163 ASSERT_EQ(*obj.GetValue<JsonObject::NumT>("key_0"), 0); 164 165 ASSERT_NE(obj.GetValue<JsonObject::NumT>("key_1"), nullptr); 166 ASSERT_EQ(*obj.GetValue<JsonObject::NumT>("key_1"), 128); 167 168 ASSERT_NE(obj.GetValue<JsonObject::NumT>("key_2"), nullptr); 169 ASSERT_EQ(*obj.GetValue<JsonObject::NumT>("key_2"), -256); 170 171 ASSERT_NE(obj.GetValue<JsonObject::NumT>("key_3"), nullptr); 172 ASSERT_EQ(*obj.GetValue<JsonObject::NumT>("key_3"), .512); 173 174 ASSERT_NE(obj.GetValue<JsonObject::NumT>("key_4"), nullptr); 175 ASSERT_EQ(*obj.GetValue<JsonObject::NumT>("key_4"), 1.024); 176 177 ASSERT_NE(obj.GetValue<JsonObject::NumT>("key_5"), nullptr); 178 ASSERT_EQ(*obj.GetValue<JsonObject::NumT>("key_5"), -204.8); 179 } 180 181 HWTEST(JsonParser, Boolean, testing::ext::TestSize.Level0) 182 { 183 auto str = R"( 184 { 185 "key_0" : true, 186 "key_1" : false 187 } 188 )"; 189 190 JsonObject obj(str); 191 ASSERT_TRUE(obj.IsValid()); 192 193 ASSERT_NE(obj.GetValue<JsonObject::BoolT>("key_0"), nullptr); 194 ASSERT_EQ(*obj.GetValue<JsonObject::BoolT>("key_0"), true); 195 196 ASSERT_NE(obj.GetValue<JsonObject::BoolT>("key_1"), nullptr); 197 ASSERT_EQ(*obj.GetValue<JsonObject::BoolT>("key_1"), false); 198 } 199 200 HWTEST(JsonParser, InvalidJson, testing::ext::TestSize.Level0) 201 { 202 auto repeated_keys = R"( 203 { 204 "key_0" : "key_0.value0", 205 "key_0" : "key_0.value1", 206 } 207 )"; 208 209 JsonObject obj(repeated_keys); 210 ASSERT_FALSE(obj.IsValid()); 211 } 212 } // namespace panda::json_parser::test 213