blob: a93950a6080014e5d13666103fbdedf41546c94c [file] [log] [blame]
bbudge@chromium.org218cd872012-02-27 05:26:42 +09001// 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_
bbudge@chromium.org218cd872012-02-27 05:26:42 +09007
avia6a6a682015-12-27 07:15:14 +09008#include <stddef.h>
9
bbudge@chromium.org218cd872012-02-27 05:26:42 +090010#include <string>
11
12#include "base/base_export.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090013#include "base/files/file_path.h"
avia6a6a682015-12-27 07:15:14 +090014#include "base/macros.h"
bbudge@chromium.org218cd872012-02-27 05:26:42 +090015#include "base/values.h"
16
17class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer {
18 public:
prashhir58322f42015-03-05 18:30:57 +090019 // |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.
gab49f05322015-02-01 04:09:02 +090022 explicit JSONFileValueSerializer(const base::FilePath& json_file_path);
bbudge@chromium.org218cd872012-02-27 05:26:42 +090023
gab49f05322015-02-01 04:09:02 +090024 ~JSONFileValueSerializer() override;
bbudge@chromium.org218cd872012-02-27 05:26:42 +090025
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.
dcheng7dc8df52014-10-21 19:54:51 +090034 bool Serialize(const base::Value& root) override;
bbudge@chromium.org218cd872012-02-27 05:26:42 +090035
36 // Equivalent to Serialize(root) except binary values are omitted from the
37 // output.
brettw@chromium.org88aa6552013-06-15 02:56:08 +090038 bool SerializeAndOmitBinaryValues(const base::Value& root);
bbudge@chromium.org218cd872012-02-27 05:26:42 +090039
prashhir58322f42015-03-05 18:30:57 +090040 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
sky03cc9b32016-11-11 10:12:37 +090051 // deserialization. |options| is a bitmask of JSONParserOptions.
52 explicit JSONFileValueDeserializer(const base::FilePath& json_file_path,
53 int options = 0);
prashhir58322f42015-03-05 18:30:57 +090054
55 ~JSONFileValueDeserializer() override;
56
bbudge@chromium.org218cd872012-02-27 05:26:42 +090057 // 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.
dchengcc8e4d82016-04-05 06:25:51 +090064 std::unique_ptr<base::Value> Deserialize(int* error_code,
65 std::string* error_message) override;
bbudge@chromium.org218cd872012-02-27 05:26:42 +090066
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.
thestigdd7aaa02014-10-21 12:11:21 +090077 static const char kAccessDenied[];
78 static const char kCannotReadFile[];
79 static const char kFileLocked[];
80 static const char kNoSuchFile[];
bbudge@chromium.org218cd872012-02-27 05:26:42 +090081
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
prashhir58322f42015-03-05 18:30:57 +090086 // Returns the size (in bytes) of JSON string read from disk in the last
gab49f05322015-02-01 04:09:02 +090087 // successful |Deserialize()| call.
88 size_t get_last_read_size() const { return last_read_size_; }
89
bbudge@chromium.org218cd872012-02-27 05:26:42 +090090 private:
prashhir58322f42015-03-05 18:30:57 +090091 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if
92 // there were file errors.
93 int ReadFileToString(std::string* json_string);
bbudge@chromium.org218cd872012-02-27 05:26:42 +090094
gab49f05322015-02-01 04:09:02 +090095 const base::FilePath json_file_path_;
sky03cc9b32016-11-11 10:12:37 +090096 const int options_;
gab49f05322015-02-01 04:09:02 +090097 size_t last_read_size_;
bbudge@chromium.org218cd872012-02-27 05:26:42 +090098
prashhir58322f42015-03-05 18:30:57 +090099 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueDeserializer);
bbudge@chromium.org218cd872012-02-27 05:26:42 +0900100};
101
102#endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
103