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 } | 16 } |
19 | 17 |
20 JSONStringValueSerializer::JSONStringValueSerializer( | 18 JSONStringValueSerializer::JSONStringValueSerializer( |
Matt Giuca
2015/02/16 00:18:27
You deleted this constructor in the header file, s
| |
21 const base::StringPiece& json_string) | 19 const base::StringPiece& json_string) |
22 : json_string_(nullptr), | 20 : json_string_(nullptr), |
23 json_string_readonly_(json_string), | 21 pretty_print_(false) { |
24 pretty_print_(false), | |
25 allow_trailing_comma_(false) { | |
26 } | 22 } |
27 | 23 |
28 JSONStringValueSerializer::~JSONStringValueSerializer() {} | 24 JSONStringValueSerializer::~JSONStringValueSerializer() {} |
29 | 25 |
30 bool JSONStringValueSerializer::Serialize(const Value& root) { | 26 bool JSONStringValueSerializer::Serialize(const Value& root) { |
31 return SerializeInternal(root, false); | 27 return SerializeInternal(root, false); |
32 } | 28 } |
33 | 29 |
34 bool JSONStringValueSerializer::SerializeAndOmitBinaryValues( | 30 bool JSONStringValueSerializer::SerializeAndOmitBinaryValues( |
35 const Value& root) { | 31 const Value& root) { |
36 return SerializeInternal(root, true); | 32 return SerializeInternal(root, true); |
37 } | 33 } |
38 | 34 |
39 bool JSONStringValueSerializer::SerializeInternal(const Value& root, | 35 bool JSONStringValueSerializer::SerializeInternal(const Value& root, |
40 bool omit_binary_values) { | 36 bool omit_binary_values) { |
41 if (!json_string_) | 37 if (!json_string_) |
42 return false; | 38 return false; |
43 | 39 |
44 int options = 0; | 40 int options = 0; |
45 if (omit_binary_values) | 41 if (omit_binary_values) |
46 options |= base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES; | 42 options |= base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES; |
47 if (pretty_print_) | 43 if (pretty_print_) |
48 options |= base::JSONWriter::OPTIONS_PRETTY_PRINT; | 44 options |= base::JSONWriter::OPTIONS_PRETTY_PRINT; |
49 | 45 |
50 return base::JSONWriter::WriteWithOptions(&root, options, json_string_); | 46 return base::JSONWriter::WriteWithOptions(&root, options, json_string_); |
51 } | 47 } |
52 | 48 |
53 Value* JSONStringValueSerializer::Deserialize(int* error_code, | 49 JSONStringValueDeserializer::JSONStringValueDeserializer( |
50 std::string* json_string) | |
51 : json_string_readonly_(*json_string), | |
52 allow_trailing_comma_(false) { | |
53 } | |
54 | |
55 JSONStringValueDeserializer::JSONStringValueDeserializer( | |
56 const base::StringPiece& json_string) | |
57 : json_string_readonly_(json_string), | |
58 allow_trailing_comma_(false) { | |
59 } | |
60 | |
61 JSONStringValueDeserializer::~JSONStringValueDeserializer() {} | |
62 | |
63 Value* JSONStringValueDeserializer::Deserialize(int* error_code, | |
54 std::string* error_str) { | 64 std::string* error_str) { |
55 return base::JSONReader::ReadAndReturnError(json_string_readonly_, | 65 return base::JSONReader::ReadAndReturnError(json_string_readonly_, |
56 allow_trailing_comma_ ? base::JSON_ALLOW_TRAILING_COMMAS : | 66 allow_trailing_comma_ ? base::JSON_ALLOW_TRAILING_COMMAS : |
57 base::JSON_PARSE_RFC, | 67 base::JSON_PARSE_RFC, |
58 error_code, error_str); | 68 error_code, error_str); |
59 } | 69 } |
OLD | NEW |