| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/json_reader.h" | 5 #include "base/json_reader.h" |
| 6 | 6 |
| 7 #include "base/float_util.h" | 7 #include "base/float_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 | 13 |
| 13 static const JSONReader::Token kInvalidToken(JSONReader::Token::INVALID_TOKEN, | 14 static const JSONReader::Token kInvalidToken(JSONReader::Token::INVALID_TOKEN, |
| 14 0, 0); | 15 0, 0); |
| 15 static const int kStackLimit = 100; | 16 static const int kStackLimit = 100; |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 inline int HexToInt(wchar_t c) { | 20 inline int HexToInt(wchar_t c) { |
| 20 if ('0' <= c && c <= '9') { | 21 if ('0' <= c && c <= '9') { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 } | 112 } |
| 112 | 113 |
| 113 /* static */ | 114 /* static */ |
| 114 std::string JSONReader::FormatErrorMessage(int line, int column, | 115 std::string JSONReader::FormatErrorMessage(int line, int column, |
| 115 const char* description) { | 116 const char* description) { |
| 116 return StringPrintf("Line: %i, column: %i, %s", | 117 return StringPrintf("Line: %i, column: %i, %s", |
| 117 line, column, description); | 118 line, column, description); |
| 118 } | 119 } |
| 119 | 120 |
| 120 JSONReader::JSONReader() | 121 JSONReader::JSONReader() |
| 121 : start_pos_(NULL), json_pos_(NULL), stack_depth_(0), | 122 : start_pos_(NULL), json_pos_(NULL), stack_depth_(0), |
| 122 allow_trailing_comma_(false) {} | 123 allow_trailing_comma_(false) {} |
| 123 | 124 |
| 124 Value* JSONReader::JsonToValue(const std::string& json, bool check_root, | 125 Value* JSONReader::JsonToValue(const std::string& json, bool check_root, |
| 125 bool allow_trailing_comma) { | 126 bool allow_trailing_comma) { |
| 126 // The input must be in UTF-8. | 127 // The input must be in UTF-8. |
| 127 if (!IsStringUTF8(json.c_str())) { | 128 if (!IsStringUTF8(json.c_str())) { |
| 128 error_message_ = kUnsupportedEncoding; | 129 error_message_ = kUnsupportedEncoding; |
| 129 return NULL; | 130 return NULL; |
| 130 } | 131 } |
| 131 | 132 |
| 132 // The conversion from UTF8 to wstring removes null bytes for us | 133 // The conversion from UTF8 to wstring removes null bytes for us |
| (...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 if (*pos == '\n') { | 629 if (*pos == '\n') { |
| 629 ++line_number; | 630 ++line_number; |
| 630 column_number = 1; | 631 column_number = 1; |
| 631 } else { | 632 } else { |
| 632 ++column_number; | 633 ++column_number; |
| 633 } | 634 } |
| 634 } | 635 } |
| 635 | 636 |
| 636 error_message_ = FormatErrorMessage(line_number, column_number, description); | 637 error_message_ = FormatErrorMessage(line_number, column_number, description); |
| 637 } | 638 } |
| OLD | NEW |