Reputation: 1085
I have a json file that looks like this
{
"items" : [ {
"Name" : "India",
"FullName" : "India",
"Region" : "Asia"
}, {
"Name" : "USA",
"FullName" : "United States of America",
"Region" : "Americas"
}, {
"Name" : "UK",
"FullName" : "United Kingdon",
"DisplayLabel" : "Europe"
} ]
}
Pojo class looks like this
Countries.java
package model;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.xml.bind.annotation.XmlElement;
public class Countries {
@XmlElement( required = true )
@JsonProperty( "Name" )
private String name;
@XmlElement( required = true )
@JsonProperty( "FullName" )
private String fullName;
@XmlElement( required = true )
@JsonProperty( "Region" )
private String region;
public Countries(@JsonProperty( "Name" ) String name,
@JsonProperty( "FullName" ) String fullName,
@JsonProperty( "Region" ) String region) {
this.name = name;
this.fullName = fullName;
this.region = region;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getFullName() {
return fullName;
}
public void setRegion(String region) {
this.region = region;
}
public String getRegion() {
return region;
}
}
Items.java
package model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
@JsonIgnoreProperties( ignoreUnknown = true )
public class Items {
public Items() {
super();
}
@XmlElement( required = true )
@JsonProperty( "items" )
protected List<Countries> view;
public void setView(List<Countries> view) {
this.view = view;
}
public List<Countries> getView() {
return view;
}
}
I am using jackson APIs. Using ObjectMapper to serialise json. Ex.
Items views = mapper.readValue(jsonString, Items.class);
I cant to exclude those entries that have Region="Europe". How to achieve this. Thanks in advance.
Upvotes: 3
Views: 2897
Reputation: 7844
Jackson should be focused on serialization and deserialization, not executing application logic outside of that realm. You are likely better off deserializing the content and then iterating through the objects to apply your criteria.
List<Country> nonEuropean = views.getView().stream()
.filter(c -> !c.getRegion().equals("Europe"))
.collect(Collectors.toList());
Architecturally, keeping the scope of Jackson contained to translating JSON to Java and vis-versa allows you to use the same Jackson config throughout your entire project. In your current use you want to omit European countries, but in another area of your application you may want to include those entities.
Upvotes: 2
Reputation: 14383
The best I could come with, is a custom Deserialiser that filters out regions (that obviously can come under either "Region"
or "DisplayLabel"
) but the result collection will contain null entries that will require post processing to be removed:
Here is the complete custom Deserializer
package model;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
public class CountriesDeserializer extends JsonDeserializer<Countries> {
@Override
public Countries deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
String region = null;
JsonNode countryNode = p.getCodec().readTree(p);
JsonNode regionNode = countryNode.get("Region");
if (regionNode == null) regionNode = countryNode.get("DisplayLabel");
if (regionNode != null) region = regionNode.asText();
if ("Europe".equals(region) == false) {
return new Countries(countryNode.get("Name").asText(),
countryNode.get("FullName").asText(), region);
}
return null;
}
}
it needs to be specified on the collection property of Items
class:
@XmlElement( required = true )
@JsonProperty( "items" )
@JsonDeserialize(contentUsing = CountriesDeserializer.class)
protected List<Countries> view;
the result of calling the ObjectMapper
is a collection with one null entry.
Upvotes: 1