mardi 21 juin 2016

Designing json to save changing data

I used to save data from a java application by using JSON. Basically, the application was a complex form. Each value entered by the user was stored with the field name as the key. I came across a problem when the keys were changed when the application became more complex. Per example,the application had a field "Optionnal Module", so the JSON looked like:

"Optional Module:": "Yes"

Then, we added another "Optional Module", so we had

"Optional Module 1:": "Yes",
"Optional Module 2:": "No"

In the code, I was loading directly the value into the textfield, like optModule.setText(parsedJSON.get("Optionnal Module"));

But when I added the second module, it changed to

if (parsedJSON.get("Optionnal Module") != null) {
    optModul1.setText(parsedJSON.get("Optionnal Module"));
} else {
    optModul1.setText(parsedJSON.get("Optionnal Module 1"));
}

As you can see, the code can get confusing with all changes made to the application.

I can surely use a Facade design pattern to avoid keep the class simple, but I still have another class to maintain...

I then thought I could had an ID instead of the text field name for the JSON key, which is working. Is it a good practice? Is there another way to deal with these changes?

I hope I'm understable.

Thanks!

Aucun commentaire:

Enregistrer un commentaire