• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "base/json/json_file_value_serializer.h"
6 
7 #include "base/files/file_util.h"
8 #include "base/json/json_string_value_serializer.h"
9 #include "base/logging.h"
10 #include "build/build_config.h"
11 
12 using base::FilePath;
13 
14 const char JSONFileValueDeserializer::kAccessDenied[] = "Access denied.";
15 const char JSONFileValueDeserializer::kCannotReadFile[] = "Can't read file.";
16 const char JSONFileValueDeserializer::kFileLocked[] = "File locked.";
17 const char JSONFileValueDeserializer::kNoSuchFile[] = "File doesn't exist.";
18 
JSONFileValueSerializer(const base::FilePath & json_file_path)19 JSONFileValueSerializer::JSONFileValueSerializer(
20     const base::FilePath& json_file_path)
21     : json_file_path_(json_file_path) {
22 }
23 
~JSONFileValueSerializer()24 JSONFileValueSerializer::~JSONFileValueSerializer() {
25 }
26 
Serialize(const base::Value & root)27 bool JSONFileValueSerializer::Serialize(const base::Value& root) {
28   return SerializeInternal(root, false);
29 }
30 
SerializeAndOmitBinaryValues(const base::Value & root)31 bool JSONFileValueSerializer::SerializeAndOmitBinaryValues(
32     const base::Value& root) {
33   return SerializeInternal(root, true);
34 }
35 
SerializeInternal(const base::Value & root,bool omit_binary_values)36 bool JSONFileValueSerializer::SerializeInternal(const base::Value& root,
37                                                 bool omit_binary_values) {
38   std::string json_string;
39   JSONStringValueSerializer serializer(&json_string);
40   serializer.set_pretty_print(true);
41   bool result = omit_binary_values ?
42       serializer.SerializeAndOmitBinaryValues(root) :
43       serializer.Serialize(root);
44   if (!result)
45     return false;
46 
47   int data_size = static_cast<int>(json_string.size());
48   if (base::WriteFile(json_file_path_, json_string.data(), data_size) !=
49       data_size)
50     return false;
51 
52   return true;
53 }
54 
JSONFileValueDeserializer(const base::FilePath & json_file_path,int options)55 JSONFileValueDeserializer::JSONFileValueDeserializer(
56     const base::FilePath& json_file_path,
57     int options)
58     : json_file_path_(json_file_path), options_(options), last_read_size_(0U) {}
59 
~JSONFileValueDeserializer()60 JSONFileValueDeserializer::~JSONFileValueDeserializer() {
61 }
62 
ReadFileToString(std::string * json_string)63 int JSONFileValueDeserializer::ReadFileToString(std::string* json_string) {
64   DCHECK(json_string);
65   if (!base::ReadFileToString(json_file_path_, json_string)) {
66 #if defined(OS_WIN)
67     int error = ::GetLastError();
68     if (error == ERROR_SHARING_VIOLATION || error == ERROR_LOCK_VIOLATION) {
69       return JSON_FILE_LOCKED;
70     } else if (error == ERROR_ACCESS_DENIED) {
71       return JSON_ACCESS_DENIED;
72     }
73 #endif
74     if (!base::PathExists(json_file_path_))
75       return JSON_NO_SUCH_FILE;
76     else
77       return JSON_CANNOT_READ_FILE;
78   }
79 
80   last_read_size_ = json_string->size();
81   return JSON_NO_ERROR;
82 }
83 
GetErrorMessageForCode(int error_code)84 const char* JSONFileValueDeserializer::GetErrorMessageForCode(int error_code) {
85   switch (error_code) {
86     case JSON_NO_ERROR:
87       return "";
88     case JSON_ACCESS_DENIED:
89       return kAccessDenied;
90     case JSON_CANNOT_READ_FILE:
91       return kCannotReadFile;
92     case JSON_FILE_LOCKED:
93       return kFileLocked;
94     case JSON_NO_SUCH_FILE:
95       return kNoSuchFile;
96     default:
97       NOTREACHED();
98       return "";
99   }
100 }
101 
Deserialize(int * error_code,std::string * error_str)102 std::unique_ptr<base::Value> JSONFileValueDeserializer::Deserialize(
103     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) {
108     if (error_code)
109       *error_code = error;
110     if (error_str)
111       *error_str = GetErrorMessageForCode(error);
112     return NULL;
113   }
114 
115   JSONStringValueDeserializer deserializer(json_string, options_);
116   return deserializer.Deserialize(error_code, error_str);
117 }
118