java - 使用 JSON 模式在 Java 中创建和序列化数据

标签 java json

我正在寻找将现有 JSON 架构文件加载到我的 java 应用程序中的方法,以便我可以使用它将传入数据序列化为 JSON 对象以输出到外部应用程序。

问题是我无法为 JSON 创建类,因为我希望能够提供不同的架构文件并让程序创建不同的对象以供使用。这样,如果出站数据需要更改,唯一的更新是在架构文件中,并且不需要将新类添加到项目中。或者,如果我将数据输出到具有不同要求的不同应用程序,我只需要更改架构文件。

这是我将使用的架构示例:

{
    "description": "cashierBalance",
    "properties": {
        "transactionDate": {
            "type": "string",
            "description": "The date and the transaction was provided by the Point of Sale system.",
            "format": "date-time"
        },
        "shiftDate": {
            "type": "string",
            "description": "The date of the cashier work shift for this transaction",
            "format": "date-time"
        },
        "revenueCenter": {
            "type": "array",
            "description": "An array of all revenue centers worked by the cashier during the referenced shift."
        },
        "cashValues": {
            "type": "object",
            "properties": {
                "cashSales": {
                    "type": "string",
                    "description": "Sum of the sales (including) taxes that occurred in Cash during the shift."
                },
                "cashReturns": {
                    "type": "string",
                    "description": "Sum of the cash returned (e.g. change)"
                },
                "cashDrops": {
                    "type": "string",
                    "description": "Sum of the cash taken from cash drawer and placed in a drop box or safe."
                },
                "cashPayout": {
                    "type": "string",
                    "description": "Sum of cash removed from the cash drawer, exluding cashReturns or cashTips, during the shift."
                },
                "cashPayin": {
                    "type": "string",
                    "description": "Sum of cash put into the cash drawer after initial opening."
                },
                "cashTips": {
                    "type": "string",
                    "description": "Sum of cash paid out from the drawer for tips."
                },
                "cashTotal": {
                    "type": "string",
                    "description": "Total amount of cash in the drawer at the close of the shift."
                },
                "cashCurrency": {
                    "type": "string",
                    "description": "The currency of the cash for the elements in this object."
                }
            },
            "required": [
                "cashSales",
                "cashReturns",
                "cashTotal",
                "cashCurrency"
            ]
        },
        "cashierInfo": {
            "type": "object",
            "properties": {
                "cashierID": {
                    "type": "string",
                    "description": "The identifier of the Cashier."
                },
                "cashierName": {
                    "type": "string",
                    "description": "The name of the Cashier."
                }
            },
            "required": [
                "cashierID",
                "cashierName"
            ]
        }
    },
    "required": [
        "transactionDate",
        "shiftDate",
        "revenueCenter",
        "cashValues",
        "cashierInfo"
    ]
}

最佳答案

JSON 可以使用 Jackson 中的对象映射器轻松反序列化为通用对象但是,除非您在反序列化时通过指示结构来提供,否则它们不会有更具体的类信息。或者,您可以只使用您使用的库提供的基本 JSON 对象。

假设您已经拥有支持 JSON 中所有内容的类,如果您在 json 中以某种方式指示 JSON 中的每个对象应该属于哪个类,您可以根据读入创建一个自定义解串器并映射到这些类。

您无法(轻松)即时创建新的、丰富的类并使其可供实例化。 It's possible但我猜测您需要在 json 中提供的详细信息量来处理非平凡的类将是疯狂的(有什么方法,它支持的任何父类或接口(interface),等等...)。如果父类本身位于 json 中,事情会变得更加棘手。您有很多模式需要跟踪。我不知道有任何非 alpha 库可以做到这一点。

关于java - 使用 JSON 模式在 Java 中创建和序列化数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49201228/

相关文章:

php - 如何让 Zend_JSON 创建对象而不是数组

javascript - 读取一堆 JSON 文件,转换它们,然后保存它们

.net - 命名空间 'Json' 中不存在类型或命名空间名称 'System'(您是否缺少程序集引用?)

javascript - D3 错误 - 未捕获的 TypeError : Cannot read property 'children' of undefined

java - 如何在定义的线程(池)上运行类的方法?

java - JPA继承两个或多个父类(super class)

java - 使用 Spring MVC 在 jsp 文件中显示 XML

json - 可以将来自SQL查询的数据插入elasticsearch吗?

java - 如何在Android中正确初始化对象/数据

java - Android 音频环回