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