2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // @author Nicholas Ormrod <njormrod@fb.com>
19 #ifndef DYNAMIC_CONVERTER_H
20 #define DYNAMIC_CONVERTER_H
22 #include <folly/dynamic.h>
24 template <typename T> T convertTo(const dynamic&);
25 template <typename T> dynamic toDynamic(const T&);
29 * convertTo returns a well-typed representation of the input dynamic.
33 * dynamic d = dynamic::array(
34 * dynamic::array(1, 2, 3),
35 * dynamic::array(4, 5)); // a vector of vector of int
36 * auto vvi = convertTo<fbvector<fbvector<int>>>(d);
38 * See docs/DynamicConverter.md for supported types and customization
42 #include <type_traits>
44 #include <boost/iterator/iterator_adaptor.hpp>
45 #include <boost/mpl/has_xxx.hpp>
46 #include <folly/Likely.h>
50 ///////////////////////////////////////////////////////////////////////////////
53 namespace dynamicconverter_detail {
55 BOOST_MPL_HAS_XXX_TRAIT_DEF(value_type);
56 BOOST_MPL_HAS_XXX_TRAIT_DEF(iterator);
57 BOOST_MPL_HAS_XXX_TRAIT_DEF(mapped_type);
59 template <typename T> struct iterator_class_is_container {
60 typedef std::reverse_iterator<typename T::iterator> some_iterator;
61 enum { value = has_value_type<T>::value &&
62 std::is_constructible<T, some_iterator, some_iterator>::value };
66 using class_is_container = typename
68 has_iterator<T>::value,
69 iterator_class_is_container<T>,
73 template <typename T> struct class_is_range {
74 enum { value = has_value_type<T>::value &&
75 has_iterator<T>::value };
79 template <typename T> struct is_container
81 std::is_class<T>::value,
82 class_is_container<T>,
86 template <typename T> struct is_range
88 std::is_class<T>::value,
93 template <typename T> struct is_map
94 : std::integral_constant<
96 is_range<T>::value && has_mapped_type<T>::value
99 } // namespace dynamicconverter_detail
101 ///////////////////////////////////////////////////////////////////////////////
105 * We have iterators that dereference to dynamics, but need iterators
106 * that dereference to typename T.
108 * Implementation details:
109 * 1. We cache the value of the dereference operator. This is necessary
110 * because boost::iterator_adaptor requires *it to return a
112 * 2. For const reasons, we cannot call operator= to refresh the
113 * cache: we must call the destructor then placement new.
116 namespace dynamicconverter_detail {
119 struct Dereferencer {
120 static inline void derefToCache(
121 T* /* mem */, const dynamic::const_item_iterator& /* it */) {
122 throw TypeError("array", dynamic::Type::OBJECT);
125 static inline void derefToCache(T* mem, const dynamic::const_iterator& it) {
126 new (mem) T(convertTo<T>(*it));
130 template<typename F, typename S>
131 struct Dereferencer<std::pair<F, S>> {
133 derefToCache(std::pair<F, S>* mem, const dynamic::const_item_iterator& it) {
134 new (mem) std::pair<F, S>(
135 convertTo<F>(it->first), convertTo<S>(it->second)
139 // Intentional duplication of the code in Dereferencer
140 template <typename T>
141 static inline void derefToCache(T* mem, const dynamic::const_iterator& it) {
142 new (mem) T(convertTo<T>(*it));
146 template <typename T, typename It>
147 class Transformer : public boost::iterator_adaptor<
150 typename T::value_type
152 friend class boost::iterator_core_access;
154 typedef typename T::value_type ttype;
156 mutable ttype cache_;
160 ++this->base_reference();
164 ttype& dereference() const {
165 if (LIKELY(!valid_)) {
167 Dereferencer<ttype>::derefToCache(&cache_, this->base_reference());
174 explicit Transformer(const It& it)
175 : Transformer::iterator_adaptor_(it), valid_(false) {}
178 // conversion factory
179 template <typename T, typename It>
180 inline std::move_iterator<Transformer<T, It>>
181 conversionIterator(const It& it) {
182 return std::make_move_iterator(Transformer<T, It>(it));
185 } // namespace dynamicconverter_detail
187 ///////////////////////////////////////////////////////////////////////////////
188 // DynamicConverter specializations
191 * Each specialization of DynamicConverter has the function
192 * 'static T convert(const dynamic&);'
195 // default - intentionally unimplemented
196 template <typename T, typename Enable = void> struct DynamicConverter;
200 struct DynamicConverter<bool> {
201 static bool convert(const dynamic& d) {
207 template <typename T>
208 struct DynamicConverter<T,
209 typename std::enable_if<std::is_integral<T>::value &&
210 !std::is_same<T, bool>::value>::type> {
211 static T convert(const dynamic& d) {
212 return folly::to<T>(d.asInt());
217 template <typename T>
218 struct DynamicConverter<T,
219 typename std::enable_if<std::is_enum<T>::value>::type> {
220 static T convert(const dynamic& d) {
221 using type = typename std::underlying_type<T>::type;
222 return static_cast<T>(DynamicConverter<type>::convert(d));
227 template <typename T>
228 struct DynamicConverter<T,
229 typename std::enable_if<std::is_floating_point<T>::value>::type> {
230 static T convert(const dynamic& d) {
231 return folly::to<T>(d.asDouble());
237 struct DynamicConverter<folly::fbstring> {
238 static folly::fbstring convert(const dynamic& d) {
245 struct DynamicConverter<std::string> {
246 static std::string convert(const dynamic& d) {
247 return d.asString().toStdString();
252 template <typename F, typename S>
253 struct DynamicConverter<std::pair<F,S>> {
254 static std::pair<F, S> convert(const dynamic& d) {
255 if (d.isArray() && d.size() == 2) {
256 return std::make_pair(convertTo<F>(d[0]), convertTo<S>(d[1]));
257 } else if (d.isObject() && d.size() == 1) {
258 auto it = d.items().begin();
259 return std::make_pair(convertTo<F>(it->first), convertTo<S>(it->second));
261 throw TypeError("array (size 2) or object (size 1)", d.type());
267 template <typename C>
268 struct DynamicConverter<C,
269 typename std::enable_if<
270 dynamicconverter_detail::is_container<C>::value>::type> {
271 static C convert(const dynamic& d) {
273 return C(dynamicconverter_detail::conversionIterator<C>(d.begin()),
274 dynamicconverter_detail::conversionIterator<C>(d.end()));
275 } else if (d.isObject()) {
276 return C(dynamicconverter_detail::conversionIterator<C>
278 dynamicconverter_detail::conversionIterator<C>
281 throw TypeError("object or array", d.type());
286 ///////////////////////////////////////////////////////////////////////////////
287 // DynamicConstructor specializations
290 * Each specialization of DynamicConstructor has the function
291 * 'static dynamic construct(const C&);'
295 template <typename C, typename Enable = void>
296 struct DynamicConstructor {
297 static dynamic construct(const C& x) {
304 struct DynamicConstructor<C,
305 typename std::enable_if<
306 dynamicconverter_detail::is_map<C>::value>::type> {
307 static dynamic construct(const C& x) {
308 dynamic d = dynamic::object;
309 for (auto& pair : x) {
310 d.insert(toDynamic(pair.first), toDynamic(pair.second));
318 struct DynamicConstructor<C,
319 typename std::enable_if<
320 !dynamicconverter_detail::is_map<C>::value &&
321 !std::is_constructible<StringPiece, const C&>::value &&
322 dynamicconverter_detail::is_range<C>::value>::type> {
323 static dynamic construct(const C& x) {
324 dynamic d = dynamic::array;
325 for (auto& item : x) {
326 d.push_back(toDynamic(item));
333 template<typename A, typename B>
334 struct DynamicConstructor<std::pair<A, B>, void> {
335 static dynamic construct(const std::pair<A, B>& x) {
336 dynamic d = dynamic::array;
337 d.push_back(toDynamic(x.first));
338 d.push_back(toDynamic(x.second));
343 ///////////////////////////////////////////////////////////////////////////////
346 template <typename T>
347 T convertTo(const dynamic& d) {
348 return DynamicConverter<typename std::remove_cv<T>::type>::convert(d);
352 dynamic toDynamic(const T& x) {
353 return DynamicConstructor<typename std::remove_cv<T>::type>::construct(x);
358 #endif // DYNAMIC_CONVERTER_H