| 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_string_value_serializer.h" | 5 #include "base/json/json_string_value_serializer.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 | 10 |
| 11 using base::Value; | 11 using base::Value; |
| 12 | 12 |
| 13 JSONStringValueSerializer::JSONStringValueSerializer(std::string* json_string) | 13 JSONStringValueSerializer::JSONStringValueSerializer(std::string* json_string) |
| 14 : json_string_(json_string), | 14 : json_string_(json_string), |
| 15 json_string_readonly_(*json_string), | 15 pretty_print_(false) { |
| 16 pretty_print_(false), | |
| 17 allow_trailing_comma_(false) { | |
| 18 } | |
| 19 | |
| 20 JSONStringValueSerializer::JSONStringValueSerializer( | |
| 21 const base::StringPiece& json_string) | |
| 22 : json_string_(nullptr), | |
| 23 json_string_readonly_(json_string), | |
| 24 pretty_print_(false), | |
| 25 allow_trailing_comma_(false) { | |
| 26 } | 16 } |
| 27 | 17 |
| 28 JSONStringValueSerializer::~JSONStringValueSerializer() {} | 18 JSONStringValueSerializer::~JSONStringValueSerializer() {} |
| 29 | 19 |
| 30 bool JSONStringValueSerializer::Serialize(const Value& root) { | 20 bool JSONStringValueSerializer::Serialize(const Value& root) { |
| 31 return SerializeInternal(root, false); | 21 return SerializeInternal(root, false); |
| 32 } | 22 } |
| 33 | 23 |
| 34 bool JSONStringValueSerializer::SerializeAndOmitBinaryValues( | 24 bool JSONStringValueSerializer::SerializeAndOmitBinaryValues( |
| 35 const Value& root) { | 25 const Value& root) { |
| 36 return SerializeInternal(root, true); | 26 return SerializeInternal(root, true); |
| 37 } | 27 } |
| 38 | 28 |
| 39 bool JSONStringValueSerializer::SerializeInternal(const Value& root, | 29 bool JSONStringValueSerializer::SerializeInternal(const Value& root, |
| 40 bool omit_binary_values) { | 30 bool omit_binary_values) { |
| 41 if (!json_string_) | 31 if (!json_string_) |
| 42 return false; | 32 return false; |
| 43 | 33 |
| 44 int options = 0; | 34 int options = 0; |
| 45 if (omit_binary_values) | 35 if (omit_binary_values) |
| 46 options |= base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES; | 36 options |= base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES; |
| 47 if (pretty_print_) | 37 if (pretty_print_) |
| 48 options |= base::JSONWriter::OPTIONS_PRETTY_PRINT; | 38 options |= base::JSONWriter::OPTIONS_PRETTY_PRINT; |
| 49 | 39 |
| 50 return base::JSONWriter::WriteWithOptions(&root, options, json_string_); | 40 return base::JSONWriter::WriteWithOptions(&root, options, json_string_); |
| 51 } | 41 } |
| 52 | 42 |
| 53 Value* JSONStringValueSerializer::Deserialize(int* error_code, | 43 JSONStringValueDeserializer::JSONStringValueDeserializer( |
| 54 std::string* error_str) { | 44 const base::StringPiece& json_string) |
| 55 return base::JSONReader::ReadAndReturnError(json_string_readonly_, | 45 : json_string_(json_string), |
| 46 allow_trailing_comma_(false) { |
| 47 } |
| 48 |
| 49 JSONStringValueDeserializer::~JSONStringValueDeserializer() {} |
| 50 |
| 51 Value* JSONStringValueDeserializer::Deserialize(int* error_code, |
| 52 std::string* error_str) { |
| 53 return base::JSONReader::ReadAndReturnError(json_string_, |
| 56 allow_trailing_comma_ ? base::JSON_ALLOW_TRAILING_COMMAS : | 54 allow_trailing_comma_ ? base::JSON_ALLOW_TRAILING_COMMAS : |
| 57 base::JSON_PARSE_RFC, | 55 base::JSON_PARSE_RFC, |
| 58 error_code, error_str); | 56 error_code, error_str); |
| 59 } | 57 } |
| OLD | NEW |