使用JSON模式在java

时间:2018-03-09 19:54:21

标签: 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"
    ]
}

1 个答案:

答案 0 :(得分:0)

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

假设您的类已经支持您在JSON中拥有的任何内容,如果在json中您以某种方式指示JSON中每个对象应该是哪个类,您可以创建自定义deserailizer从您的阅读和映射到这些类。

您无法(轻松)动态创建新的丰富类,并使其可用于实例化。 It's possible但是我猜测你需要在json中提供处理非平凡类的细节数量是多么疯狂(有什么方法,它支持的任何父类或接口,等...)。如果父类本身在json中,则会变得更加棘手。您有很多要跟踪的架构。我不知道有任何非alpha库可以做到这一点。