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_file_value_serializer.h"
7 #include "base/files/file_util.h"
8 #include "base/json/json_string_value_serializer.h"
9 #include "base/logging.h"
13 const char JSONFileValueDeserializer::kAccessDenied
[] = "Access denied.";
14 const char JSONFileValueDeserializer::kCannotReadFile
[] = "Can't read file.";
15 const char JSONFileValueDeserializer::kFileLocked
[] = "File locked.";
16 const char JSONFileValueDeserializer::kNoSuchFile
[] = "File doesn't exist.";
18 JSONFileValueSerializer::JSONFileValueSerializer(
19 const base::FilePath
& json_file_path
)
20 : json_file_path_(json_file_path
) {
23 JSONFileValueSerializer::~JSONFileValueSerializer() {
26 bool JSONFileValueSerializer::Serialize(const base::Value
& root
) {
27 return SerializeInternal(root
, false);
30 bool JSONFileValueSerializer::SerializeAndOmitBinaryValues(
31 const base::Value
& root
) {
32 return SerializeInternal(root
, true);
35 bool JSONFileValueSerializer::SerializeInternal(const base::Value
& root
,
36 bool omit_binary_values
) {
37 std::string json_string
;
38 JSONStringValueSerializer
serializer(&json_string
);
39 serializer
.set_pretty_print(true);
40 bool result
= omit_binary_values
?
41 serializer
.SerializeAndOmitBinaryValues(root
) :
42 serializer
.Serialize(root
);
46 int data_size
= static_cast<int>(json_string
.size());
47 if (base::WriteFile(json_file_path_
, json_string
.data(), data_size
) !=
54 JSONFileValueDeserializer::JSONFileValueDeserializer(
55 const base::FilePath
& json_file_path
)
56 : json_file_path_(json_file_path
),
57 allow_trailing_comma_(false),
61 JSONFileValueDeserializer::~JSONFileValueDeserializer() {
64 int JSONFileValueDeserializer::ReadFileToString(std::string
* json_string
) {
66 if (!base::ReadFileToString(json_file_path_
, json_string
)) {
68 int error
= ::GetLastError();
69 if (error
== ERROR_SHARING_VIOLATION
|| error
== ERROR_LOCK_VIOLATION
) {
70 return JSON_FILE_LOCKED
;
71 } else if (error
== ERROR_ACCESS_DENIED
) {
72 return JSON_ACCESS_DENIED
;
75 if (!base::PathExists(json_file_path_
))
76 return JSON_NO_SUCH_FILE
;
78 return JSON_CANNOT_READ_FILE
;
81 last_read_size_
= json_string
->size();
85 const char* JSONFileValueDeserializer::GetErrorMessageForCode(int error_code
) {
89 case JSON_ACCESS_DENIED
:
91 case JSON_CANNOT_READ_FILE
:
92 return kCannotReadFile
;
93 case JSON_FILE_LOCKED
:
95 case JSON_NO_SUCH_FILE
:
103 base::Value
* JSONFileValueDeserializer::Deserialize(int* error_code
,
104 std::string
* error_str
) {
105 std::string json_string
;
106 int error
= ReadFileToString(&json_string
);
107 if (error
!= JSON_NO_ERROR
) {
111 *error_str
= GetErrorMessageForCode(error
);
115 JSONStringValueDeserializer
deserializer(json_string
);
116 deserializer
.set_allow_trailing_comma(allow_trailing_comma_
);
117 return deserializer
.Deserialize(error_code
, error_str
);