Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(329)

Unified Diff: base/json/json_value_converter.h

Issue 9184002: Add custom field converter to JSONValueConverter. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/json/json_value_converter_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/json/json_value_converter.h
diff --git a/base/json/json_value_converter.h b/base/json/json_value_converter.h
index e933dadad5c4812f13526930ef374be89a391ade..a607aee3abdaeee9463e1f691eada43e556df660 100644
--- a/base/json/json_value_converter.h
+++ b/base/json/json_value_converter.h
@@ -15,6 +15,7 @@
#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
#include "base/string16.h"
+#include "base/string_piece.h"
#include "base/values.h"
// JSONValueConverter converts a JSON value into a C++ struct in a
@@ -187,6 +188,27 @@ class BasicValueConverter<bool> : public ValueConverter<bool> {
DISALLOW_COPY_AND_ASSIGN(BasicValueConverter);
};
+template <typename FieldType>
+class CustomFieldConverter : public ValueConverter<FieldType> {
+ public:
+ typedef bool(*ConvertFunc)(const StringPiece& value, FieldType* field);
+
+ CustomFieldConverter(ConvertFunc convert_func)
+ : convert_func_(convert_func) {}
+
+ virtual bool Convert(const base::Value& value,
+ FieldType* field) const OVERRIDE {
+ std::string string_value;
+ return value.GetAsString(&string_value) &&
+ convert_func_(string_value, field);
+ }
+
+ private:
+ ConvertFunc convert_func_;
+
+ DISALLOW_COPY_AND_ASSIGN(CustomFieldConverter);
+};
+
template <typename NestedType>
class NestedValueConverter : public ValueConverter<NestedType> {
public:
@@ -320,6 +342,17 @@ class JSONValueConverter {
new internal::NestedValueConverter<NestedType>));
}
+ template <typename FieldType>
+ void RegisterCustomField(
+ const std::string& field_name,
+ FieldType StructType::* field,
+ bool (*convert_func)(const StringPiece&, FieldType*)) {
+ fields_.push_back(new internal::FieldConverter<StructType, FieldType>(
+ field_name,
+ field,
+ new internal::CustomFieldConverter<FieldType>(convert_func)));
+ }
+
void RegisterRepeatedInt(const std::string& field_name,
std::vector<int> StructType::* field) {
fields_.push_back(
« no previous file with comments | « no previous file | base/json/json_value_converter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698