folly/DynamicConverter.h

When dynamic objects contain data of a known type, it is sometimes useful to have its well-typed representation. A broad set of type-conversions are contained in DynamicConverter.h, and facilitate the transformation of dynamic objects into their well-typed format.

Usage


Simply pass a dynamic into a templated convertTo:

    dynamic d = { { , ,  }, { ,  } }; // a vector of vector of int
auto vvi = convertTo<fbvector<fbvector<int>>>(d);

Supported Types


convertTo naturally supports conversions to

  1. arithmetic types (such as int64_t, unsigned short, bool, and double)
  2. fbstring, std::string
  3. containers and map-containers

NOTE:

convertTo will assume that Type is a container if

  • it has a Type::value_type, and
  • it has a Type::iterator, and
  • it has a constructor that accepts two InputIterators

Additionally, convertTo will assume that Type is a map if

  • it has a Type::key_type, and
  • it has a Type::mapped_type, and
  • value_type is a pair of const key_type and mapped_type

If Type meets the container criteria, then it will be constructed by calling its InputIterator constructor.

Customization


If you want to use convertTo to convert dynamics into your own custom class, then all you have to do is provide a template specialization of DynamicConverter with the static method convert. Make sure you put it in namespace folly.

Example:

    struct Token {
int kind_;
fbstring lexeme_; explicit Token(int kind, const fbstring& lexeme)
: kind_(kind), lexeme_(lexeme) {}
};
namespace folly {
template <> struct DynamicConverter<Token> {
static Token convert(const dynamic& d) {
int k = convertTo<int>(d["KIND"]);
fbstring lex = convertTo<fbstring>(d["LEXEME"]);
return Token(k, lex);
}
};
}

最新文章

  1. ffmpeg-20160508-git-bin
  2. Gradle tip #3: Tasks ordering
  3. SQLServer学习笔记&lt;&gt;相关子查询及复杂查询
  4. C# 数组CopyTo
  5. 真相:中国版BBB用USB连电脑没有盘符的根本原因分析
  6. ListView列表拖拽排序
  7. FJ省队集训DAY1 T1
  8. 测试jsp
  9. EXT JS 4.3 在线学习
  10. MySQL的SELECT ...for update
  11. zookeeper 事务日志
  12. day 22 二十二、面向对象导入、名称空间、类与对象
  13. CSP201312-4 有趣的数【dp】
  14. [android] 获取系统的联系人信息
  15. L259
  16. 【opencv基础】OpenCV installation stuck at [ 98%] Built target opencv_perf_stitching with no error
  17. xampp 教程
  18. C/C++笔记 #035# Makefile
  19. shell作业后台执行的方法
  20. java学习笔记—标准连接池的实现(27)

热门文章

  1. pig 自定义udf中读取hdfs 文件
  2. 深入理解Feign之源码解析
  3. 1. Java EE简介 - JavaEE基础系列
  4. Shell 批量搜索关键词并保存结果到文件中(数组、循环)
  5. grunt使用
  6. iOS怎么来实现关闭自动锁屏
  7. 使用vue
  8. 异步任务中的重新进入(Reentrancy)
  9. HTTP Client Performance Improvements
  10. LG4169 [Violet]天使玩偶/SJY摆棋子