Update V8 to version 4.3.57.1 (cherry-pick).
[chromium-blink-merge.git] / base / json / json_reader.cc
blobe17d450ebfae57cf065dc0ac205494de87e5f3b3
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/json/json_reader.h"
7 #include "base/json/json_parser.h"
8 #include "base/logging.h"
10 namespace base {
12 // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError.
13 COMPILE_ASSERT(JSONReader::JSON_PARSE_ERROR_COUNT < 1000,
14 json_reader_error_out_of_bounds);
16 const char JSONReader::kInvalidEscape[] =
17 "Invalid escape sequence.";
18 const char JSONReader::kSyntaxError[] =
19 "Syntax error.";
20 const char JSONReader::kUnexpectedToken[] =
21 "Unexpected token.";
22 const char JSONReader::kTrailingComma[] =
23 "Trailing comma not allowed.";
24 const char JSONReader::kTooMuchNesting[] =
25 "Too much nesting.";
26 const char JSONReader::kUnexpectedDataAfterRoot[] =
27 "Unexpected data after root element.";
28 const char JSONReader::kUnsupportedEncoding[] =
29 "Unsupported encoding. JSON must be UTF-8.";
30 const char JSONReader::kUnquotedDictionaryKey[] =
31 "Dictionary keys must be quoted.";
33 JSONReader::JSONReader()
34 : JSONReader(JSON_PARSE_RFC) {
37 JSONReader::JSONReader(int options)
38 : parser_(new internal::JSONParser(options)) {
41 JSONReader::~JSONReader() {
44 // static
45 Value* JSONReader::Read(const StringPiece& json) {
46 internal::JSONParser parser(JSON_PARSE_RFC);
47 return parser.Parse(json);
50 // static
51 Value* JSONReader::Read(const StringPiece& json,
52 int options) {
53 internal::JSONParser parser(options);
54 return parser.Parse(json);
57 // static
58 Value* JSONReader::ReadAndReturnError(const StringPiece& json,
59 int options,
60 int* error_code_out,
61 std::string* error_msg_out) {
62 internal::JSONParser parser(options);
63 Value* root = parser.Parse(json);
64 if (root)
65 return root;
67 if (error_code_out)
68 *error_code_out = parser.error_code();
69 if (error_msg_out)
70 *error_msg_out = parser.GetErrorMessage();
72 return NULL;
75 // static
76 std::string JSONReader::ErrorCodeToString(JsonParseError error_code) {
77 switch (error_code) {
78 case JSON_NO_ERROR:
79 return std::string();
80 case JSON_INVALID_ESCAPE:
81 return kInvalidEscape;
82 case JSON_SYNTAX_ERROR:
83 return kSyntaxError;
84 case JSON_UNEXPECTED_TOKEN:
85 return kUnexpectedToken;
86 case JSON_TRAILING_COMMA:
87 return kTrailingComma;
88 case JSON_TOO_MUCH_NESTING:
89 return kTooMuchNesting;
90 case JSON_UNEXPECTED_DATA_AFTER_ROOT:
91 return kUnexpectedDataAfterRoot;
92 case JSON_UNSUPPORTED_ENCODING:
93 return kUnsupportedEncoding;
94 case JSON_UNQUOTED_DICTIONARY_KEY:
95 return kUnquotedDictionaryKey;
96 default:
97 NOTREACHED();
98 return std::string();
102 Value* JSONReader::ReadToValue(const std::string& json) {
103 return parser_->Parse(json);
106 JSONReader::JsonParseError JSONReader::error_code() const {
107 return parser_->error_code();
110 std::string JSONReader::GetErrorMessage() const {
111 return parser_->GetErrorMessage();
114 } // namespace base