| OLD | NEW |
| 1 // Copyright (c) 2011 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 #ifndef BASE_JSON_JSON_VALUE_SERIALIZER_H_ | 5 #ifndef BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 6 #define BASE_JSON_JSON_VALUE_SERIALIZER_H_ | 6 #define BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 | 15 |
| 16 class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { | |
| 17 public: | |
| 18 // json_string is the string that will be source of the deserialization | |
| 19 // or the destination of the serialization. The caller of the constructor | |
| 20 // retains ownership of the string. | |
| 21 explicit JSONStringValueSerializer(std::string* json_string) | |
| 22 : json_string_(json_string), | |
| 23 initialized_with_const_string_(false), | |
| 24 pretty_print_(false), | |
| 25 allow_trailing_comma_(false) { | |
| 26 } | |
| 27 | |
| 28 // This version allows initialization with a const string reference for | |
| 29 // deserialization only. | |
| 30 explicit JSONStringValueSerializer(const std::string& json_string) | |
| 31 : json_string_(&const_cast<std::string&>(json_string)), | |
| 32 initialized_with_const_string_(true), | |
| 33 pretty_print_(false), | |
| 34 allow_trailing_comma_(false) { | |
| 35 } | |
| 36 | |
| 37 virtual ~JSONStringValueSerializer(); | |
| 38 | |
| 39 // Attempt to serialize the data structure represented by Value into | |
| 40 // JSON. If the return value is true, the result will have been written | |
| 41 // into the string passed into the constructor. | |
| 42 virtual bool Serialize(const Value& root) OVERRIDE; | |
| 43 | |
| 44 // Equivalent to Serialize(root) except binary values are omitted from the | |
| 45 // output. | |
| 46 bool SerializeAndOmitBinaryValues(const Value& root); | |
| 47 | |
| 48 // Attempt to deserialize the data structure encoded in the string passed | |
| 49 // in to the constructor into a structure of Value objects. If the return | |
| 50 // value is NULL, and if |error_code| is non-null, |error_code| will | |
| 51 // contain an integer error code (either JsonFileError or JsonParseError). | |
| 52 // If |error_message| is non-null, it will be filled in with a formatted | |
| 53 // error message including the location of the error if appropriate. | |
| 54 // The caller takes ownership of the returned value. | |
| 55 virtual Value* Deserialize(int* error_code, | |
| 56 std::string* error_message) OVERRIDE; | |
| 57 | |
| 58 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } | |
| 59 bool pretty_print() { return pretty_print_; } | |
| 60 | |
| 61 void set_allow_trailing_comma(bool new_value) { | |
| 62 allow_trailing_comma_ = new_value; | |
| 63 } | |
| 64 | |
| 65 private: | |
| 66 bool SerializeInternal(const Value& root, bool omit_binary_values); | |
| 67 | |
| 68 std::string* json_string_; | |
| 69 bool initialized_with_const_string_; | |
| 70 bool pretty_print_; // If true, serialization will span multiple lines. | |
| 71 // If true, deserialization will allow trailing commas. | |
| 72 bool allow_trailing_comma_; | |
| 73 | |
| 74 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); | |
| 75 }; | |
| 76 | |
| 77 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { | 16 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { |
| 78 public: | 17 public: |
| 79 // json_file_patch is the path of a file that will be source of the | 18 // json_file_patch is the path of a file that will be source of the |
| 80 // deserialization or the destination of the serialization. | 19 // deserialization or the destination of the serialization. |
| 81 // When deserializing, the file should exist, but when serializing, the | 20 // When deserializing, the file should exist, but when serializing, the |
| 82 // serializer will attempt to create the file at the specified location. | 21 // serializer will attempt to create the file at the specified location. |
| 83 explicit JSONFileValueSerializer(const FilePath& json_file_path) | 22 explicit JSONFileValueSerializer(const FilePath& json_file_path) |
| 84 : json_file_path_(json_file_path), | 23 : json_file_path_(json_file_path), |
| 85 allow_trailing_comma_(false) {} | 24 allow_trailing_comma_(false) {} |
| 86 | 25 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 FilePath json_file_path_; | 78 FilePath json_file_path_; |
| 140 bool allow_trailing_comma_; | 79 bool allow_trailing_comma_; |
| 141 | 80 |
| 142 // A wrapper for file_util::ReadFileToString which returns a non-zero | 81 // A wrapper for file_util::ReadFileToString which returns a non-zero |
| 143 // JsonFileError if there were file errors. | 82 // JsonFileError if there were file errors. |
| 144 int ReadFileToString(std::string* json_string); | 83 int ReadFileToString(std::string* json_string); |
| 145 | 84 |
| 146 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | 85 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
| 147 }; | 86 }; |
| 148 | 87 |
| 149 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_ | 88 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 89 |
| OLD | NEW |