Sambhav
Sambhav

Reputation: 227

Keep few fields in Json - Java

public class User {
String name;
String id;
Address[] address;
....
....
//40 more fields
}

public class Address {
String street;
String city;
String state;

}

I have a List and I need to convert it to json with only few fields.

public String fetchUsers(List<Users> users, List<String> fields) {
//fetch the list of users having the specific fields in the list and return as json
}

fields = ["name", "address.state"]

I can remove fields in json.... But, I need to keep restricted fields as per values passed in the method. I can use any third party lib as well.

Upvotes: 0

Views: 672

Answers (1)

Syed Khalid Ahmed
Syed Khalid Ahmed

Reputation: 3232

Use com.google.gson.Gson library for serialize your objects into json and you have to create ExclusionStrategy for your object for fields which you want to skip or not. create a GsonBuilder object from that and parse your object from it.

this is perfectly working fine.

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

 public class ABC {

  public class User {

    public String name;
    public String id;
    public Address[] address;
  }

  public class Address {

    public String street;
    public String city;
    public String state;

  }


  public static ExclusionStrategy createStrategy(List<String> fields) {
    return new ExclusionStrategy() {
      public boolean shouldSkipField(FieldAttributes fieldAttributes) {
        if (fields.stream().anyMatch(e -> e.equals(fieldAttributes.getName()))) {
          return false;
        }
        return true;
      }

      public boolean shouldSkipClass(Class aClass) {
        return false;
      }
    };
  }


  public String fetchUsers(List<User> users, List<String> fields) {
    GsonBuilder builder = new GsonBuilder();
    builder.setExclusionStrategies(createStrategy(fields));
    Gson gson = builder.create();
    return gson.toJson(users);
  }

  public static void main(String[] args) {
    ABC x = new ABC();

    Address add = x.new Address();
    add.city = "city";
    add.state = "state";
    add.street = "street";

    Address[] array = new Address[1];
    array[0] = add;

    User user = x.new User();
    user.address = array;
    user.id = "id";
    user.name = "name";

    List<User> users = new ArrayList<>();
    users.add(user);

    List<String> fields = Arrays.asList("name", "address", "state");
    String json = x.fetchUsers(users, fields);

    System.out.println(json);

  }


}

and output of this code is :

[{"name":"name","address":[{"state":"state"}]}]

and dependency for Gson is.

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.5</version>
    </dependency>

Upvotes: 1

Related Questions