Showing posts with label json. Show all posts
Showing posts with label json. Show all posts

Monday, January 13, 2014

Annotations for Ignoring properties In JSON - Jackson

For my reference....

Sometimes POJOs contain properties that you do not want to write out, so you can do:

public class Value {
  public int value;
  @JsonIgnore public int internalValue;
}
and get JSON like:
{ "value" : 42 }

or, you may get properties in JSON that you just want to skip: if so, you can use:

@JsonIgnoreProperties({ "extra", "uselessValue" })
public class Value {
  public int value;
}
which would be able to handle JSON like:
{ "value" : 42, "extra" : "fluffy", "uselessValue" : -13 }

Finally, you may even want to just ignore any "extra" properties from JSON (ones for which there is no counterpart in POJO). This can be done by adding:

@JsonIgnoreProperties(ignoreUnknown=true)
public class PojoWithAny {
  public int value;
}

Reference:
http://jackson.codehaus.org/

Friday, January 3, 2014

JSON Schema to Model Converter


JSON schema/Example to model converter
http://www.jsonschema2pojo.org/

This supports Jackson and Google gson JSON libraries.

JSON Viewer
http://jsonviewer.stack.hu/


Example: for my project JSON example
{
  "projectName":"TestProject",

  "properties": {
    "createdBy": "Kondal Kolipaka",
    "createdTime": "21-01-2013",
    "lastModifiedTime":"21-02-2013"
  }
}


Generated Model for above Project JSON example:

package com.kk;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("com.googlecode.jsonschema2pojo")
@JsonPropertyOrder({
"projectName",
"properties"
})
public class Project {

@JsonProperty("projectName")
private String projectName;
@JsonProperty("properties")
private Properties properties;
private Map<StringObject> additionalProperties = newHashMap<StringObject>();

@JsonProperty("projectName")
public String getProjectName() {
return projectName;
}

@JsonProperty("projectName")
public void setProjectName(String projectName) {
this.projectName = projectName;
}

@JsonProperty("properties")
public Properties getProperties() {
return properties;
}

@JsonProperty("properties")
public void setProperties(Properties properties) {
this.properties = properties;
}

@JsonAnyGetter
public Map<StringObject> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperties(String nameObject value) {
this.additionalProperties.put(namevalue);
}

}
-----------------------------------com.kk.Properties.java-----------------------------------

package com.kk;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("com.googlecode.jsonschema2pojo")
@JsonPropertyOrder({
"createdBy",
"createdTime",
"lastModifiedTime"
})
public class Properties {

@JsonProperty("createdBy")
private String createdBy;
@JsonProperty("createdTime")
private String createdTime;
@JsonProperty("lastModifiedTime")
private String lastModifiedTime;
private Map<StringObject> additionalProperties = newHashMap<StringObject>();

@JsonProperty("createdBy")
public String getCreatedBy() {
return createdBy;
}

@JsonProperty("createdBy")
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}

@JsonProperty("createdTime")
public String getCreatedTime() {
return createdTime;
}

@JsonProperty("createdTime")
public void setCreatedTime(String createdTime) {
this.createdTime = createdTime;
}

@JsonProperty("lastModifiedTime")
public String getLastModifiedTime() {
return lastModifiedTime;
}

@JsonProperty("lastModifiedTime")
public void setLastModifiedTime(String lastModifiedTime) {
this.lastModifiedTime = lastModifiedTime;
}

@JsonAnyGetter
public Map<StringObject> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperties(String nameObject value) {
this.additionalProperties.put(namevalue);
}

}

As we can see it also generates getAdditionalProperties(), this would be very much useful later point of time if we wanted to send some additional information without really modifying the existing structure.