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