blob: 17dea560382dd5266bc6a503d2c869ec994d6593 [file] [log] [blame]
pkasting@chromium.org36515db2009-11-26 05:47:52 +09001// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#include "testing/gtest/include/gtest/gtest.h"
brettw@chromium.org7cd41eb2009-10-24 05:00:20 +09006#include "base/json/json_reader.h"
aa@chromium.orgca9c79e2008-12-30 04:59:08 +09007#include "base/scoped_ptr.h"
initial.commit3f4a7322008-07-27 06:49:38 +09008#include "base/values.h"
tc@google.comd3bb16f2008-08-09 02:26:42 +09009#include "build/build_config.h"
initial.commit3f4a7322008-07-27 06:49:38 +090010
brettw@chromium.org7cd41eb2009-10-24 05:00:20 +090011namespace base {
12
initial.commit3f4a7322008-07-27 06:49:38 +090013TEST(JSONReaderTest, Reading) {
14 // some whitespace checking
aa@chromium.orgca9c79e2008-12-30 04:59:08 +090015 scoped_ptr<Value> root;
16 root.reset(JSONReader().JsonToValue(" null ", false, false));
17 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +090018 ASSERT_TRUE(root->IsType(Value::TYPE_NULL));
initial.commit3f4a7322008-07-27 06:49:38 +090019
20 // Invalid JSON string
aa@chromium.orgca9c79e2008-12-30 04:59:08 +090021 root.reset(JSONReader().JsonToValue("nu", false, false));
22 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +090023
24 // Simple bool
aa@chromium.orgca9c79e2008-12-30 04:59:08 +090025 root.reset(JSONReader().JsonToValue("true ", false, false));
26 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +090027 ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN));
initial.commit3f4a7322008-07-27 06:49:38 +090028
munjal@chromium.org124260d2009-07-24 06:34:40 +090029 // Embedded comment
30 root.reset(JSONReader().JsonToValue("/* comment */null", false, false));
31 ASSERT_TRUE(root.get());
32 ASSERT_TRUE(root->IsType(Value::TYPE_NULL));
33 root.reset(JSONReader().JsonToValue("40 /* comment */", false, false));
34 ASSERT_TRUE(root.get());
35 ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
36 root.reset(JSONReader().JsonToValue("true // comment", false, false));
37 ASSERT_TRUE(root.get());
38 ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN));
39 root.reset(JSONReader().JsonToValue("/* comment */\"sample string\"",
40 false, false));
41 ASSERT_TRUE(root.get());
42 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
43 std::string value;
44 ASSERT_TRUE(root->GetAsString(&value));
45 ASSERT_EQ("sample string", value);
46
initial.commit3f4a7322008-07-27 06:49:38 +090047 // Test number formats
aa@chromium.orgca9c79e2008-12-30 04:59:08 +090048 root.reset(JSONReader().JsonToValue("43", false, false));
49 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +090050 ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
51 int int_val = 0;
52 ASSERT_TRUE(root->GetAsInteger(&int_val));
53 ASSERT_EQ(43, int_val);
initial.commit3f4a7322008-07-27 06:49:38 +090054
55 // According to RFC4627, oct, hex, and leading zeros are invalid JSON.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +090056 root.reset(JSONReader().JsonToValue("043", false, false));
57 ASSERT_FALSE(root.get());
58 root.reset(JSONReader().JsonToValue("0x43", false, false));
59 ASSERT_FALSE(root.get());
60 root.reset(JSONReader().JsonToValue("00", false, false));
61 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +090062
63 // Test 0 (which needs to be special cased because of the leading zero
64 // clause).
aa@chromium.orgca9c79e2008-12-30 04:59:08 +090065 root.reset(JSONReader().JsonToValue("0", false, false));
66 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +090067 ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
68 int_val = 1;
69 ASSERT_TRUE(root->GetAsInteger(&int_val));
70 ASSERT_EQ(0, int_val);
initial.commit3f4a7322008-07-27 06:49:38 +090071
mmentovai@google.com8dcf71c2008-08-08 02:15:41 +090072 // Numbers that overflow ints should succeed, being internally promoted to
73 // storage as doubles
aa@chromium.orgca9c79e2008-12-30 04:59:08 +090074 root.reset(JSONReader().JsonToValue("2147483648", false, false));
75 ASSERT_TRUE(root.get());
tc@google.comd3bb16f2008-08-09 02:26:42 +090076 double real_val;
tc@google.comd3bb16f2008-08-09 02:26:42 +090077 ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
78 real_val = 0.0;
79 ASSERT_TRUE(root->GetAsReal(&real_val));
80 ASSERT_DOUBLE_EQ(2147483648.0, real_val);
aa@chromium.orgca9c79e2008-12-30 04:59:08 +090081 root.reset(JSONReader().JsonToValue("-2147483649", false, false));
82 ASSERT_TRUE(root.get());
tc@google.comd3bb16f2008-08-09 02:26:42 +090083 ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
84 real_val = 0.0;
85 ASSERT_TRUE(root->GetAsReal(&real_val));
86 ASSERT_DOUBLE_EQ(-2147483649.0, real_val);
initial.commit3f4a7322008-07-27 06:49:38 +090087
88 // Parse a double
aa@chromium.orgca9c79e2008-12-30 04:59:08 +090089 root.reset(JSONReader().JsonToValue("43.1", false, false));
90 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +090091 ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
tc@google.comd3bb16f2008-08-09 02:26:42 +090092 real_val = 0.0;
initial.commit3f4a7322008-07-27 06:49:38 +090093 ASSERT_TRUE(root->GetAsReal(&real_val));
94 ASSERT_DOUBLE_EQ(43.1, real_val);
initial.commit3f4a7322008-07-27 06:49:38 +090095
aa@chromium.orgca9c79e2008-12-30 04:59:08 +090096 root.reset(JSONReader().JsonToValue("4.3e-1", false, false));
97 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +090098 ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
99 real_val = 0.0;
100 ASSERT_TRUE(root->GetAsReal(&real_val));
101 ASSERT_DOUBLE_EQ(.43, real_val);
initial.commit3f4a7322008-07-27 06:49:38 +0900102
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900103 root.reset(JSONReader().JsonToValue("2.1e0", false, false));
104 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900105 ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
106 real_val = 0.0;
107 ASSERT_TRUE(root->GetAsReal(&real_val));
108 ASSERT_DOUBLE_EQ(2.1, real_val);
initial.commit3f4a7322008-07-27 06:49:38 +0900109
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900110 root.reset(JSONReader().JsonToValue("2.1e+0001", false, false));
111 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900112 ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
113 real_val = 0.0;
114 ASSERT_TRUE(root->GetAsReal(&real_val));
115 ASSERT_DOUBLE_EQ(21.0, real_val);
initial.commit3f4a7322008-07-27 06:49:38 +0900116
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900117 root.reset(JSONReader().JsonToValue("0.01", false, false));
118 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900119 ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
120 real_val = 0.0;
121 ASSERT_TRUE(root->GetAsReal(&real_val));
122 ASSERT_DOUBLE_EQ(0.01, real_val);
initial.commit3f4a7322008-07-27 06:49:38 +0900123
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900124 root.reset(JSONReader().JsonToValue("1.00", false, false));
125 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900126 ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
127 real_val = 0.0;
128 ASSERT_TRUE(root->GetAsReal(&real_val));
129 ASSERT_DOUBLE_EQ(1.0, real_val);
initial.commit3f4a7322008-07-27 06:49:38 +0900130
131 // Fractional parts must have a digit before and after the decimal point.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900132 root.reset(JSONReader().JsonToValue("1.", false, false));
133 ASSERT_FALSE(root.get());
134 root.reset(JSONReader().JsonToValue(".1", false, false));
135 ASSERT_FALSE(root.get());
136 root.reset(JSONReader().JsonToValue("1.e10", false, false));
137 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900138
139 // Exponent must have a digit following the 'e'.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900140 root.reset(JSONReader().JsonToValue("1e", false, false));
141 ASSERT_FALSE(root.get());
142 root.reset(JSONReader().JsonToValue("1E", false, false));
143 ASSERT_FALSE(root.get());
144 root.reset(JSONReader().JsonToValue("1e1.", false, false));
145 ASSERT_FALSE(root.get());
146 root.reset(JSONReader().JsonToValue("1e1.0", false, false));
147 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900148
149 // INF/-INF/NaN are not valid
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900150 root.reset(JSONReader().JsonToValue("1e1000", false, false));
151 ASSERT_FALSE(root.get());
152 root.reset(JSONReader().JsonToValue("-1e1000", false, false));
153 ASSERT_FALSE(root.get());
154 root.reset(JSONReader().JsonToValue("NaN", false, false));
155 ASSERT_FALSE(root.get());
156 root.reset(JSONReader().JsonToValue("nan", false, false));
157 ASSERT_FALSE(root.get());
158 root.reset(JSONReader().JsonToValue("inf", false, false));
159 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900160
161 // Invalid number formats
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900162 root.reset(JSONReader().JsonToValue("4.3.1", false, false));
163 ASSERT_FALSE(root.get());
164 root.reset(JSONReader().JsonToValue("4e3.1", false, false));
165 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900166
167 // Test string parser
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900168 root.reset(JSONReader().JsonToValue("\"hello world\"", false, false));
169 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900170 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
171 std::wstring str_val;
172 ASSERT_TRUE(root->GetAsString(&str_val));
173 ASSERT_EQ(L"hello world", str_val);
initial.commit3f4a7322008-07-27 06:49:38 +0900174
175 // Empty string
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900176 root.reset(JSONReader().JsonToValue("\"\"", false, false));
177 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900178 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
179 str_val.clear();
180 ASSERT_TRUE(root->GetAsString(&str_val));
181 ASSERT_EQ(L"", str_val);
initial.commit3f4a7322008-07-27 06:49:38 +0900182
183 // Test basic string escapes
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900184 root.reset(JSONReader().JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"",
185 false, false));
186 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900187 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
188 str_val.clear();
189 ASSERT_TRUE(root->GetAsString(&str_val));
sky@google.com0ba93512008-10-02 02:26:06 +0900190 ASSERT_EQ(L" \"\\/\b\f\n\r\t\v", str_val);
initial.commit3f4a7322008-07-27 06:49:38 +0900191
192 // Test hex and unicode escapes including the null character.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900193 root.reset(JSONReader().JsonToValue("\"\\x41\\x00\\u1234\"", false,
tc@google.comce6a78d2008-07-29 09:01:31 +0900194 false));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900195 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900196 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
197 str_val.clear();
198 ASSERT_TRUE(root->GetAsString(&str_val));
199 ASSERT_EQ(std::wstring(L"A\0\x1234", 3), str_val);
initial.commit3f4a7322008-07-27 06:49:38 +0900200
201 // Test invalid strings
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900202 root.reset(JSONReader().JsonToValue("\"no closing quote", false, false));
203 ASSERT_FALSE(root.get());
204 root.reset(JSONReader().JsonToValue("\"\\z invalid escape char\"", false,
205 false));
206 ASSERT_FALSE(root.get());
207 root.reset(JSONReader().JsonToValue("\"\\xAQ invalid hex code\"", false,
208 false));
209 ASSERT_FALSE(root.get());
210 root.reset(JSONReader().JsonToValue("not enough hex chars\\x1\"", false,
211 false));
212 ASSERT_FALSE(root.get());
213 root.reset(JSONReader().JsonToValue("\"not enough escape chars\\u123\"",
214 false, false));
215 ASSERT_FALSE(root.get());
216 root.reset(JSONReader().JsonToValue("\"extra backslash at end of input\\\"",
217 false, false));
218 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900219
220 // Basic array
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900221 root.reset(JSONReader::Read("[true, false, null]", false));
222 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900223 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900224 ListValue* list = static_cast<ListValue*>(root.get());
darin@google.comf3272802008-08-15 05:27:29 +0900225 ASSERT_EQ(3U, list->GetSize());
tc@google.comce6a78d2008-07-29 09:01:31 +0900226
227 // Test with trailing comma. Should be parsed the same as above.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900228 scoped_ptr<Value> root2;
229 root2.reset(JSONReader::Read("[true, false, null, ]", true));
230 EXPECT_TRUE(root->Equals(root2.get()));
initial.commit3f4a7322008-07-27 06:49:38 +0900231
232 // Empty array
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900233 root.reset(JSONReader::Read("[]", false));
234 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900235 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900236 list = static_cast<ListValue*>(root.get());
darin@google.comf3272802008-08-15 05:27:29 +0900237 ASSERT_EQ(0U, list->GetSize());
initial.commit3f4a7322008-07-27 06:49:38 +0900238
239 // Nested arrays
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900240 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null]",
241 false));
242 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900243 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900244 list = static_cast<ListValue*>(root.get());
darin@google.comf3272802008-08-15 05:27:29 +0900245 ASSERT_EQ(4U, list->GetSize());
tc@google.comce6a78d2008-07-29 09:01:31 +0900246
247 // Lots of trailing commas.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900248 root2.reset(JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]",
249 true));
250 EXPECT_TRUE(root->Equals(root2.get()));
initial.commit3f4a7322008-07-27 06:49:38 +0900251
252 // Invalid, missing close brace.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900253 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null", false));
254 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900255
256 // Invalid, too many commas
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900257 root.reset(JSONReader::Read("[true,, null]", false));
258 ASSERT_FALSE(root.get());
259 root.reset(JSONReader::Read("[true,, null]", true));
260 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900261
262 // Invalid, no commas
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900263 root.reset(JSONReader::Read("[true null]", false));
264 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900265
266 // Invalid, trailing comma
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900267 root.reset(JSONReader::Read("[true,]", false));
268 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900269
tc@google.comce6a78d2008-07-29 09:01:31 +0900270 // Valid if we set |allow_trailing_comma| to true.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900271 root.reset(JSONReader::Read("[true,]", true));
272 ASSERT_TRUE(root.get());
tc@google.comce6a78d2008-07-29 09:01:31 +0900273 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900274 list = static_cast<ListValue*>(root.get());
darin@google.comf3272802008-08-15 05:27:29 +0900275 EXPECT_EQ(1U, list->GetSize());
tc@google.comce6a78d2008-07-29 09:01:31 +0900276 Value* tmp_value = NULL;
277 ASSERT_TRUE(list->Get(0, &tmp_value));
278 EXPECT_TRUE(tmp_value->IsType(Value::TYPE_BOOLEAN));
279 bool bool_value = false;
280 ASSERT_TRUE(tmp_value->GetAsBoolean(&bool_value));
281 EXPECT_TRUE(bool_value);
tc@google.comce6a78d2008-07-29 09:01:31 +0900282
283 // Don't allow empty elements, even if |allow_trailing_comma| is
284 // true.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900285 root.reset(JSONReader::Read("[,]", true));
286 EXPECT_FALSE(root.get());
287 root.reset(JSONReader::Read("[true,,]", true));
288 EXPECT_FALSE(root.get());
289 root.reset(JSONReader::Read("[,true,]", true));
290 EXPECT_FALSE(root.get());
291 root.reset(JSONReader::Read("[true,,false]", true));
292 EXPECT_FALSE(root.get());
tc@google.comce6a78d2008-07-29 09:01:31 +0900293
initial.commit3f4a7322008-07-27 06:49:38 +0900294 // Test objects
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900295 root.reset(JSONReader::Read("{}", false));
296 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900297 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
initial.commit3f4a7322008-07-27 06:49:38 +0900298
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900299 root.reset(JSONReader::Read(
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900300 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", false));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900301 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900302 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900303 DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900304 real_val = 0.0;
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900305 ASSERT_TRUE(dict_val->GetReal(L"number", &real_val));
initial.commit3f4a7322008-07-27 06:49:38 +0900306 ASSERT_DOUBLE_EQ(9.87654321, real_val);
307 Value* null_val = NULL;
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900308 ASSERT_TRUE(dict_val->Get(L"null", &null_val));
initial.commit3f4a7322008-07-27 06:49:38 +0900309 ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL));
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900310 str_val.clear();
311 ASSERT_TRUE(dict_val->GetString(L"S", &str_val));
312 ASSERT_EQ(L"str", str_val);
tc@google.comce6a78d2008-07-29 09:01:31 +0900313
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900314 root2.reset(JSONReader::Read(
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900315 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true));
mark@chromium.org95c9ec92009-06-27 06:17:24 +0900316 ASSERT_TRUE(root2.get());
317 EXPECT_TRUE(root->Equals(root2.get()));
318
319 // Test newline equivalence.
320 root2.reset(JSONReader::Read(
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900321 "{\n"
322 " \"number\":9.87654321,\n"
323 " \"null\":null,\n"
324 " \"\\x53\":\"str\",\n"
325 "}\n", true));
mark@chromium.org95c9ec92009-06-27 06:17:24 +0900326 ASSERT_TRUE(root2.get());
327 EXPECT_TRUE(root->Equals(root2.get()));
328
329 root2.reset(JSONReader::Read(
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900330 "{\r\n"
331 " \"number\":9.87654321,\r\n"
332 " \"null\":null,\r\n"
333 " \"\\x53\":\"str\",\r\n"
334 "}\r\n", true));
mark@chromium.org95c9ec92009-06-27 06:17:24 +0900335 ASSERT_TRUE(root2.get());
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900336 EXPECT_TRUE(root->Equals(root2.get()));
initial.commit3f4a7322008-07-27 06:49:38 +0900337
338 // Test nesting
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900339 root.reset(JSONReader::Read(
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900340 "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", false));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900341 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900342 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900343 dict_val = static_cast<DictionaryValue*>(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900344 DictionaryValue* inner_dict = NULL;
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900345 ASSERT_TRUE(dict_val->GetDictionary(L"inner", &inner_dict));
initial.commit3f4a7322008-07-27 06:49:38 +0900346 ListValue* inner_array = NULL;
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900347 ASSERT_TRUE(inner_dict->GetList(L"array", &inner_array));
darin@google.comf3272802008-08-15 05:27:29 +0900348 ASSERT_EQ(1U, inner_array->GetSize());
tc@google.comce6a78d2008-07-29 09:01:31 +0900349 bool_value = true;
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900350 ASSERT_TRUE(dict_val->GetBoolean(L"false", &bool_value));
initial.commit3f4a7322008-07-27 06:49:38 +0900351 ASSERT_FALSE(bool_value);
352 inner_dict = NULL;
nsylvain@chromium.org12426672009-03-04 07:59:43 +0900353 ASSERT_TRUE(dict_val->GetDictionary(L"d", &inner_dict));
tc@google.comce6a78d2008-07-29 09:01:31 +0900354
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900355 root2.reset(JSONReader::Read(
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900356 "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900357 EXPECT_TRUE(root->Equals(root2.get()));
initial.commit3f4a7322008-07-27 06:49:38 +0900358
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900359 // Test keys with periods
360 root.reset(JSONReader::Read(
361 "{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", false));
362 ASSERT_TRUE(root.get());
363 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
364 dict_val = static_cast<DictionaryValue*>(root.get());
365 int integer_value = 0;
366 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"a.b", &integer_value));
367 EXPECT_EQ(3, integer_value);
368 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"c", &integer_value));
369 EXPECT_EQ(2, integer_value);
370 inner_dict = NULL;
371 ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion(L"d.e.f",
372 &inner_dict));
373 ASSERT_EQ(1U, inner_dict->size());
374 EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion(L"g.h.i.j",
375 &integer_value));
376 EXPECT_EQ(1, integer_value);
377
378 root.reset(JSONReader::Read("{\"a\":{\"b\":2},\"a.b\":1}", false));
379 ASSERT_TRUE(root.get());
380 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
381 dict_val = static_cast<DictionaryValue*>(root.get());
382 EXPECT_TRUE(dict_val->GetInteger(L"a.b", &integer_value));
383 EXPECT_EQ(2, integer_value);
384 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"a.b", &integer_value));
385 EXPECT_EQ(1, integer_value);
386
initial.commit3f4a7322008-07-27 06:49:38 +0900387 // Invalid, no closing brace
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900388 root.reset(JSONReader::Read("{\"a\": true", false));
389 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900390
391 // Invalid, keys must be quoted
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900392 root.reset(JSONReader::Read("{foo:true}", false));
393 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900394
395 // Invalid, trailing comma
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900396 root.reset(JSONReader::Read("{\"a\":true,}", false));
397 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900398
399 // Invalid, too many commas
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900400 root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", false));
401 ASSERT_FALSE(root.get());
402 root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", true));
403 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900404
405 // Invalid, no separator
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900406 root.reset(JSONReader::Read("{\"a\" \"b\"}", false));
407 ASSERT_FALSE(root.get());
tc@google.comce6a78d2008-07-29 09:01:31 +0900408
409 // Invalid, lone comma.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900410 root.reset(JSONReader::Read("{,}", false));
411 ASSERT_FALSE(root.get());
412 root.reset(JSONReader::Read("{,}", true));
413 ASSERT_FALSE(root.get());
414 root.reset(JSONReader::Read("{\"a\":true,,}", true));
415 ASSERT_FALSE(root.get());
416 root.reset(JSONReader::Read("{,\"a\":true}", true));
417 ASSERT_FALSE(root.get());
418 root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", true));
419 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900420
421 // Test stack overflow
initial.commit3f4a7322008-07-27 06:49:38 +0900422 std::string evil(1000000, '[');
423 evil.append(std::string(1000000, ']'));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900424 root.reset(JSONReader::Read(evil, false));
425 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900426
427 // A few thousand adjacent lists is fine.
428 std::string not_evil("[");
429 not_evil.reserve(15010);
430 for (int i = 0; i < 5000; ++i) {
431 not_evil.append("[],");
432 }
433 not_evil.append("[]]");
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900434 root.reset(JSONReader::Read(not_evil, false));
435 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900436 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900437 list = static_cast<ListValue*>(root.get());
darin@google.comf3272802008-08-15 05:27:29 +0900438 ASSERT_EQ(5001U, list->GetSize());
initial.commit3f4a7322008-07-27 06:49:38 +0900439
440 // Test utf8 encoded input
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900441 root.reset(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"",
tc@google.comce6a78d2008-07-29 09:01:31 +0900442 false, false));
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900443 ASSERT_TRUE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900444 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
445 str_val.clear();
446 ASSERT_TRUE(root->GetAsString(&str_val));
447 ASSERT_EQ(L"\x7f51\x9875", str_val);
initial.commit3f4a7322008-07-27 06:49:38 +0900448
jungshik@google.com37790f32008-09-26 06:42:00 +0900449 // Test invalid utf8 encoded input
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900450 root.reset(JSONReader().JsonToValue("\"345\xb0\xa1\xb0\xa2\"",
451 false, false));
452 ASSERT_FALSE(root.get());
453 root.reset(JSONReader().JsonToValue("\"123\xc0\x81\"",
454 false, false));
455 ASSERT_FALSE(root.get());
jungshik@google.com37790f32008-09-26 06:42:00 +0900456
initial.commit3f4a7322008-07-27 06:49:38 +0900457 // Test invalid root objects.
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900458 root.reset(JSONReader::Read("null", false));
459 ASSERT_FALSE(root.get());
460 root.reset(JSONReader::Read("true", false));
461 ASSERT_FALSE(root.get());
462 root.reset(JSONReader::Read("10", false));
463 ASSERT_FALSE(root.get());
464 root.reset(JSONReader::Read("\"root\"", false));
465 ASSERT_FALSE(root.get());
initial.commit3f4a7322008-07-27 06:49:38 +0900466}
aa@chromium.org971901c2008-12-06 07:14:46 +0900467
468TEST(JSONReaderTest, ErrorMessages) {
469 // Error strings should not be modified in case of success.
470 std::string error_message;
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900471 scoped_ptr<Value> root;
472 root.reset(JSONReader::ReadAndReturnError("[42]", false, &error_message));
aa@chromium.org09941d42008-12-24 11:07:48 +0900473 EXPECT_TRUE(error_message.empty());
aa@chromium.org971901c2008-12-06 07:14:46 +0900474
475 // Test line and column counting
476 const char* big_json = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
477 // error here --------------------------------^
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900478 root.reset(JSONReader::ReadAndReturnError(big_json, false, &error_message));
479 EXPECT_FALSE(root.get());
aa@chromium.org09941d42008-12-24 11:07:48 +0900480 EXPECT_EQ(JSONReader::FormatErrorMessage(5, 9, JSONReader::kSyntaxError),
aa@chromium.org971901c2008-12-06 07:14:46 +0900481 error_message);
482
483 // Test each of the error conditions
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900484 root.reset(JSONReader::ReadAndReturnError("{},{}", false, &error_message));
485 EXPECT_FALSE(root.get());
aa@chromium.org09941d42008-12-24 11:07:48 +0900486 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 3,
aa@chromium.org971901c2008-12-06 07:14:46 +0900487 JSONReader::kUnexpectedDataAfterRoot), error_message);
488
489 std::string nested_json;
490 for (int i = 0; i < 101; ++i) {
491 nested_json.insert(nested_json.begin(), '[');
492 nested_json.append(1, ']');
493 }
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900494 root.reset(JSONReader::ReadAndReturnError(nested_json, false,
495 &error_message));
496 EXPECT_FALSE(root.get());
aa@chromium.org09941d42008-12-24 11:07:48 +0900497 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 101, JSONReader::kTooMuchNesting),
aa@chromium.org971901c2008-12-06 07:14:46 +0900498 error_message);
499
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900500 root.reset(JSONReader::ReadAndReturnError("42", false, &error_message));
501 EXPECT_FALSE(root.get());
aa@chromium.org09941d42008-12-24 11:07:48 +0900502 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 1,
aa@chromium.org971901c2008-12-06 07:14:46 +0900503 JSONReader::kBadRootElementType), error_message);
504
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900505 root.reset(JSONReader::ReadAndReturnError("[1,]", false, &error_message));
506 EXPECT_FALSE(root.get());
aa@chromium.org09941d42008-12-24 11:07:48 +0900507 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 4, JSONReader::kTrailingComma),
aa@chromium.org971901c2008-12-06 07:14:46 +0900508 error_message);
509
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900510 root.reset(JSONReader::ReadAndReturnError("{foo:\"bar\"}", false,
511 &error_message));
512 EXPECT_FALSE(root.get());
aa@chromium.org09941d42008-12-24 11:07:48 +0900513 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2,
aa@chromium.org971901c2008-12-06 07:14:46 +0900514 JSONReader::kUnquotedDictionaryKey), error_message);
515
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900516 root.reset(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", false,
517 &error_message));
518 EXPECT_FALSE(root.get());
aa@chromium.org09941d42008-12-24 11:07:48 +0900519 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 14, JSONReader::kTrailingComma),
aa@chromium.org971901c2008-12-06 07:14:46 +0900520 error_message);
521
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900522 root.reset(JSONReader::ReadAndReturnError("[nu]", false, &error_message));
523 EXPECT_FALSE(root.get());
aa@chromium.org09941d42008-12-24 11:07:48 +0900524 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, JSONReader::kSyntaxError),
aa@chromium.org971901c2008-12-06 07:14:46 +0900525 error_message);
526
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900527 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", false,
528 &error_message));
529 EXPECT_FALSE(root.get());
aa@chromium.org09941d42008-12-24 11:07:48 +0900530 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
aa@chromium.org971901c2008-12-06 07:14:46 +0900531 error_message);
532
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900533 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", false,
534 &error_message));
535 EXPECT_FALSE(root.get());
aa@chromium.org09941d42008-12-24 11:07:48 +0900536 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
aa@chromium.org971901c2008-12-06 07:14:46 +0900537 error_message);
538
aa@chromium.orgca9c79e2008-12-30 04:59:08 +0900539 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false,
540 &error_message));
541 EXPECT_FALSE(root.get());
aa@chromium.org09941d42008-12-24 11:07:48 +0900542 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
aa@chromium.org971901c2008-12-06 07:14:46 +0900543 error_message);
aa@chromium.org09941d42008-12-24 11:07:48 +0900544
aa@chromium.org971901c2008-12-06 07:14:46 +0900545}
brettw@chromium.org7cd41eb2009-10-24 05:00:20 +0900546
547} // namespace base