1

I am looking for ways that I can load an existing JSON schema file into my java application so that I can use it to serialize incoming data into a JSON object to be output to an external application.

The issue is that I can not create a class for the JSON as I want to be able to provide different schema files and have the program then create different objects for use. This way if the outbound data needs to change the only update is in the schema file, and no new classes need to be added to the project. Or if I am outputting data to a different application that has different requirements, I only need to change the schema file.

Here is an example of the schema I will be using:

{
    "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"
    ]
}
Eric P
  • 13
  • 5
  • You can start by looking into [JsonSchema2Pojo](https://github.com/joelittlejohn/jsonschema2pojo). – Chetan Jadhav CD Mar 09 '18 at 20:01
  • 2
    You do not need to have the JSON ever become a POJO you can just keep it as a JsonElement within your Java code. – bhspencer Mar 09 '18 at 20:09
  • 1
    You can still use a JsonSchema to validate the JsonElement if you want to. Besides if you could have the Json deserialized into some new Class of JavaObject what would you do with it. None of your existing java code would know how to interact with it. – bhspencer Mar 09 '18 at 20:11

1 Answers1

0

JSON can easily deserialize into generic Objects using the object mapper in Jackson, however, they'll have no more specific class info unless you provide at time of deserialization by indicating the structure. Or you can just use the base JSON object that whichever library you use gives you to work with.

Assuming you've the classes already to support whatever you've got in your JSON, if in the json you indicate somehow which class each object in the JSON is supposed to be, you could create a custom deserailizer from your read in and map to those classes.

You cannot (easily) create new, rich classes on the fly and make them available to be instantiated. It's possible but I'm guessing the amount of detail you'd need to provide in the json to handle non-trivial classes would be insane (what method there are, any parent classes or interfaces it supports, etc...) . Gets even trickier if the parent classes themselves are in the json. You've got lots of schema to track. I'm not aware of any non-alpha library that does this.

Ray
  • 40,256
  • 21
  • 101
  • 138
  • The JSON I am dealing with will only be holding basic data like integers and strings, or at the most complex an array or int/strings. I have edited my post to include an example of the schema I will be using. – Eric P Mar 12 '18 at 13:03
  • 1
    Thank you for the recomendation for Jackson. After reading lots of documentation and tutorials on it, I was able to parse the JSON schema input into a generic JSON object to output. There is much more work to do but this was the starting point I needed. – Eric P Mar 12 '18 at 18:41