/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
// @author Nicholas Ormrod <njormrod@fb.com>
-#ifndef DYNAMIC_CONVERTER_H
-#define DYNAMIC_CONVERTER_H
+#pragma once
#include <folly/dynamic.h>
namespace folly {
*
* Example:
*
- * dynamic d = { { 1, 2, 3 }, { 4, 5 } }; // a vector of vector of int
+ * dynamic d = dynamic::array(
+ * dynamic::array(1, 2, 3),
+ * dynamic::array(4, 5)); // a vector of vector of int
* auto vvi = convertTo<fbvector<fbvector<int>>>(d);
*
* See docs/DynamicConverter.md for supported types and customization
template <>
struct DynamicConverter<std::string> {
static std::string convert(const dynamic& d) {
- return d.asString().toStdString();
+ return d.asString();
}
};
}
};
+// identity
+template <typename C>
+struct DynamicConstructor<
+ C,
+ typename std::enable_if<std::is_same<C, dynamic>::value>::type> {
+ static dynamic construct(const C& x) {
+ return x;
+ }
+};
+
// maps
-template<typename C>
-struct DynamicConstructor<C,
+template <typename C>
+struct DynamicConstructor<
+ C,
typename std::enable_if<
- dynamicconverter_detail::is_map<C>::value>::type> {
+ !std::is_same<C, dynamic>::value &&
+ dynamicconverter_detail::is_map<C>::value>::type> {
static dynamic construct(const C& x) {
dynamic d = dynamic::object;
- for (auto& pair : x) {
+ for (const auto& pair : x) {
d.insert(toDynamic(pair.first), toDynamic(pair.second));
}
return d;
};
// other ranges
-template<typename C>
-struct DynamicConstructor<C,
+template <typename C>
+struct DynamicConstructor<
+ C,
typename std::enable_if<
- !dynamicconverter_detail::is_map<C>::value &&
- !std::is_constructible<StringPiece, const C&>::value &&
- dynamicconverter_detail::is_range<C>::value>::type> {
+ !std::is_same<C, dynamic>::value &&
+ !dynamicconverter_detail::is_map<C>::value &&
+ !std::is_constructible<StringPiece, const C&>::value &&
+ dynamicconverter_detail::is_range<C>::value>::type> {
static dynamic construct(const C& x) {
- dynamic d = {};
- for (auto& item : x) {
+ dynamic d = dynamic::array;
+ for (const auto& item : x) {
d.push_back(toDynamic(item));
}
return d;
template<typename A, typename B>
struct DynamicConstructor<std::pair<A, B>, void> {
static dynamic construct(const std::pair<A, B>& x) {
- dynamic d = {};
+ dynamic d = dynamic::array;
d.push_back(toDynamic(x.first));
d.push_back(toDynamic(x.second));
return d;
}
} // namespace folly
-
-#endif // DYNAMIC_CONVERTER_H