0

Is there any way to serialize a class into json but only with fields I want to use in particular case without need of creating multiple variations of class? Let's take an example:

class User{

    @JsonField
    private String name;

    @JsonField
    private String surname;

    @JsonField
    private String hashedCode;

    @JsonField
    private String city;

    @JsonField
    private String anotherDummyString;
}

Now in one of my methods I would like to have a mapping to json Object which only contains name, city and anotherDummyString.

In Second method I want to have surname and city. In third method ... .

Is there any pleasant and neat way to achive this? I was wondering if I can "hide" these fields which I don't need.

Michal
  • 1
  • 3
  • You are right, my mistake, updated post – Michal Sep 24 '18 at 18:30
  • Whether the JSON serializer can do this depends entirely on *which* JSON library you're using, don't you think? – Andreas Sep 24 '18 at 18:33
  • 1
    See [Serialize Only Fields that meet a Custom Criteria with Jackson](https://www.baeldung.com/jackson-serialize-field-custom-criteria) – Andreas Sep 24 '18 at 18:34
  • Possible duplicate of [How to include only specific properties when serializing with Jackson](https://stackoverflow.com/q/26509033/5221149), or [How do I exclude fields with Jackson not using annotations?](https://stackoverflow.com/q/13764280/5221149) – Andreas Sep 24 '18 at 18:36
  • Which library are you using to do the serialization/deserialization? Can any library be used for a valid answer? – f-CJ Sep 25 '18 at 07:23

2 Answers2

0

Are you looking for something like this? (Using javax.json)

JsonObject getJson1() {
    return Json.createObjectBuilder().add("name", this.name).add("city", this.city).build();
}
JsonObject getJson2() {
    return Json.createObjectBuilder().add("surname", this.surname).add("city", this.city).build();
}

Just .add what you need in each function call.

luk2302
  • 55,258
  • 23
  • 97
  • 137
Nicholas Pipitone
  • 4,002
  • 4
  • 24
  • 39
0

I hope you are looking for a kind of filter for your fields in Class

This can be achieved using Jackson @JsonFilter

package com.concretepage;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonFilter("student")
public class Student 
{
    @JsonProperty("name")
    private String name;

    @JsonProperty("surname")    
    private String surname;

    @JsonProperty("hashedCode") 
    private String hashedCode;

    @JsonProperty("city")   
    private String city;

    @JsonProperty("anotherDummyString") 
    private String anotherDummyString;

}  

Create a simple filter for you above class

SimpleFilterProvider filterProvider = new SimpleFilterProvider();
filterProvider.addFilter("student",
        SimpleBeanPropertyFilter.serializeAllExcept("name", "city","anotherDummyString")); 

Set it to a object mapper

ObjectMapper mapper = new ObjectMapper();
mapper.setFilterProvider(filterProvider); 

To get the json message

//Pass the student object  
String jsonData = mapper.writerWithDefaultPrettyPrinter()
               .writeValueAsString(student);

You must able to have you 2 variance of class by creating one more sample filter like above

rahul shetty
  • 126
  • 7