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) |
| 14 : json_string_(json_string), |
| 15 initialized_with_const_string_(false), |
| 16 pretty_print_(false), |
| 17 allow_trailing_comma_(false) { |
| 18 DCHECK(json_string); |
| 19 } |
| 20 |
| 21 JSONStringValueSerializer::JSONStringValueSerializer( |
| 22 const std::string& json_string) |
| 23 : json_string_(&const_cast<std::string&>(json_string)), |
| 24 initialized_with_const_string_(true), |
| 25 pretty_print_(false), |
| 26 allow_trailing_comma_(false) { |
| 27 } |
| 28 |
13 JSONStringValueSerializer::~JSONStringValueSerializer() {} | 29 JSONStringValueSerializer::~JSONStringValueSerializer() {} |
14 | 30 |
15 bool JSONStringValueSerializer::Serialize(const Value& root) { | 31 bool JSONStringValueSerializer::Serialize(const Value& root) { |
16 return SerializeInternal(root, false); | 32 return SerializeInternal(root, false); |
17 } | 33 } |
18 | 34 |
19 bool JSONStringValueSerializer::SerializeAndOmitBinaryValues( | 35 bool JSONStringValueSerializer::SerializeAndOmitBinaryValues( |
20 const Value& root) { | 36 const Value& root) { |
21 return SerializeInternal(root, true); | 37 return SerializeInternal(root, true); |
22 } | 38 } |
23 | 39 |
24 bool JSONStringValueSerializer::SerializeInternal(const Value& root, | 40 bool JSONStringValueSerializer::SerializeInternal(const Value& root, |
25 bool omit_binary_values) { | 41 bool omit_binary_values) { |
26 if (!json_string_ || initialized_with_const_string_) | 42 if (initialized_with_const_string_) |
27 return false; | 43 return false; |
28 | 44 |
29 int options = 0; | 45 int options = 0; |
30 if (omit_binary_values) | 46 if (omit_binary_values) |
31 options |= base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES; | 47 options |= base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES; |
32 if (pretty_print_) | 48 if (pretty_print_) |
33 options |= base::JSONWriter::OPTIONS_PRETTY_PRINT; | 49 options |= base::JSONWriter::OPTIONS_PRETTY_PRINT; |
34 | 50 |
35 return base::JSONWriter::WriteWithOptions(&root, options, json_string_); | 51 return base::JSONWriter::WriteWithOptions(&root, options, json_string_); |
36 } | 52 } |
37 | 53 |
38 Value* JSONStringValueSerializer::Deserialize(int* error_code, | 54 Value* JSONStringValueSerializer::Deserialize(int* error_code, |
39 std::string* error_str) { | 55 std::string* error_str) { |
40 if (!json_string_) | |
41 return NULL; | |
42 | |
43 return base::JSONReader::ReadAndReturnError(*json_string_, | 56 return base::JSONReader::ReadAndReturnError(*json_string_, |
44 allow_trailing_comma_ ? base::JSON_ALLOW_TRAILING_COMMAS : | 57 allow_trailing_comma_ ? base::JSON_ALLOW_TRAILING_COMMAS : |
45 base::JSON_PARSE_RFC, | 58 base::JSON_PARSE_RFC, |
46 error_code, error_str); | 59 error_code, error_str); |
47 } | 60 } |
OLD | NEW |