Fix sort order of unlaunched apps on app list start page.
[chromium-blink-merge.git] / base / json / json_file_value_serializer.h
blob6cfcbe83c2f8295fcd5f277bf637b6afda809e2a
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.
5 #ifndef BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
6 #define BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
8 #include <string>
10 #include "base/base_export.h"
11 #include "base/basictypes.h"
12 #include "base/files/file_path.h"
13 #include "base/values.h"
15 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer {
16 public:
17 // |json_file_path_| is the path of a file that will be source of the
18 // deserialization or the destination of the serialization.
19 // When deserializing, the file should exist, but when serializing, the
20 // serializer will attempt to create the file at the specified location.
21 explicit JSONFileValueSerializer(const base::FilePath& json_file_path);
23 ~JSONFileValueSerializer() override;
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.
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.
33 bool Serialize(const base::Value& root) override;
35 // Equivalent to Serialize(root) except binary values are omitted from the
36 // output.
37 bool SerializeAndOmitBinaryValues(const base::Value& root);
39 // Attempt to deserialize the data structure encoded in the file passed
40 // in to the constructor into a structure of Value objects. If the return
41 // value is NULL, and if |error_code| is non-null, |error_code| will
42 // contain an integer error code (either JsonFileError or JsonParseError).
43 // If |error_message| is non-null, it will be filled in with a formatted
44 // error message including the location of the error if appropriate.
45 // The caller takes ownership of the returned value.
46 base::Value* Deserialize(int* error_code,
47 std::string* error_message) override;
49 // This enum is designed to safely overlap with JSONReader::JsonParseError.
50 enum JsonFileError {
51 JSON_NO_ERROR = 0,
52 JSON_ACCESS_DENIED = 1000,
53 JSON_CANNOT_READ_FILE,
54 JSON_FILE_LOCKED,
55 JSON_NO_SUCH_FILE
58 // File-specific error messages that can be returned.
59 static const char kAccessDenied[];
60 static const char kCannotReadFile[];
61 static const char kFileLocked[];
62 static const char kNoSuchFile[];
64 // Convert an error code into an error message. |error_code| is assumed to
65 // be a JsonFileError.
66 static const char* GetErrorMessageForCode(int error_code);
68 void set_allow_trailing_comma(bool new_value) {
69 allow_trailing_comma_ = new_value;
72 // Returns the syze (in bytes) of JSON string read from disk in the last
73 // successful |Deserialize()| call.
74 size_t get_last_read_size() const { return last_read_size_; }
76 private:
77 bool SerializeInternal(const base::Value& root, bool omit_binary_values);
79 const base::FilePath json_file_path_;
80 bool allow_trailing_comma_;
81 size_t last_read_size_;
83 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if
84 // there were file errors.
85 int ReadFileToString(std::string* json_string);
87 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer);
90 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_