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. 4 5 #include "base/json/json_reader.h" 6 7 #include "base/json/json_parser.h" 8 #include "base/logging.h" 9 #include "base/values.h" 10 11 namespace base { 12 13 // Values 1000 and above are used by JSONFileValueSerializer::JsonFileError. 14 static_assert(JSONReader::JSON_PARSE_ERROR_COUNT < 1000, 15 "JSONReader error out of bounds"); 16 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."; 33 34 JSONReader::JSONReader() 35 : JSONReader(JSON_PARSE_RFC) { 36 } 37 38 JSONReader::JSONReader(int options) 39 : parser_(new internal::JSONParser(options)) { 40 } 41 42 JSONReader::~JSONReader() { 43 } 44 45 // static 46 scoped_ptr<Value> JSONReader::Read(const StringPiece& json) { 47 internal::JSONParser parser(JSON_PARSE_RFC); 48 return make_scoped_ptr(parser.Parse(json)); 49 } 50 51 // static 52 scoped_ptr<Value> JSONReader::Read(const StringPiece& json, int options) { 53 internal::JSONParser parser(options); 54 return make_scoped_ptr(parser.Parse(json)); 55 } 56 57 58 // static 59 scoped_ptr<Value> JSONReader::ReadAndReturnError(const StringPiece& json, 60 int options, 61 int* error_code_out, 62 std::string* error_msg_out, 63 int* error_line_out, 64 int* error_column_out) { 65 internal::JSONParser parser(options); 66 scoped_ptr<Value> root(parser.Parse(json)); 67 if (!root) { 68 if (error_code_out) 69 *error_code_out = parser.error_code(); 70 if (error_msg_out) 71 *error_msg_out = parser.GetErrorMessage(); 72 if (error_line_out) 73 *error_line_out = parser.error_line(); 74 if (error_column_out) 75 *error_column_out = parser.error_column(); 76 } 77 78 return root; 79 } 80 81 // static 82 std::string JSONReader::ErrorCodeToString(JsonParseError error_code) { 83 switch (error_code) { 84 case JSON_NO_ERROR: 85 return std::string(); 86 case JSON_INVALID_ESCAPE: 87 return kInvalidEscape; 88 case JSON_SYNTAX_ERROR: 89 return kSyntaxError; 90 case JSON_UNEXPECTED_TOKEN: 91 return kUnexpectedToken; 92 case JSON_TRAILING_COMMA: 93 return kTrailingComma; 94 case JSON_TOO_MUCH_NESTING: 95 return kTooMuchNesting; 96 case JSON_UNEXPECTED_DATA_AFTER_ROOT: 97 return kUnexpectedDataAfterRoot; 98 case JSON_UNSUPPORTED_ENCODING: 99 return kUnsupportedEncoding; 100 case JSON_UNQUOTED_DICTIONARY_KEY: 101 return kUnquotedDictionaryKey; 102 default: 103 NOTREACHED(); 104 return std::string(); 105 } 106 } 107 108 scoped_ptr<Value> JSONReader::ReadToValue(const std::string& json) { 109 return make_scoped_ptr(parser_->Parse(json)); 110 } 111 112 JSONReader::JsonParseError JSONReader::error_code() const { 113 return parser_->error_code(); 114 } 115 116 std::string JSONReader::GetErrorMessage() const { 117 return parser_->GetErrorMessage(); 118 } 119 120 } // namespace base 121