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
10namespace base {
11
12// Values 1000 and above are used by JSONFileValueSerializer::JsonFileError.
13COMPILE_ASSERT(JSONReader::JSON_PARSE_ERROR_COUNT < 1000,
14               json_reader_error_out_of_bounds);
15
16const char* JSONReader::kInvalidEscape =
17    "Invalid escape sequence.";
18const char* JSONReader::kSyntaxError =
19    "Syntax error.";
20const char* JSONReader::kUnexpectedToken =
21    "Unexpected token.";
22const char* JSONReader::kTrailingComma =
23    "Trailing comma not allowed.";
24const char* JSONReader::kTooMuchNesting =
25    "Too much nesting.";
26const char* JSONReader::kUnexpectedDataAfterRoot =
27    "Unexpected data after root element.";
28const char* JSONReader::kUnsupportedEncoding =
29    "Unsupported encoding. JSON must be UTF-8.";
30const char* JSONReader::kUnquotedDictionaryKey =
31    "Dictionary keys must be quoted.";
32
33JSONReader::JSONReader()
34    : parser_(new internal::JSONParser(JSON_PARSE_RFC)) {
35}
36
37JSONReader::JSONReader(int options)
38    : parser_(new internal::JSONParser(options)) {
39}
40
41JSONReader::~JSONReader() {
42}
43
44// static
45Value* JSONReader::Read(const StringPiece& json) {
46  internal::JSONParser parser(JSON_PARSE_RFC);
47  return parser.Parse(json);
48}
49
50// static
51Value* JSONReader::Read(const StringPiece& json,
52                        int options) {
53  internal::JSONParser parser(options);
54  return parser.Parse(json);
55}
56
57// static
58Value* 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;
66
67  if (error_code_out)
68    *error_code_out = parser.error_code();
69  if (error_msg_out)
70    *error_msg_out = parser.GetErrorMessage();
71
72  return NULL;
73}
74
75// static
76std::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();
99  }
100}
101
102Value* JSONReader::ReadToValue(const std::string& json) {
103  return parser_->Parse(json);
104}
105
106JSONReader::JsonParseError JSONReader::error_code() const {
107  return parser_->error_code();
108}
109
110std::string JSONReader::GetErrorMessage() const {
111  return parser_->GetErrorMessage();
112}
113
114}  // namespace base
115