OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/json_schema_validator.h" | 5 #include "chrome/common/json_schema_validator.h" |
6 | 6 |
7 #include <cfloat> | 7 #include <cfloat> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 double GetNumberValue(Value* value) { | 17 double GetNumberValue(Value* value) { |
18 double result = 0; | 18 double result = 0; |
19 if (value->GetAsReal(&result)) | 19 if (value->GetAsDouble(&result)) |
20 return result; | 20 return result; |
21 | 21 |
22 int int_result = 0; | 22 int int_result = 0; |
23 if (value->GetAsInteger(&int_result)) { | 23 if (value->GetAsInteger(&int_result)) { |
24 return int_result; | 24 return int_result; |
25 } | 25 } |
26 | 26 |
27 CHECK(false) << "Unexpected value type: " << value->GetType(); | 27 CHECK(false) << "Unexpected value type: " << value->GetType(); |
28 return 0; | 28 return 0; |
29 } | 29 } |
30 | 30 |
31 bool GetNumberFromDictionary(DictionaryValue* value, const std::string& key, | 31 bool GetNumberFromDictionary(DictionaryValue* value, const std::string& key, |
32 double* number) { | 32 double* number) { |
33 if (value->GetReal(key, number)) | 33 if (value->GetDouble(key, number)) |
34 return true; | 34 return true; |
35 | 35 |
36 int int_value = 0; | 36 int int_value = 0; |
37 if (value->GetInteger(key, &int_value)) { | 37 if (value->GetInteger(key, &int_value)) { |
38 *number = int_value; | 38 *number = int_value; |
39 return true; | 39 return true; |
40 } | 40 } |
41 | 41 |
42 return false; | 42 return false; |
43 } | 43 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 90 |
91 // static | 91 // static |
92 std::string JSONSchemaValidator::GetJSONSchemaType(Value* value) { | 92 std::string JSONSchemaValidator::GetJSONSchemaType(Value* value) { |
93 switch (value->GetType()) { | 93 switch (value->GetType()) { |
94 case Value::TYPE_NULL: | 94 case Value::TYPE_NULL: |
95 return "null"; | 95 return "null"; |
96 case Value::TYPE_BOOLEAN: | 96 case Value::TYPE_BOOLEAN: |
97 return "boolean"; | 97 return "boolean"; |
98 case Value::TYPE_INTEGER: | 98 case Value::TYPE_INTEGER: |
99 return "integer"; | 99 return "integer"; |
100 case Value::TYPE_REAL: { | 100 case Value::TYPE_DOUBLE: { |
101 double double_value = 0; | 101 double double_value = 0; |
102 value->GetAsReal(&double_value); | 102 value->GetAsDouble(&double_value); |
103 if (std::abs(double_value) <= std::pow(2.0, DBL_MANT_DIG) && | 103 if (std::abs(double_value) <= std::pow(2.0, DBL_MANT_DIG) && |
104 double_value == floor(double_value)) { | 104 double_value == floor(double_value)) { |
105 return "integer"; | 105 return "integer"; |
106 } else { | 106 } else { |
107 return "number"; | 107 return "number"; |
108 } | 108 } |
109 } | 109 } |
110 case Value::TYPE_STRING: | 110 case Value::TYPE_STRING: |
111 return "string"; | 111 return "string"; |
112 case Value::TYPE_DICTIONARY: | 112 case Value::TYPE_DICTIONARY: |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 CHECK(choices->Get(i, &choice)); | 262 CHECK(choices->Get(i, &choice)); |
263 switch (choice->GetType()) { | 263 switch (choice->GetType()) { |
264 case Value::TYPE_NULL: | 264 case Value::TYPE_NULL: |
265 case Value::TYPE_BOOLEAN: | 265 case Value::TYPE_BOOLEAN: |
266 case Value::TYPE_STRING: | 266 case Value::TYPE_STRING: |
267 if (instance->Equals(choice)) | 267 if (instance->Equals(choice)) |
268 return; | 268 return; |
269 break; | 269 break; |
270 | 270 |
271 case Value::TYPE_INTEGER: | 271 case Value::TYPE_INTEGER: |
272 case Value::TYPE_REAL: | 272 case Value::TYPE_DOUBLE: |
273 if (instance->IsType(Value::TYPE_INTEGER) || | 273 if (instance->IsType(Value::TYPE_INTEGER) || |
274 instance->IsType(Value::TYPE_REAL)) { | 274 instance->IsType(Value::TYPE_DOUBLE)) { |
275 if (GetNumberValue(choice) == GetNumberValue(instance)) | 275 if (GetNumberValue(choice) == GetNumberValue(instance)) |
276 return; | 276 return; |
277 } | 277 } |
278 break; | 278 break; |
279 | 279 |
280 default: | 280 default: |
281 CHECK(false) << "Unexpected type in enum: " << choice->GetType(); | 281 CHECK(false) << "Unexpected type in enum: " << choice->GetType(); |
282 } | 282 } |
283 } | 283 } |
284 | 284 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 | 495 |
496 if (*additional_properties_schema) { | 496 if (*additional_properties_schema) { |
497 std::string additional_properties_type("any"); | 497 std::string additional_properties_type("any"); |
498 CHECK((*additional_properties_schema)->GetString( | 498 CHECK((*additional_properties_schema)->GetString( |
499 "type", &additional_properties_type)); | 499 "type", &additional_properties_type)); |
500 return additional_properties_type == "any"; | 500 return additional_properties_type == "any"; |
501 } else { | 501 } else { |
502 return default_allow_additional_properties_; | 502 return default_allow_additional_properties_; |
503 } | 503 } |
504 } | 504 } |
OLD | NEW |