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);
}
};
}

DynamicConverter的更多相关文章

  1. Slf4j MDC 使用和 基于 Logback 的实现分析

    前言 如今,在 Java 开发中,日志的打印输出是必不可少的, 关于  有了日志之后,我们就可以追踪各种线上问题.但是,在分布式系统中,各种无关日志穿行其中,导致我们可能无法直接定位整个操作流程.因此 ...

  2. Folly: Facebook Open-source Library Readme.md 和 Overview.md(感觉包含的东西并不多,还是Boost更有用)

    folly/ For a high level overview see the README Components Below is a list of (some) Folly component ...

随机推荐

  1. asp.net string有多行文字

    用如下格式设置

  2. 桔子桑Blog(小程序)V 0.4

    这两天对这个个人博客小程序的UI又作了一些补充,目前看来,小程序的主要功能如下: 1.博客/日常栏目的导航切换 为了避免两个模块的UI上的过于单一,我将两个模块的列表页作了区分: 边距是自适应的(针对 ...

  3. CUDA Samples: green ball

    以下CUDA sample是分别用C++和CUDA实现的生成的绿色的球图像,并对其中使用到的CUDA函数进行了解说,code参考了<GPU高性能编程CUDA实战>一书的第五章,各个文件内容 ...

  4. 类加载器:ClassLoader与Class的区别

    1.类加载器 java字节码(类)的加载是由虚拟机来完成的,虚拟机把描述类的Class文件加载到内存,并对数据进行校验.解析和初始化,最终形成能被java虚拟机直接使用的java类型,这就是虚拟机的类 ...

  5. cocoapods导入第三方库后,xcode上import不提示,找不到第三方库的解决办法

    选择你的工程tagets, -> Build Settings -> Search Paths -> User Header Search Paths 双击User Header S ...

  6. CuratorFramework入门指南

    CuratorFramework入门指南 原文地址:https://github.com/Netflix/curator/wiki/Getting-Started CuratorFramework作为 ...

  7. 【剑指offer】翻转单词顺序,C++实现

    原创博文,转载请注明出处! 本题牛客网地址 本题代码的github地址 本系列文章的索引地址 # 题目 # 思路       两次翻转,第一次翻转整个句子,第二次翻转每个单词(单词之间用逗号隔开) # ...

  8. WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递 【转】

    原文:http://www.cnblogs.com/lori/p/4045633.html 下面定义一个复杂类型对象 public class User_Info { public int Id { ...

  9. c/c++程序设计涉及的一些知识点

    c/c++程序设计涉及的一些知识点 c中的printf函数 main(){ int b = 3; int arr[]= {6,7,8,9,10}; int * ptr = arr; *(ptr++) ...

  10. 初探babel转换器的安装与使用

    一.配置.babelrc文件(没有名字的文件) Babel的配置文件是.babelrc,存放在项目的根目录下.使用Babel的第一步,就是配置这个文件. 基本格式如下: { "presets ...