question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need field-specific information in custom JsonSerializer

See original GitHub issue
In 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:open
  • Created 9 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
narramadancommented, May 27, 2020

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.

1reaction
mishrabhilashcommented, Mar 13, 2019

plus one for the feature

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found