OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef BASE_JSON_JSON_VALUE_SERIALIZER_H_ | 5 #ifndef BASE_JSON_JSON_VALUE_SERIALIZER_H_ |
6 #define BASE_JSON_JSON_VALUE_SERIALIZER_H_ | 6 #define BASE_JSON_JSON_VALUE_SERIALIZER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); | 68 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); |
69 }; | 69 }; |
70 | 70 |
71 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { | 71 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { |
72 public: | 72 public: |
73 // json_file_patch is the path of a file that will be source of the | 73 // json_file_patch is the path of a file that will be source of the |
74 // deserialization or the destination of the serialization. | 74 // deserialization or the destination of the serialization. |
75 // When deserializing, the file should exist, but when serializing, the | 75 // When deserializing, the file should exist, but when serializing, the |
76 // serializer will attempt to create the file at the specified location. | 76 // serializer will attempt to create the file at the specified location. |
77 explicit JSONFileValueSerializer(const FilePath& json_file_path) | 77 explicit JSONFileValueSerializer(const FilePath& json_file_path) |
78 : json_file_path_(json_file_path) {} | 78 : json_file_path_(json_file_path), |
| 79 allow_trailing_comma_(false) {} |
79 | 80 |
80 virtual ~JSONFileValueSerializer() {} | 81 virtual ~JSONFileValueSerializer() {} |
81 | 82 |
82 // DO NOT USE except in unit tests to verify the file was written properly. | 83 // DO NOT USE except in unit tests to verify the file was written properly. |
83 // We should never serialize directly to a file since this will block the | 84 // We should never serialize directly to a file since this will block the |
84 // thread. Instead, serialize to a string and write to the file you want on | 85 // thread. Instead, serialize to a string and write to the file you want on |
85 // the file thread. | 86 // the file thread. |
86 // | 87 // |
87 // Attempt to serialize the data structure represented by Value into | 88 // Attempt to serialize the data structure represented by Value into |
88 // JSON. If the return value is true, the result will have been written | 89 // JSON. If the return value is true, the result will have been written |
(...skipping 22 matching lines...) Expand all Loading... |
111 // File-specific error messages that can be returned. | 112 // File-specific error messages that can be returned. |
112 static const char* kAccessDenied; | 113 static const char* kAccessDenied; |
113 static const char* kCannotReadFile; | 114 static const char* kCannotReadFile; |
114 static const char* kFileLocked; | 115 static const char* kFileLocked; |
115 static const char* kNoSuchFile; | 116 static const char* kNoSuchFile; |
116 | 117 |
117 // Convert an error code into an error message. |error_code| is assumed to | 118 // Convert an error code into an error message. |error_code| is assumed to |
118 // be a JsonFileError. | 119 // be a JsonFileError. |
119 static const char* GetErrorMessageForCode(int error_code); | 120 static const char* GetErrorMessageForCode(int error_code); |
120 | 121 |
| 122 void set_allow_trailing_comma(bool new_value) { |
| 123 allow_trailing_comma_ = new_value; |
| 124 } |
| 125 |
121 private: | 126 private: |
122 FilePath json_file_path_; | 127 FilePath json_file_path_; |
| 128 bool allow_trailing_comma_; |
123 | 129 |
124 // A wrapper for file_util::ReadFileToString which returns a non-zero | 130 // A wrapper for file_util::ReadFileToString which returns a non-zero |
125 // JsonFileError if there were file errors. | 131 // JsonFileError if there were file errors. |
126 int ReadFileToString(std::string* json_string); | 132 int ReadFileToString(std::string* json_string); |
127 | 133 |
128 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | 134 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
129 }; | 135 }; |
130 | 136 |
131 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_ | 137 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_ |
OLD | NEW |