json_file_value_serializer.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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.
4
5#include "base/json/json_file_value_serializer.h"
6
7#include "base/file_util.h"
8#include "base/json/json_string_value_serializer.h"
9#include "base/logging.h"
10
11using base::FilePath;
12
13const char* JSONFileValueSerializer::kAccessDenied = "Access denied.";
14const char* JSONFileValueSerializer::kCannotReadFile = "Can't read file.";
15const char* JSONFileValueSerializer::kFileLocked = "File locked.";
16const char* JSONFileValueSerializer::kNoSuchFile = "File doesn't exist.";
17
18bool JSONFileValueSerializer::Serialize(const base::Value& root) {
19  return SerializeInternal(root, false);
20}
21
22bool JSONFileValueSerializer::SerializeAndOmitBinaryValues(
23    const base::Value& root) {
24  return SerializeInternal(root, true);
25}
26
27bool JSONFileValueSerializer::SerializeInternal(const base::Value& root,
28                                                bool omit_binary_values) {
29  std::string json_string;
30  JSONStringValueSerializer serializer(&json_string);
31  serializer.set_pretty_print(true);
32  bool result = omit_binary_values ?
33      serializer.SerializeAndOmitBinaryValues(root) :
34      serializer.Serialize(root);
35  if (!result)
36    return false;
37
38  int data_size = static_cast<int>(json_string.size());
39  if (file_util::WriteFile(json_file_path_,
40                           json_string.data(),
41                           data_size) != data_size)
42    return false;
43
44  return true;
45}
46
47int JSONFileValueSerializer::ReadFileToString(std::string* json_string) {
48  DCHECK(json_string);
49  if (!file_util::ReadFileToString(json_file_path_, json_string)) {
50#if defined(OS_WIN)
51    int error = ::GetLastError();
52    if (error == ERROR_SHARING_VIOLATION || error == ERROR_LOCK_VIOLATION) {
53      return JSON_FILE_LOCKED;
54    } else if (error == ERROR_ACCESS_DENIED) {
55      return JSON_ACCESS_DENIED;
56    }
57#endif
58    if (!file_util::PathExists(json_file_path_))
59      return JSON_NO_SUCH_FILE;
60    else
61      return JSON_CANNOT_READ_FILE;
62  }
63  return JSON_NO_ERROR;
64}
65
66const char* JSONFileValueSerializer::GetErrorMessageForCode(int error_code) {
67  switch (error_code) {
68    case JSON_NO_ERROR:
69      return "";
70    case JSON_ACCESS_DENIED:
71      return kAccessDenied;
72    case JSON_CANNOT_READ_FILE:
73      return kCannotReadFile;
74    case JSON_FILE_LOCKED:
75      return kFileLocked;
76    case JSON_NO_SUCH_FILE:
77      return kNoSuchFile;
78    default:
79      NOTREACHED();
80      return "";
81  }
82}
83
84base::Value* JSONFileValueSerializer::Deserialize(int* error_code,
85                                                  std::string* error_str) {
86  std::string json_string;
87  int error = ReadFileToString(&json_string);
88  if (error != JSON_NO_ERROR) {
89    if (error_code)
90      *error_code = error;
91    if (error_str)
92      *error_str = GetErrorMessageForCode(error);
93    return NULL;
94  }
95
96  JSONStringValueSerializer serializer(json_string);
97  serializer.set_allow_trailing_comma(allow_trailing_comma_);
98  return serializer.Deserialize(error_code, error_str);
99}
100