<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> </dependency>
CBOR encoded data with Jackson
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-cbor</artifactId> <version>2.9.6</version> </dependency>
In order to read and write MessagePack encoded data
<dependency> <groupId>org.msgpack</groupId> <artifactId>jackson-dataformat-msgpack</artifactId> <version>0.8.16</version> </dependency>
com.fasterxml.jackson.databind.ObjectMapper [databind has objectMapper]
ObjectMapper is because it maps JSON into Java Objects (deserialization), or Java Objects into JSON (serialization).
Custom Deserializer
1. Create SimpleModule
2. Register objectmapper.registerModule(module);
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public Car deserialize(JsonParser parser, DeserializationContext deserializer)throws IOException {
Custom Serializer
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public void serialize(Car car, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
JsonNode
com.fasterxml.jackson.databind.JsonNode
is Jackson's tree modelObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(json)
ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = readJsonIntoJsonNode(); String json = objectMapper.writeValueAsString(jsonNode);
- Read + Write Annotations
- is used to tell Jackson to ignore a certain property (field) of a Java object.
@JsonIgnore public long personId = 0;
- @JsonIgnoreType marks whole class as ignored
- @JsonAutoDetect inculdes private properties
- @JsonSetter use it when property name is not same as field name in class
public long getPersonId() { return this.personId; } @JsonSetter("id") public void setPersonId(long personId) { this.personId = personId; }
- Write Annotations
- The Jackson annotation
@JsonInclude
tells Jackson only to include properties under certain circumstances. For instance, that properties should only be included if they are non-null, non-empty, or have non-default values. @JsonInclude(JsonInclude.Include.NON_EMPTY) public class PersonInclude { public long personId = 0; public String name = null; }
- @JsonAnyGetter
- @JsonPropertyOrder
@JsonPropertyOrder({"name", "personId"}) public class PersonPropertyOrder {
- @JsonRawValue
- @JsonValue
- @JsonSerialize tells to use custom serializer for a property
@JsonIgnoreProperties({"firstName", "lastName"}) public class PersonIgnoreProperties {
@JsonIgnoreType public static class Address { public String streetName = null; public String houseNumber = null; public String zipCode = null; public String city = null; public String country = null; }
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY ) public class PersonAutoDetect { private long personId = 123; public String name = null;
is used for calling unrecognized properties
we must add the
@JsonCreator
annotation to the constructor. But that alone is not enough. We must also annotate the parameters of the constructor to tell Jackson which fields from the JSON object to pass to which constructor parameters.@JsonCreator public PersonImmutable( @JsonProperty("id") long id, @JsonProperty("name") String name ) { this.id = id; this.name = name; }
The Jackson annotation
@JacksonInject
is used to inject values into the parsed objects, instead of reading those values from the JSONThe Jackson annotation
@JsonDeserialize
is used to specify a custom de-serializer class for a given field in a Java object.same as setter except its getter
public class PersonSerializer {
public long personId = 0;
public String name = "John";
@JsonSerialize(using = OptimizedBooleanSerializer.class)
public boolean enabled = false;
}
Jackson Annotations
@JsonIdentityInfo
allows to serialize a POJO by id but only when it is encountered second time during serialization.@JsonIdentityReference
serialize the POJO by id the first time it is encountered.package example; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonIdentityReference; import com.fasterxml.jackson.annotation.ObjectIdGenerators; @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") @JsonIdentityReference(alwaysAsId = true) public class Student {}}
https://www.logicbig.com/tutorials/misc/jackson/json-identity-reference.html
____________________________________________________________________________
@JsonIgnore instead is a member-level or method-level annotation, which expects that the properties to be excluded are marked one by one.
@JsonIgnore@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "reroute") public SetgetEmployee() { return this.employee; }
@JsonIgnoreProperties is an annotation at the class level and it expects that the properties to be excluded would be explicitly indicated in the form of a list of strings.
@JsonPropertyOrder : Defines ordering to use while serializing
@JsonIgnoreProperties(ignoreUnknown = true) @JsonPropertyOrder({ "id", "doj", "employee"}) public class EmploymentDetails {@JsonIgnoreProperties(value = {"employee"}, ignoreUnknown = true) @JsonPropertyOrder({ "id", "doj", "employee"}) public class EmploymentDetails {https://www.logicbig.com/tutorials/misc/jackson.html______________________________________________________________________________@JsonIgnoreType
can be used to ignore the whole class,
https://www.logicbig.com/tutorials/misc/jackson/json-ignore-type.html
@JacksonInject
annotation is used to indicate that value of annotated property will be injected during deserialization.
This is useful if we want to add additional information which is not included in
https://www.logicbig.com/tutorials/misc/jackson/jackson-inject.html
the source JSON.
@JsonAlias({"department", "employeeDept" }) private String dept;_______________________________________________________________________________@JsonCreator
The @JsonCreator
annotation can be used on constructors or factory methods for mapping incoming JSON properties to the constructor/factory method arguments. This annotation is used only during deserialization and can be particularly useful for immutable objects
No comments:
Post a Comment