blob: aab47eec2dc269f64126207bc42030bfd3650c3a [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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 <string>
9
10#include "base/base_export.h"
11#include "base/basictypes.h"
12#include "base/files/file_path.h"
13#include "base/values.h"
14
15class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer {
16 public:
Dave Moorecc0e4f92015-03-10 15:23:04 -070017 // |json_file_path_| is the path of a file that will be destination of the
18 // serialization. The serializer will attempt to create the file at the
19 // specified location.
Benjamin Lermancdfc88d2015-02-03 14:35:12 +010020 explicit JSONFileValueSerializer(const base::FilePath& json_file_path);
James Robinson646469d2014-10-03 15:33:28 -070021
Benjamin Lermancdfc88d2015-02-03 14:35:12 +010022 ~JSONFileValueSerializer() override;
James Robinson646469d2014-10-03 15:33:28 -070023
24 // DO NOT USE except in unit tests to verify the file was written properly.
25 // We should never serialize directly to a file since this will block the
26 // thread. Instead, serialize to a string and write to the file you want on
27 // the file thread.
28 //
29 // Attempt to serialize the data structure represented by Value into
30 // JSON. If the return value is true, the result will have been written
31 // into the file whose name was passed into the constructor.
James Robinsone1b30cf2014-10-21 12:25:40 -070032 bool Serialize(const base::Value& root) override;
James Robinson646469d2014-10-03 15:33:28 -070033
34 // Equivalent to Serialize(root) except binary values are omitted from the
35 // output.
36 bool SerializeAndOmitBinaryValues(const base::Value& root);
37
Dave Moorecc0e4f92015-03-10 15:23:04 -070038 private:
39 bool SerializeInternal(const base::Value& root, bool omit_binary_values);
40
41 const base::FilePath json_file_path_;
42
43 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer);
44};
45
46class BASE_EXPORT JSONFileValueDeserializer : public base::ValueDeserializer {
47 public:
48 // |json_file_path_| is the path of a file that will be source of the
49 // deserialization.
50 explicit JSONFileValueDeserializer(const base::FilePath& json_file_path);
51
52 ~JSONFileValueDeserializer() override;
53
James Robinson646469d2014-10-03 15:33:28 -070054 // Attempt to deserialize the data structure encoded in the file passed
55 // in to the constructor into a structure of Value objects. If the return
56 // value is NULL, and if |error_code| is non-null, |error_code| will
57 // contain an integer error code (either JsonFileError or JsonParseError).
58 // If |error_message| is non-null, it will be filled in with a formatted
59 // error message including the location of the error if appropriate.
60 // The caller takes ownership of the returned value.
James Robinsone1b30cf2014-10-21 12:25:40 -070061 base::Value* Deserialize(int* error_code,
62 std::string* error_message) override;
James Robinson646469d2014-10-03 15:33:28 -070063
64 // This enum is designed to safely overlap with JSONReader::JsonParseError.
65 enum JsonFileError {
66 JSON_NO_ERROR = 0,
67 JSON_ACCESS_DENIED = 1000,
68 JSON_CANNOT_READ_FILE,
69 JSON_FILE_LOCKED,
70 JSON_NO_SUCH_FILE
71 };
72
73 // File-specific error messages that can be returned.
James Robinsone1b30cf2014-10-21 12:25:40 -070074 static const char kAccessDenied[];
75 static const char kCannotReadFile[];
76 static const char kFileLocked[];
77 static const char kNoSuchFile[];
James Robinson646469d2014-10-03 15:33:28 -070078
79 // Convert an error code into an error message. |error_code| is assumed to
80 // be a JsonFileError.
81 static const char* GetErrorMessageForCode(int error_code);
82
83 void set_allow_trailing_comma(bool new_value) {
84 allow_trailing_comma_ = new_value;
85 }
86
Dave Moorecc0e4f92015-03-10 15:23:04 -070087 // Returns the size (in bytes) of JSON string read from disk in the last
Benjamin Lermancdfc88d2015-02-03 14:35:12 +010088 // successful |Deserialize()| call.
89 size_t get_last_read_size() const { return last_read_size_; }
90
James Robinson646469d2014-10-03 15:33:28 -070091 private:
Dave Moorecc0e4f92015-03-10 15:23:04 -070092 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if
93 // there were file errors.
94 int ReadFileToString(std::string* json_string);
James Robinson646469d2014-10-03 15:33:28 -070095
Benjamin Lermancdfc88d2015-02-03 14:35:12 +010096 const base::FilePath json_file_path_;
James Robinson646469d2014-10-03 15:33:28 -070097 bool allow_trailing_comma_;
Benjamin Lermancdfc88d2015-02-03 14:35:12 +010098 size_t last_read_size_;
James Robinson646469d2014-10-03 15:33:28 -070099
Dave Moorecc0e4f92015-03-10 15:23:04 -0700100 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueDeserializer);
James Robinson646469d2014-10-03 15:33:28 -0700101};
102
103#endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
104