blob: 67d2342b4cc16074a5e548afa02e7ddaebafcfbc [file] [log] [blame]
Alex Vakulenko0d205d72016-01-15 13:02:14 -08001// 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
17class 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
48class BASE_EXPORT JSONFileValueDeserializer : public base::ValueDeserializer {
49 public:
50 // |json_file_path_| is the path of a file that will be source of the
Luis Hector Chaveze5b2c6f2017-07-26 17:33:47 +000051 // deserialization.
52 explicit JSONFileValueDeserializer(const base::FilePath& json_file_path);
Alex Vakulenko0d205d72016-01-15 13:02:14 -080053
54 ~JSONFileValueDeserializer() override;
55
56 // Attempt to deserialize the data structure encoded in the file passed
57 // in to the constructor into a structure of Value objects. If the return
58 // value is NULL, and if |error_code| is non-null, |error_code| will
59 // contain an integer error code (either JsonFileError or JsonParseError).
60 // If |error_message| is non-null, it will be filled in with a formatted
61 // error message including the location of the error if appropriate.
62 // The caller takes ownership of the returned value.
Luis Hector Chavez94ffa552016-05-25 15:29:35 -070063 std::unique_ptr<base::Value> Deserialize(int* error_code,
64 std::string* error_message) override;
Alex Vakulenko0d205d72016-01-15 13:02:14 -080065
66 // This enum is designed to safely overlap with JSONReader::JsonParseError.
67 enum JsonFileError {
68 JSON_NO_ERROR = 0,
69 JSON_ACCESS_DENIED = 1000,
70 JSON_CANNOT_READ_FILE,
71 JSON_FILE_LOCKED,
72 JSON_NO_SUCH_FILE
73 };
74
75 // File-specific error messages that can be returned.
76 static const char kAccessDenied[];
77 static const char kCannotReadFile[];
78 static const char kFileLocked[];
79 static const char kNoSuchFile[];
80
81 // Convert an error code into an error message. |error_code| is assumed to
82 // be a JsonFileError.
83 static const char* GetErrorMessageForCode(int error_code);
84
Luis Hector Chaveze5b2c6f2017-07-26 17:33:47 +000085 void set_allow_trailing_comma(bool new_value) {
86 allow_trailing_comma_ = new_value;
87 }
88
Alex Vakulenko0d205d72016-01-15 13:02:14 -080089 // Returns the size (in bytes) of JSON string read from disk in the last
90 // successful |Deserialize()| call.
91 size_t get_last_read_size() const { return last_read_size_; }
92
93 private:
94 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if
95 // there were file errors.
96 int ReadFileToString(std::string* json_string);
97
98 const base::FilePath json_file_path_;
Luis Hector Chaveze5b2c6f2017-07-26 17:33:47 +000099 bool allow_trailing_comma_;
Alex Vakulenko0d205d72016-01-15 13:02:14 -0800100 size_t last_read_size_;
101
102 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueDeserializer);
103};
104
105#endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
106