Revert of Re-enabled contenteditable and text-changed tests after content editable...
[chromium-blink-merge.git] / base / json / json_reader.cc
blobad3ea98b31422b2ec98494d497487da36744f214
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"
9 #include "base/values.h"
11 namespace base {
13 // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError.
14 COMPILE_ASSERT(JSONReader::JSON_PARSE_ERROR_COUNT < 1000,
15 json_reader_error_out_of_bounds);
17 const char JSONReader::kInvalidEscape[] =
18 "Invalid escape sequence.";
19 const char JSONReader::kSyntaxError[] =
20 "Syntax error.";
21 const char JSONReader::kUnexpectedToken[] =
22 "Unexpected token.";
23 const char JSONReader::kTrailingComma[] =
24 "Trailing comma not allowed.";
25 const char JSONReader::kTooMuchNesting[] =
26 "Too much nesting.";
27 const char JSONReader::kUnexpectedDataAfterRoot[] =
28 "Unexpected data after root element.";
29 const char JSONReader::kUnsupportedEncoding[] =
30 "Unsupported encoding. JSON must be UTF-8.";
31 const char JSONReader::kUnquotedDictionaryKey[] =
32 "Dictionary keys must be quoted.";
34 JSONReader::JSONReader()
35 : JSONReader(JSON_PARSE_RFC) {
38 JSONReader::JSONReader(int options)
39 : parser_(new internal::JSONParser(options)) {
42 JSONReader::~JSONReader() {
45 // static
46 Value* JSONReader::DeprecatedRead(const StringPiece& json) {
47 return Read(json).release();
50 // static
51 scoped_ptr<Value> JSONReader::Read(const StringPiece& json) {
52 internal::JSONParser parser(JSON_PARSE_RFC);
53 return make_scoped_ptr(parser.Parse(json));
56 // static
57 Value* JSONReader::DeprecatedRead(const StringPiece& json, int options) {
58 return Read(json, options).release();
61 // static
62 scoped_ptr<Value> JSONReader::Read(const StringPiece& json, int options) {
63 internal::JSONParser parser(options);
64 return make_scoped_ptr(parser.Parse(json));
67 // static
68 Value* JSONReader::DeprecatedReadAndReturnError(const StringPiece& json,
69 int options,
70 int* error_code_out,
71 std::string* error_msg_out) {
72 return ReadAndReturnError(json, options, error_code_out, error_msg_out)
73 .release();
76 // static
77 scoped_ptr<Value> JSONReader::ReadAndReturnError(const StringPiece& json,
78 int options,
79 int* error_code_out,
80 std::string* error_msg_out) {
81 internal::JSONParser parser(options);
82 scoped_ptr<Value> root(parser.Parse(json));
83 if (!root) {
84 if (error_code_out)
85 *error_code_out = parser.error_code();
86 if (error_msg_out)
87 *error_msg_out = parser.GetErrorMessage();
90 return root;
93 // static
94 std::string JSONReader::ErrorCodeToString(JsonParseError error_code) {
95 switch (error_code) {
96 case JSON_NO_ERROR:
97 return std::string();
98 case JSON_INVALID_ESCAPE:
99 return kInvalidEscape;
100 case JSON_SYNTAX_ERROR:
101 return kSyntaxError;
102 case JSON_UNEXPECTED_TOKEN:
103 return kUnexpectedToken;
104 case JSON_TRAILING_COMMA:
105 return kTrailingComma;
106 case JSON_TOO_MUCH_NESTING:
107 return kTooMuchNesting;
108 case JSON_UNEXPECTED_DATA_AFTER_ROOT:
109 return kUnexpectedDataAfterRoot;
110 case JSON_UNSUPPORTED_ENCODING:
111 return kUnsupportedEncoding;
112 case JSON_UNQUOTED_DICTIONARY_KEY:
113 return kUnquotedDictionaryKey;
114 default:
115 NOTREACHED();
116 return std::string();
120 scoped_ptr<Value> JSONReader::ReadToValue(const std::string& json) {
121 return make_scoped_ptr(parser_->Parse(json));
124 JSONReader::JsonParseError JSONReader::error_code() const {
125 return parser_->error_code();
128 std::string JSONReader::GetErrorMessage() const {
129 return parser_->GetErrorMessage();
132 } // namespace base