When should an object be used instead of a json object? When should an object be used instead of a json object? json json

When should an object be used instead of a json object?


If you are coming from a Objective-C / PHP (or some other language) dictionaries, or other kind of map, with key/value pairs are common.

But in Java, typically, strong typed object are preferred : sometimes referred as POJOs (Plain Old Java Objects). Java is an Object Oriented language, and you get benefits from OO concepts down the line. Not always obvious for very small applications, but usually it's the way to go. Strong typed object involves a bit more boiler plate code, but any modern IDE will do it for you (generate getters/ setters). So this is no argument against it.

On top of it, it is VERY EASY to store POJO object from/to JSON, using Jackson / GSon / Moxy. There are many good JSON providers / libraries that work with POJO without any config (or very few).

Other advantages I can see off hand :

  • When using pojo IDE will also shows you availables getters / setters while coding.

  • Today you can store your "objects" in JSON on disk, tomorrow you might store them in a database. You can switch from Jackon serialization (or your preferred JSON library) to JPA easily, with a few set of annotations.

  • Many frameworks (JAX-RS, JPA, JAXB) expects strong typed object, and will use reflections or similar methods to inspect and handle your objects.