jdereg / json-io

Convert Java to JSON. Convert JSON to Java. Pretty print JSON. Java JSON serializer. Deep copy Java object graphs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide feature to serializer only fields having given access modifier

xeronix opened this issue · comments

I want to serialize fields with specific access modifier for all the classes. Currently there is no way to specify it.
In jackson JSON mapper there is option to specify field visibility for serialization.

Example:

public class TestA {
    private int a;
    protected int b;
    public int c;
}

To serialize only protected fields for all classes:

Map<String, Object> jsonWriterArgs = new HashMap<>();
jsonWriterArgs.put(JsonWriter.FIELD_VISIBILITY, FieldVisibility.PROTECTED);  
// this will serialize integer b

To serialize only protected+public fields for all classes:

Map<String, Object> jsonWriterArgs = new HashMap<>();
jsonWriterArgs.put(JsonWriter.FIELD_VISIBILITY, FieldVisibility.PROTECTED | FieldVisibility.PUBLIC);  
// this will serialize integer b and integer c

I believe this already exists. See the 'FIELD_SPECIFIERS' option.

FIELD_SPECIFIERS options uses a map of class as key and field name list as value.
My use case is to serialize only protected members of classes.

There are lot of classes and i need to find protected members for each one of them, create a list and then add it to the map for every class.

There is no global way of doing it as of now or i am missing something here.

json-io is open source. You are welcome to add this capability and appropriate unit tests. I will review and once successfully passing "muster", commit to main branch and release.

I have already modified it for my usage. I will try to merge it with test cases.

Please submit a pull request (if you haven't already) for your suggested change. Thanks!