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