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
3 // found in the LICENSE file.
5 #include "base/json/json_string_value_serializer.h"
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/logging.h"
13 JSONStringValueSerializer::JSONStringValueSerializer(std::string
* json_string
)
14 : json_string_(json_string
),
15 json_string_readonly_(*json_string
),
17 allow_trailing_comma_(false) {
20 JSONStringValueSerializer::JSONStringValueSerializer(
21 const base::StringPiece
& json_string
)
22 : json_string_(nullptr),
23 json_string_readonly_(json_string
),
25 allow_trailing_comma_(false) {
28 JSONStringValueSerializer::~JSONStringValueSerializer() {}
30 bool JSONStringValueSerializer::Serialize(const Value
& root
) {
31 return SerializeInternal(root
, false);
34 bool JSONStringValueSerializer::SerializeAndOmitBinaryValues(
36 return SerializeInternal(root
, true);
39 bool JSONStringValueSerializer::SerializeInternal(const Value
& root
,
40 bool omit_binary_values
) {
45 if (omit_binary_values
)
46 options
|= base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES
;
48 options
|= base::JSONWriter::OPTIONS_PRETTY_PRINT
;
50 return base::JSONWriter::WriteWithOptions(&root
, options
, json_string_
);
53 Value
* JSONStringValueSerializer::Deserialize(int* error_code
,
54 std::string
* error_str
) {
55 return base::JSONReader::ReadAndReturnError(json_string_readonly_
,
56 allow_trailing_comma_
? base::JSON_ALLOW_TRAILING_COMMAS
:
58 error_code
, error_str
);