Java:JSON->原虫反向转换 [英] Java: JSON -> Protobuf & back conversion

查看:120
本文介绍了Java:JSON->原虫反向转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的系统,该系统在GUI和服务器之间使用基于 protobuf的通信协议.现在,我想添加一些持久性,但是目前protobuf消息已直接转换为第三方自定义对象.

I have an existing system, which is using protobuf-based communication protocol between GUI and server. Now I would like to add some persistence, but at the moment protobuf messages are straight converted to a third-party custom objects.

是否有一种方法可以将 proto 消息转换为 json ,然后可以将其持久保存到数据库中.

Is there a way to convert proto messages to json, which could be then persisted to database.

注意::我不太喜欢将二进制protobuf写入数据库的想法,因为有一天它可能与新版本不向后兼容,从而破坏了系统.

N.B.: I don't much like an idea of writing binary protobuf to database, because it can one day become not backward-compatible with newer versions and break the system that way.

推荐答案

我们当前正在使用 protobuf-java-格式以将我们的Protobuf消息(Message的任何子类)转换为JSON格式以通过我们的Web API发送.

We are currently using protobuf-java-format to convert our Protobuf messages (anything subclass of Message) into a JSON format to send over our web API.

只需:

  JsonFormat.printToString(protoMessage)

这篇关于Java:JSON->原虫反向转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆