| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/json_reader.h" | 5 #include "base/json/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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 : start_pos_(NULL), | 86 : start_pos_(NULL), |
| 87 json_pos_(NULL), | 87 json_pos_(NULL), |
| 88 end_pos_(NULL), | 88 end_pos_(NULL), |
| 89 stack_depth_(0), | 89 stack_depth_(0), |
| 90 allow_trailing_comma_(false), | 90 allow_trailing_comma_(false), |
| 91 error_code_(JSON_NO_ERROR), | 91 error_code_(JSON_NO_ERROR), |
| 92 error_line_(0), | 92 error_line_(0), |
| 93 error_col_(0) {} | 93 error_col_(0) {} |
| 94 | 94 |
| 95 // static | 95 // static |
| 96 Value* JSONReader::Read(const std::string& json, | 96 Value* JSONReader::Read(const StringPiece& json, |
| 97 bool allow_trailing_comma) { | 97 bool allow_trailing_comma) { |
| 98 return ReadAndReturnError(json, allow_trailing_comma, NULL, NULL); | 98 return ReadAndReturnError(json, allow_trailing_comma, NULL, NULL); |
| 99 } | 99 } |
| 100 | 100 |
| 101 // static | 101 // static |
| 102 Value* JSONReader::ReadAndReturnError(const std::string& json, | 102 Value* JSONReader::ReadAndReturnError(const StringPiece& json, |
| 103 bool allow_trailing_comma, | 103 bool allow_trailing_comma, |
| 104 int* error_code_out, | 104 int* error_code_out, |
| 105 std::string* error_msg_out) { | 105 std::string* error_msg_out) { |
| 106 JSONReader reader = JSONReader(); | 106 JSONReader reader = JSONReader(); |
| 107 Value* root = reader.JsonToValue(json, true, allow_trailing_comma); | 107 Value* root = reader.JsonToValue(json, true, allow_trailing_comma); |
| 108 if (root) | 108 if (root) |
| 109 return root; | 109 return root; |
| 110 | 110 |
| 111 if (error_code_out) | 111 if (error_code_out) |
| 112 *error_code_out = reader.error_code(); | 112 *error_code_out = reader.error_code(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 141 NOTREACHED(); | 141 NOTREACHED(); |
| 142 return std::string(); | 142 return std::string(); |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 std::string JSONReader::GetErrorMessage() const { | 146 std::string JSONReader::GetErrorMessage() const { |
| 147 return FormatErrorMessage(error_line_, error_col_, | 147 return FormatErrorMessage(error_line_, error_col_, |
| 148 ErrorCodeToString(error_code_)); | 148 ErrorCodeToString(error_code_)); |
| 149 } | 149 } |
| 150 | 150 |
| 151 Value* JSONReader::JsonToValue(const std::string& json, bool check_root, | 151 Value* JSONReader::JsonToValue(const StringPiece& json, bool check_root, |
| 152 bool allow_trailing_comma) { | 152 bool allow_trailing_comma) { |
| 153 // The input must be in UTF-8. | 153 // The input must be in UTF-8. |
| 154 if (!IsStringUTF8(json.data())) { | 154 if (!IsStringUTF8(json.data())) { |
| 155 error_code_ = JSON_UNSUPPORTED_ENCODING; | 155 error_code_ = JSON_UNSUPPORTED_ENCODING; |
| 156 return NULL; | 156 return NULL; |
| 157 } | 157 } |
| 158 | 158 |
| 159 start_pos_ = json.data(); | 159 start_pos_ = json.data(); |
| 160 end_pos_ = start_pos_ + json.size(); | 160 end_pos_ = start_pos_ + json.size(); |
| 161 | 161 |
| (...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 ++column_number; | 719 ++column_number; |
| 720 } | 720 } |
| 721 } | 721 } |
| 722 | 722 |
| 723 error_line_ = line_number; | 723 error_line_ = line_number; |
| 724 error_col_ = column_number; | 724 error_col_ = column_number; |
| 725 error_code_ = error; | 725 error_code_ = error; |
| 726 } | 726 } |
| 727 | 727 |
| 728 } // namespace base | 728 } // namespace base |
| OLD | NEW |