Giter Club home page Giter Club logo

thrift-json-rebuilder's Introduction

ThriftJsonRebuilder

License Last Commit Language

Human Readable JSON To Thrift POJO「Base Apache Thrift 0.9.2

Contributors

一、痛点

  • 目前涉及 Thrift POJOJSON 转化的 Apache Thrift 原生 Protocol 只有两种:TSimpleJSONProtocolTJSONProtocol
    • TSimpleJSONProtocol 能把 Thrift POJO 转换成 Human Readable JSON ,但是没法把 JSON 反序列化成 Thrift POJO
    • TJSONProtocol 可以在 Thrift POJOJSON 之间相互转换,但是这个 JSON 没有字段的名称信息,是以字段 ID 作为名称,这个 JSON 对人来说几乎是不可读的,无法提供给用户进行修改

二、解决方案

2.1 主要解决两个问题

  1. 完成 Thrift POJOHuman Readable JSON 再把 Human Readable JSONThrift POJO 的转换
  2. 提供统一的解决方案能够支持泛化的 Thrift POJO,即:只要有 .thrift 文件通过 Thrift compile 生成的 .java,就可开箱即用

2.2 具体实现思路

  • 基于痛点的问题,最终考虑通过把 TSimpleJsonProtocol 生成的 Human Readable JSON 进行 Rebuild 处理成 TJSONProtocol 支持的形式完成 JSONThrift POJO 的转换 IMG
  • 具体设计思路如上图,结合 Thrift POJOJSON 中的信息,提取出字段的 名称类型IDSize 等信息
    1. 使用 GsonJsonReader 读取输入 JSON
    2. 利用 JSON 中字段 Name 通过递归反射的方式来获取到 Thrift POJO Class 中的 TypeID 等信息
    3. 同时通过遍历 JSON 的方式来获取到 Size 信息
    4. 将两部分信息组合到临时的数据结构 ThriftMeta 中,动态生成 TJSONProtocol 能够处理的 JSON,完成反序列化

2.3 其他处理

由于 TSimpleJsonProtocol 在序列化 binary 类型的字段的时候是直接采用了 toString 方法,而 TJsonProtocol 在序列化 binary 类型的字段的时候则是使用了 Base64 编码,经过 Rebuild 处理会导致 binary 字段不可用的问题

  • 基于上述问题,重写了 TSimpleJsonProtocol ,修改了写 binary 类型字段的方法,详情参考 NewTSimpleJsonProtocol.java

thrift-json-rebuilder's People

Contributors

zzkzzk1996 avatar

Stargazers

 avatar 曹建发 avatar

Watchers

James Cloos avatar 曹建发 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.