summaryrefslogtreecommitdiffstats
path: root/base/json
diff options
context:
space:
mode:
authordpranke@chromium.org <dpranke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-07 18:36:00 +0000
committerdpranke@chromium.org <dpranke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-07 18:36:00 +0000
commit005bab086c8d301c29e99db5903b00bb18cbdaaf (patch)
tree5e64a9a62a571276d1f040d9ac49a1a185473c28 /base/json
parente378878b3eb3ad69affcdb3f8e36accbd1ea4c4c (diff)
downloadchromium_src-005bab086c8d301c29e99db5903b00bb18cbdaaf.zip
chromium_src-005bab086c8d301c29e99db5903b00bb18cbdaaf.tar.gz
chromium_src-005bab086c8d301c29e99db5903b00bb18cbdaaf.tar.bz2
Move json_value_serializer from content/common to base.
R=darin@chromium.org BUG=92044 TEST= Review URL: http://codereview.chromium.org/8165009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104527 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/json')
-rw-r--r--base/json/json_value_serializer.cc108
-rw-r--r--base/json/json_value_serializer.h129
2 files changed, 237 insertions, 0 deletions
diff --git a/base/json/json_value_serializer.cc b/base/json/json_value_serializer.cc
new file mode 100644
index 0000000..d667bc1
--- /dev/null
+++ b/base/json/json_value_serializer.cc
@@ -0,0 +1,108 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/json/json_value_serializer.h"
+
+#include "base/file_util.h"
+#include "base/json/json_reader.h"
+#include "base/json/json_writer.h"
+#include "base/string_util.h"
+
+const char* JSONFileValueSerializer::kAccessDenied = "Access denied.";
+const char* JSONFileValueSerializer::kCannotReadFile = "Can't read file.";
+const char* JSONFileValueSerializer::kFileLocked = "File locked.";
+const char* JSONFileValueSerializer::kNoSuchFile = "File doesn't exist.";
+
+JSONStringValueSerializer::~JSONStringValueSerializer() {}
+
+bool JSONStringValueSerializer::Serialize(const Value& root) {
+ if (!json_string_ || initialized_with_const_string_)
+ return false;
+
+ base::JSONWriter::Write(&root, pretty_print_, json_string_);
+ return true;
+}
+
+Value* JSONStringValueSerializer::Deserialize(int* error_code,
+ std::string* error_str) {
+ if (!json_string_)
+ return NULL;
+
+ return base::JSONReader::ReadAndReturnError(*json_string_,
+ allow_trailing_comma_,
+ error_code,
+ error_str);
+}
+
+/******* File Serializer *******/
+
+bool JSONFileValueSerializer::Serialize(const Value& root) {
+ std::string json_string;
+ JSONStringValueSerializer serializer(&json_string);
+ serializer.set_pretty_print(true);
+ bool result = serializer.Serialize(root);
+ if (!result)
+ return false;
+
+ int data_size = static_cast<int>(json_string.size());
+ if (file_util::WriteFile(json_file_path_,
+ json_string.data(),
+ data_size) != data_size)
+ return false;
+
+ return true;
+}
+
+int JSONFileValueSerializer::ReadFileToString(std::string* json_string) {
+ DCHECK(json_string);
+ if (!file_util::ReadFileToString(json_file_path_, json_string)) {
+#if defined(OS_WIN)
+ int error = ::GetLastError();
+ if (error == ERROR_SHARING_VIOLATION || error == ERROR_LOCK_VIOLATION) {
+ return JSON_FILE_LOCKED;
+ } else if (error == ERROR_ACCESS_DENIED) {
+ return JSON_ACCESS_DENIED;
+ }
+#endif
+ if (!file_util::PathExists(json_file_path_))
+ return JSON_NO_SUCH_FILE;
+ else
+ return JSON_CANNOT_READ_FILE;
+ }
+ return JSON_NO_ERROR;
+}
+
+const char* JSONFileValueSerializer::GetErrorMessageForCode(int error_code) {
+ switch (error_code) {
+ case JSON_NO_ERROR:
+ return "";
+ case JSON_ACCESS_DENIED:
+ return kAccessDenied;
+ case JSON_CANNOT_READ_FILE:
+ return kCannotReadFile;
+ case JSON_FILE_LOCKED:
+ return kFileLocked;
+ case JSON_NO_SUCH_FILE:
+ return kNoSuchFile;
+ default:
+ NOTREACHED();
+ return "";
+ }
+}
+
+Value* JSONFileValueSerializer::Deserialize(int* error_code,
+ std::string* error_str) {
+ std::string json_string;
+ int error = ReadFileToString(&json_string);
+ if (error != JSON_NO_ERROR) {
+ if (error_code)
+ *error_code = error;
+ if (error_str)
+ *error_str = GetErrorMessageForCode(error);
+ return NULL;
+ }
+
+ JSONStringValueSerializer serializer(json_string);
+ return serializer.Deserialize(error_code, error_str);
+}
diff --git a/base/json/json_value_serializer.h b/base/json/json_value_serializer.h
new file mode 100644
index 0000000..650008e
--- /dev/null
+++ b/base/json/json_value_serializer.h
@@ -0,0 +1,129 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_JSON_JSON_VALUE_SERIALIZER_H_
+#define BASE_JSON_JSON_VALUE_SERIALIZER_H_
+#pragma once
+
+#include <string>
+
+#include "base/base_export.h"
+#include "base/basictypes.h"
+#include "base/file_path.h"
+#include "base/values.h"
+
+class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer {
+ public:
+ // json_string is the string that will be source of the deserialization
+ // or the destination of the serialization. The caller of the constructor
+ // retains ownership of the string.
+ explicit JSONStringValueSerializer(std::string* json_string)
+ : json_string_(json_string),
+ initialized_with_const_string_(false),
+ pretty_print_(false),
+ allow_trailing_comma_(false) {
+ }
+
+ // This version allows initialization with a const string reference for
+ // deserialization only.
+ explicit JSONStringValueSerializer(const std::string& json_string)
+ : json_string_(&const_cast<std::string&>(json_string)),
+ initialized_with_const_string_(true),
+ pretty_print_(false),
+ allow_trailing_comma_(false) {
+ }
+
+ virtual ~JSONStringValueSerializer();
+
+ // Attempt to serialize the data structure represented by Value into
+ // JSON. If the return value is true, the result will have been written
+ // into the string passed into the constructor.
+ virtual bool Serialize(const Value& root);
+
+ // Attempt to deserialize the data structure encoded in the string passed
+ // in to the constructor into a structure of Value objects. If the return
+ // value is NULL, and if |error_code| is non-null, |error_code| will
+ // contain an integer error code (either JsonFileError or JsonParseError).
+ // If |error_message| is non-null, it will be filled in with a formatted
+ // error message including the location of the error if appropriate.
+ // The caller takes ownership of the returned value.
+ virtual Value* Deserialize(int* error_code, std::string* error_message);
+
+ void set_pretty_print(bool new_value) { pretty_print_ = new_value; }
+ bool pretty_print() { return pretty_print_; }
+
+ void set_allow_trailing_comma(bool new_value) {
+ allow_trailing_comma_ = new_value;
+ }
+
+ private:
+ std::string* json_string_;
+ bool initialized_with_const_string_;
+ bool pretty_print_; // If true, serialization will span multiple lines.
+ // If true, deserialization will allow trailing commas.
+ bool allow_trailing_comma_;
+
+ DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer);
+};
+
+class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer {
+ public:
+ // json_file_patch is the path of a file that will be source of the
+ // deserialization or the destination of the serialization.
+ // When deserializing, the file should exist, but when serializing, the
+ // serializer will attempt to create the file at the specified location.
+ explicit JSONFileValueSerializer(const FilePath& json_file_path)
+ : json_file_path_(json_file_path) {}
+
+ virtual ~JSONFileValueSerializer() {}
+
+ // DO NOT USE except in unit tests to verify the file was written properly.
+ // We should never serialize directly to a file since this will block the
+ // thread. Instead, serialize to a string and write to the file you want on
+ // the file thread.
+ //
+ // Attempt to serialize the data structure represented by Value into
+ // JSON. If the return value is true, the result will have been written
+ // into the file whose name was passed into the constructor.
+ virtual bool Serialize(const Value& root);
+
+ // Attempt to deserialize the data structure encoded in the file passed
+ // in to the constructor into a structure of Value objects. If the return
+ // value is NULL, and if |error_code| is non-null, |error_code| will
+ // contain an integer error code (either JsonFileError or JsonParseError).
+ // If |error_message| is non-null, it will be filled in with a formatted
+ // error message including the location of the error if appropriate.
+ // The caller takes ownership of the returned value.
+ virtual Value* Deserialize(int* error_code, std::string* error_message);
+
+ // This enum is designed to safely overlap with JSONReader::JsonParseError.
+ enum JsonFileError {
+ JSON_NO_ERROR = 0,
+ JSON_ACCESS_DENIED = 1000,
+ JSON_CANNOT_READ_FILE,
+ JSON_FILE_LOCKED,
+ JSON_NO_SUCH_FILE
+ };
+
+ // File-specific error messages that can be returned.
+ static const char* kAccessDenied;
+ static const char* kCannotReadFile;
+ static const char* kFileLocked;
+ static const char* kNoSuchFile;
+
+ // Convert an error code into an error message. |error_code| is assumed to
+ // be a JsonFileError.
+ static const char* GetErrorMessageForCode(int error_code);
+
+ private:
+ FilePath json_file_path_;
+
+ // A wrapper for file_util::ReadFileToString which returns a non-zero
+ // JsonFileError if there were file errors.
+ int ReadFileToString(std::string* json_string);
+
+ DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer);
+};
+
+#endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_