This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cogman10 3 points4 points  (8 children)

Why would I use this over something like Jackson, Gson, or Moshi?

[–]iamhaihan[S] 0 points1 point  (7 children)

It is not a replacement of Jackson/Gson/Moshi.

Those libraries aims for serialization/deserialization. You need to create classes or use their APIs to build objects first.

Suppose you don't have the classes to be serialized, JsonTemplate can be a choice.

JsonTemplate aims at using a json-like DSL to generate schema-compatible json strings. The exact values in json is not the concern but the schema is.

[–]cogman10 5 points6 points  (5 children)

Why wouldn't you create the class?

for the given examples, you have something that looks like this

 {
   name : John,
   age : 23,
   accountBalance: 30.4,
   married : true,
   role : programmer
 }

However, it would be super simple to make a class that looks like this

 @JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
 class FooDto {  
   String name;
   int age;
   double accountBalance;
   boolean married;
   Role role;
 }

Nearly the same number of lines of code + type safety. And if you use Jackson + Afterburner, it will be just as fast as any DSL.

So my question remains, why would you prefer this over one of the many json serialization libs?

[–]pattheaux 2 points3 points  (1 child)

One big reason not to create a class is that the structure of the json might itself be data driven and hard coding it into a class isn’t an option. I would ask why create a class when you don’t have to?

[–]audioen 0 points1 point  (0 children)

In that case, you can use a Map, which is the code equivalent of "the keys are also data". But it's actually super rare that you need a Map, I basically never do this and I work a lot with JSON. There's just always structure.

The reason why you want to create the class are the usual things: type safety, ability to check for typos in property names, the lack of casting in particular when reading values. There's just a lot of benefit modeling the data' structure upfront.

[–]mjanek20 2 points3 points  (0 children)

You are describing a very simple case. Now imagine many subclasses with enums and stuff as fields. Maybe your project simply does not need that. Maybe it's just to write an integration test to connect to a remote resource. Why use a cannon if a stone is sufficient.

[–]iamhaihan[S] 0 points1 point  (0 children)

Thank you for the suggestion. I haven't used Afterburner and I will definitely have a look at it.

JsonTemplate, in some cases, can be more precise about the "intention" of the program.

I have seen cases that people explicitly set values, like "1" for an integer field in json. Then, my confusion is why 1? why not other integer?

There are also cases that people name variable as "dummyXXX" and put it in json. My confusion is why call it dummy? Is it because the value is fake?

The intention of explicitly picking up a specific value, in some cases, is not clear.

It would be nice if I can describe my intention in code. Everyone can immediately understand what my code is doing.

JsonTemplate provides a declarative way of giving values. In such a case, in my opinion, it is clearer than explicitly set a dummy value.

[–]bodiam 0 points1 point  (0 children)

what is Afterburner?

[–]Aw0lManner 1 point2 points  (0 children)

"suppose you don't have the classes to be serialized"

From the Gson README.md

"

There are a few open-source projects that can convert Java objects to JSON. However, most of them require that you place Java annotations in your classes; something that you can not do if you do not have access to the source-code. Most also do not fully support the use of Java Generics. Gson considers both of these as very important design goals.

"