• 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 #ifndef BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
6 #define BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
7 
8 #include <stddef.h>
9 
10 #include <string>
11 
12 #include "base/base_export.h"
13 #include "base/files/file_path.h"
14 #include "base/macros.h"
15 #include "base/values.h"
16 
17 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer {
18  public:
19   // |json_file_path_| is the path of a file that will be destination of the
20   // serialization. The serializer will attempt to create the file at the
21   // specified location.
22   explicit JSONFileValueSerializer(const base::FilePath& json_file_path);
23 
24   ~JSONFileValueSerializer() override;
25 
26   // DO NOT USE except in unit tests to verify the file was written properly.
27   // We should never serialize directly to a file since this will block the
28   // thread. Instead, serialize to a string and write to the file you want on
29   // the file thread.
30   //
31   // Attempt to serialize the data structure represented by Value into
32   // JSON.  If the return value is true, the result will have been written
33   // into the file whose name was passed into the constructor.
34   bool Serialize(const base::Value& root) override;
35 
36   // Equivalent to Serialize(root) except binary values are omitted from the
37   // output.
38   bool SerializeAndOmitBinaryValues(const base::Value& root);
39 
40  private:
41   bool SerializeInternal(const base::Value& root, bool omit_binary_values);
42 
43   const base::FilePath json_file_path_;
44 
45   DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer);
46 };
47 
48 class BASE_EXPORT JSONFileValueDeserializer : public base::ValueDeserializer {
49  public:
50   // |json_file_path_| is the path of a file that will be source of the
51   // deserialization. |options| is a bitmask of JSONParserOptions.
52   explicit JSONFileValueDeserializer(const base::FilePath& json_file_path,
53                                      int options = 0);
54 
55   ~JSONFileValueDeserializer() override;
56 
57   // Attempt to deserialize the data structure encoded in the file passed
58   // in to the constructor into a structure of Value objects.  If the return
59   // value is NULL, and if |error_code| is non-null, |error_code| will
60   // contain an integer error code (either JsonFileError or JsonParseError).
61   // If |error_message| is non-null, it will be filled in with a formatted
62   // error message including the location of the error if appropriate.
63   // The caller takes ownership of the returned value.
64   std::unique_ptr<base::Value> Deserialize(int* error_code,
65                                            std::string* error_message) override;
66 
67   // This enum is designed to safely overlap with JSONReader::JsonParseError.
68   enum JsonFileError {
69     JSON_NO_ERROR = 0,
70     JSON_ACCESS_DENIED = 1000,
71     JSON_CANNOT_READ_FILE,
72     JSON_FILE_LOCKED,
73     JSON_NO_SUCH_FILE
74   };
75 
76   // File-specific error messages that can be returned.
77   static const char kAccessDenied[];
78   static const char kCannotReadFile[];
79   static const char kFileLocked[];
80   static const char kNoSuchFile[];
81 
82   // Convert an error code into an error message.  |error_code| is assumed to
83   // be a JsonFileError.
84   static const char* GetErrorMessageForCode(int error_code);
85 
86   // Returns the size (in bytes) of JSON string read from disk in the last
87   // successful |Deserialize()| call.
get_last_read_size()88   size_t get_last_read_size() const { return last_read_size_; }
89 
90  private:
91   // A wrapper for ReadFileToString which returns a non-zero JsonFileError if
92   // there were file errors.
93   int ReadFileToString(std::string* json_string);
94 
95   const base::FilePath json_file_path_;
96   const int options_;
97   size_t last_read_size_;
98 
99   DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueDeserializer);
100 };
101 
102 #endif  // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
103 
104