blob: 6a726d6986a0ee0e17a5576a000a1c9186f048a1 [file] [log] [blame]
David Ghandehari9e5b5872016-07-28 09:50:04 -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"
Andrew Top0d1858f2019-05-15 22:01:47 -070011#include "base/files/file_path.h"
12#include "base/macros.h"
David Ghandehari9e5b5872016-07-28 09:50:04 -070013#include "base/values.h"
Andrew Top0d1858f2019-05-15 22:01:47 -070014#include "starboard/types.h"
David Ghandehari9e5b5872016-07-28 09:50:04 -070015
16class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer {
17 public:
Andrew Top0d1858f2019-05-15 22:01:47 -070018 // |json_file_path_| is the path of a file that will be destination of the
19 // serialization. The serializer will attempt to create the file at the
20 // specified location.
21 explicit JSONFileValueSerializer(const base::FilePath& json_file_path);
David Ghandehari9e5b5872016-07-28 09:50:04 -070022
Andrew Top0d1858f2019-05-15 22:01:47 -070023 ~JSONFileValueSerializer() override;
David Ghandehari9e5b5872016-07-28 09:50:04 -070024
25 // DO NOT USE except in unit tests to verify the file was written properly.
26 // We should never serialize directly to a file since this will block the
27 // thread. Instead, serialize to a string and write to the file you want on
28 // the file thread.
29 //
30 // Attempt to serialize the data structure represented by Value into
31 // JSON. If the return value is true, the result will have been written
32 // into the file whose name was passed into the constructor.
Andrew Top0d1858f2019-05-15 22:01:47 -070033 bool Serialize(const base::Value& root) override;
David Ghandehari9e5b5872016-07-28 09:50:04 -070034
35 // Equivalent to Serialize(root) except binary values are omitted from the
36 // output.
Andrew Top0d1858f2019-05-15 22:01:47 -070037 bool SerializeAndOmitBinaryValues(const base::Value& root);
38
39 private:
40 bool SerializeInternal(const base::Value& root, bool omit_binary_values);
41
42 const base::FilePath json_file_path_;
43
44 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer);
45};
46
47class BASE_EXPORT JSONFileValueDeserializer : public base::ValueDeserializer {
48 public:
49 // |json_file_path_| is the path of a file that will be source of the
50 // deserialization. |options| is a bitmask of JSONParserOptions.
51 explicit JSONFileValueDeserializer(const base::FilePath& json_file_path,
52 int options = 0);
53
54 ~JSONFileValueDeserializer() override;
David Ghandehari9e5b5872016-07-28 09:50:04 -070055
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.
Andrew Top0d1858f2019-05-15 22:01:47 -070063 std::unique_ptr<base::Value> Deserialize(int* error_code,
64 std::string* error_message) override;
David Ghandehari9e5b5872016-07-28 09:50:04 -070065
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.
Andrew Top0d1858f2019-05-15 22:01:47 -070076 static const char kAccessDenied[];
77 static const char kCannotReadFile[];
78 static const char kFileLocked[];
79 static const char kNoSuchFile[];
David Ghandehari9e5b5872016-07-28 09:50:04 -070080
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
Andrew Top0d1858f2019-05-15 22:01:47 -070085 // Returns the size (in bytes) of JSON string read from disk in the last
86 // successful |Deserialize()| call.
87 size_t get_last_read_size() const { return last_read_size_; }
David Ghandehari9e5b5872016-07-28 09:50:04 -070088
89 private:
Andrew Top0d1858f2019-05-15 22:01:47 -070090 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if
91 // there were file errors.
David Ghandehari9e5b5872016-07-28 09:50:04 -070092 int ReadFileToString(std::string* json_string);
93
Andrew Top0d1858f2019-05-15 22:01:47 -070094 const base::FilePath json_file_path_;
95 const int options_;
96 size_t last_read_size_;
97
98 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueDeserializer);
David Ghandehari9e5b5872016-07-28 09:50:04 -070099};
100
101#endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
102