json数据存储与版本控制 [英] json data storage with versioning

查看:319
本文介绍了json数据存储与版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个Java服务器存储可以映射到Java的 JSON 数据类。 Java类可能会发生变化。我们的目标是能够更新Java类,并且仍然能够将旧版本的 JSON 数据解码为新版本的Java对象。应该有一个好的版本控制系统。例如,可以使用默认值将新字段添加到Java类。在旧的 JSON 数据没有这个字段的情况下,Java对象可以使用该字段的默认值。

There is a Java server which stores JSON data that can be mapped to a Java Class. The Java Class is subject to changes. The goal is to be able to update the Java Class and still be able to decode the JSON data of an older version to a Java Object of a newer version. There should be a good versioning system in place. For example the ability to add a new field to the Java Class with a default value. In the case the old JSON data doesn't have this field, the Java object can use the default value of the field.

  • I've seen GSON, but the versioning support is too limited.

我错过了什么吗?是否还有其他库有更好的版本支持?

Did I miss something? Are there other libraries that have a better versioning support?


  • 如何存储 JSON code> data?

  • How should one store that JSON data?

我看到两个选项。选项1:我可以在文件夹中分别存储 JSON 文件(如果它们具有相同版本,则具有共同结构)。例如,我有2个文件:json / j1.json和json / j2.json。选项2:我可以使用 NoSQL 数据库,例如 MongoDB 。但是这比以前的选择有优势吗?在 DBCollection 中请求 BasicDBObject 是没有意义的,因为我需要它能够将它转换为另一个Java类。对于这种情况,建议使用哪种选项?也许另一种选择更适合?

I see two options. Option 1: I can store those JSON files (which have a common structure if they have the same version) separately in a folder. For example I have 2 files: json/j1.json and json/j2.json. Option 2: I can use a NoSQL Database such as MongoDB. But Does this have an advantage over the previous option? It is pointless to ask for a BasicDBObject in a DBCollection since I need it to be able to cast it to another Java Class. Which option is recommended for this case? Maybe another option is better suited?

推荐答案

在撰写本文时,没有内置的杰克逊支持(2.9.x ),但是,杰克逊模型版本控制模块添加了可用于版本控制的支持在映射到POJO之前将旧版本的JSON模型转换为当前版本(反之亦然)。

There is no built-in Jackson support at the time of writing this (2.9.x), however, the Jackson Model Versioning Module adds versioning support which can be used to convert old versions of JSON models to the current version (and vice-versa) before being mapped to the POJO.

请参阅Jackson 2对版本的支持,以举例说明GSON的基本版本功能如何映射到该模块。有关完整功能的示例,请参阅模块的GitHub页面

See my answer on Jackson 2 support for versioning for an example of how GSON's basic versioning functionality can be mapped to this module. See the module's GitHub page for examples of the full functionality.

免责声明:我是该模块的作者。我还编写了 Spring MVC ResponseBodyAdvise 来使用该模块。

Disclaimer: I am the author of this module. I have also written a Spring MVC ResponseBodyAdvise for using the module.

这篇关于json数据存储与版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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