Custom JSON Serialization With GSON
Here's a curious one, in which I found myself with a limitation of not being able to output JSON with scientific notation values.
Serializing JSON With GSON
Normally, serialization of JSON via GSON is so simple, a developer with merely a basic understanding of Java could pull it off 😜. Here's a sample for baseline comparison.
So, since the formatting of the numeric values, which I was passing into a
Doubles for serialization, we'll be registering a new
TypeAdapter with the
JsonSerializer and instruct it how to handle the value in place of the default. Here's the result, wrapped into a method in a utility class.
Now that we have a new way of handling our
Gson instantiation, we can use it as such:
I'm still left questioning why I had to do this, but the exercise is an intersting one anyway. Should someone need to customize their responses even further, this is a great place to start from. I'm still a pretty big fan of GSON and while it may not be for everyone, or even the best option in OSGi plugin development with options like JAX-RS and others, it's certainly a heavily used library that is both useful and generally easy to implement.