0

Working on a Spring Boot application, one of the functionality will include software update. It will be described in a JSON like this:

{
 "DeviceProfile": {
    "firmware": {
        "name": "firmware_a",
        "version": "2.0.24.3",
        "url": " ",
        "isPatch": true,
        "patchdependency":  "1.0.0"
    },
    "software": [
        {
            "name": "mySoftware1",
             "version": "1.0.0",
               "url": "http://www.example.com",
             "action": "install"
        },
        {
            "name": "mySoftware2",
            "version": "1.1.0",
            "url": "http://www.example.com",
            "action": "install"
        },
        {
            "name": "mySoftware3",
            "version": "2.0.0",
            "url": "http://www.example.com",
            "action": "install"
        }
    ],
    "configuration": [
        {
            "url": "https://www.examples.com",
            "type": "myConfigType_1",
            "name": "myConfig_1”
        },
        {
            "url": "https://www.examples.com",
            "type": " myConfigType_2",
        "name": "myConfig_2”
        }
    ]
 }
}

There are several conditions, that have to be fulfilled - the "configuration" list to contain exactly one element and its name to be a defined string. The name of each element in the software part should start with a defined prefix. There can be other additional conditions also. So I was wondering which is the best way to do this. I can create a DeviceProfile class to extract the data, using ObjectMapper, and then implement check for every single condition, but I was wondering whether there is better way to do this. Please advise also for unit testing of the approach.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
Ivajlo Iliev
  • 303
  • 1
  • 3
  • 19
  • You can try to incorporate [Javax Validation/JSR 380](https://www.baeldung.com/javax-validation) into your project. You can deserialise `JSON` payload to `Java` `POJO` and validate it or to mix these two together, see this question: [@Valid when creating objects with jackson without controller](https://stackoverflow.com/questions/55457754/valid-when-creating-objects-with-jackson-without-controller) – Michał Ziober Apr 27 '20 at 20:17
  • 1
    I second @MichałZiober just deserialize to a POJO with Jackson and use the validation annotations to verify. If you need more complex logic, then get rid of the validations and validate the pojo. I'll actually disagree with Michal on the "mix" idea. Use either/or not both to keep the validation logic all together in one place. – SledgeHammer Apr 27 '20 at 22:47
  • @SledgeHammer, yeah, I just wanted to show possibilities. To make it clear, we can separate these two steps and deserialise and validate by two separated components. – Michał Ziober Apr 27 '20 at 22:52

1 Answers1

0

Spring comes with an Annotation called @JsonDeserialize where you can have your own Deserializers and handle them the way you want. Take this class as an example:

public class CustomDeserializer extends JsonDeserializer<Long> {
    @Override
    public Long deserialize(JsonParser jsonParser, DeserializationContext 
         deserializationContext) throws IOException {


    }
}

You can extend the Deserializer class like above and use it deserialize your data like below:

 @JsonDeserialize(using = CustomDeserializer.class)
    private Long Id;
varijkapil13
  • 390
  • 4
  • 16
Sheldom
  • 21
  • 1
  • 4
  • Thanks, Sheldon, but actually my focus is on validating whether the data in the `JSON` match certain requirements. – Ivajlo Iliev Apr 27 '20 at 19:01
  • To my understanding If the data doesn't match the requirement u want to throw exception that u can deal here in that deserializer right?? – Sheldom Apr 28 '20 at 15:12