Need field-specific information in custom JsonSerializer
See original GitHub issueIn my use case, I need to truncate some long String to fit the need of mobile
client while still keeping the full String for web.
So I built two separate Gson objects for web and mobile, and specify some
annotation on String field which needs to be truncated:
class Foo {
@MobileJsonTruncate(70)
String message;
}
But in the custom JsonSerializer, I have little information about the field
being serialized, thus I can't get the annotation to determine whether to
truncate and how long to keep.
Ideally, if we have the FieldAttributes information as a parameter of
serialize() method or stored within JsonSerializationContext, that will be a
easy job.
But now, I have to write an ugly work around like this:
class MobileJsonSerializer implements ExclusionStrategy, JsonSerializer<String>
{
@Override
public boolean shouldSkipField(final FieldAttributes aField) {
iField = aField;
return aField.getAnnotation(WebExclusive.class) != null;
}
@Override public JsonElement serialize(final String aSrc, final Type aTypeOfSrc, final JsonSerializationContext aContext) {
final MobileTruncate annotation = iField.getAnnotation(MobileTruncate.class);
if (annotation == null)
return aContext.serialize(aSrc);
final JsonPrimitive result = new JsonPrimitive(aSrc.substring(0, Math.min(aSrc.length(), annotation.value())));
iField = null;
return result;
}
@Override public boolean shouldSkipClass(final Class<?> clazz) { return false; }
private FieldAttributes iField;
}
Original issue reported on code.google.com by oasisfeng
on 1 Apr 2011 at 1:34
Issue Analytics
- State:
- Created 9 years ago
- Comments:11
Top Results From Across the Web
Serialize Only Fields that meet a Custom Criteria with Jackson
This tutorial is going to illustrate how we can use Jackson to only serialize a field if it meets a specific, custom criteria....
Read more >Jackson JSON custom serialization for certain fields
You can implement a custom serializer as follows: public class Person { public String name; public int age; @JsonSerialize(using ...
Read more >How to customize property names and values with System ...
In this article. Customize individual property names; Use camel case for all JSON property names; Use a custom JSON property naming policy ...
Read more >Jackson Tips: custom List serialization | by @cowtowncoder
... custom JsonSerializer) Jackson-databind allows a wide range of ... custom JsonSerializer if bigger changes to output are required.
Read more >Creating custom serializer and deserializer classes - IBM
You can create custom Jackson serializer and deserializer classes to use instead of the default classes. Then, create a custom ObjectMapper to use...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Need this feature.
I have a specific use case to serialize OffsetDateTime to String, but for specific fields I need to consider only the date part.
plus one for the feature