1 `folly/DynamicConverter.h`
2 --------------------------
4 When dynamic objects contain data of a known type, it is sometimes
5 useful to have its well-typed representation. A broad set of
6 type-conversions are contained in `DynamicConverter.h`, and
7 facilitate the transformation of dynamic objects into their well-typed
13 Simply pass a dynamic into a templated convertTo:
16 dynamic d = { { 1, 2, 3 }, { 4, 5 } }; // a vector of vector of int
17 auto vvi = convertTo<fbvector<fbvector<int>>>(d);
23 convertTo naturally supports conversions to
25 1. arithmetic types (such as int64_t, unsigned short, bool, and double)
26 2. fbstring, std::string
27 3. containers and map-containers
31 convertTo<Type> will assume that Type is a container if
32 * it has a Type::value_type, and
33 * it has a Type::iterator, and
34 * it has a constructor that accepts two InputIterators
36 Additionally, convertTo<Type> will assume that Type is a map if
37 * it has a Type::key_type, and
38 * it has a Type::mapped_type, and
39 * value_type is a pair of const key_type and mapped_type
41 If Type meets the container criteria, then it will be constructed
42 by calling its InputIterator constructor.
47 If you want to use convertTo to convert dynamics into your own custom
48 class, then all you have to do is provide a template specialization
49 of DynamicConverter with the static method convert. Make sure you put it
59 explicit Token(int kind, const fbstring& lexeme)
60 : kind_(kind), lexeme_(lexeme) {}
63 template <> struct DynamicConverter<Token> {
64 static Token convert(const dynamic& d) {
65 int k = convertTo<int>(d["KIND"]);
66 fbstring lex = convertTo<fbstring>(d["LEXEME"]);