Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
|
||||
public class BeanWithCreator {
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
@JsonCreator
|
||||
public BeanWithCreator(@JsonProperty("id") final int id, @JsonProperty("theName") final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.Date;
|
||||
|
||||
import org.baeldung.jackson.annotation.BeanWithCustomAnnotation.CustomAnnotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
|
||||
@CustomAnnotation
|
||||
public class BeanWithCustomAnnotation {
|
||||
public int id;
|
||||
public String name;
|
||||
public Date dateCreated;
|
||||
|
||||
public BeanWithCustomAnnotation() {
|
||||
|
||||
}
|
||||
|
||||
public BeanWithCustomAnnotation(final int id, final String name, final Date dateCreated) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.dateCreated = dateCreated;
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@JacksonAnnotationsInside
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
@JsonPropertyOrder({ "name", "id", "dateCreated" })
|
||||
public @interface CustomAnnotation {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
|
||||
@JsonFilter("myFilter")
|
||||
public class BeanWithFilter {
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
public BeanWithFilter() {
|
||||
|
||||
}
|
||||
|
||||
public BeanWithFilter(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonGetter;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonSetter;
|
||||
|
||||
public class BeanWithGetter {
|
||||
public int id;
|
||||
private String name;
|
||||
|
||||
public BeanWithGetter() {
|
||||
|
||||
}
|
||||
|
||||
public BeanWithGetter(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@JsonProperty("name")
|
||||
@JsonSetter("name")
|
||||
public void setTheName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@JsonProperty("name")
|
||||
@JsonGetter("name")
|
||||
public String getTheName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties({ "id" })
|
||||
public class BeanWithIgnore {
|
||||
@JsonIgnore
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
public BeanWithIgnore() {
|
||||
|
||||
}
|
||||
|
||||
public BeanWithIgnore(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonInject;
|
||||
|
||||
public class BeanWithInject {
|
||||
@JacksonInject
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
public BeanWithInject() {
|
||||
|
||||
}
|
||||
|
||||
public BeanWithInject(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAnyGetter;
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
|
||||
public class ExtendableBean {
|
||||
public String name;
|
||||
private Map<String, String> properties;
|
||||
|
||||
public ExtendableBean() {
|
||||
properties = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
public ExtendableBean(final String name) {
|
||||
this.name = name;
|
||||
properties = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
@JsonAnySetter
|
||||
public void add(final String key, final String value) {
|
||||
properties.put(key, value);
|
||||
}
|
||||
|
||||
@JsonAnyGetter
|
||||
public Map<String, String> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
@JsonPropertyOrder({ "name", "id" })
|
||||
public class MyBean {
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
public MyBean() {
|
||||
|
||||
}
|
||||
|
||||
public MyBean(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||
|
||||
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
|
||||
public class PrivateBean {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public PrivateBean() {
|
||||
|
||||
}
|
||||
|
||||
public PrivateBean(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonRawValue;
|
||||
|
||||
public class RawBean {
|
||||
public String name;
|
||||
|
||||
@JsonRawValue
|
||||
public String json;
|
||||
|
||||
public RawBean() {
|
||||
|
||||
}
|
||||
|
||||
public RawBean(final String name, final String json) {
|
||||
this.name = name;
|
||||
this.json = json;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonUnwrapped;
|
||||
|
||||
public class UnwrappedUser {
|
||||
public int id;
|
||||
|
||||
@JsonUnwrapped
|
||||
public Name name;
|
||||
|
||||
public UnwrappedUser() {
|
||||
|
||||
}
|
||||
|
||||
public UnwrappedUser(final int id, final Name name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static class Name {
|
||||
public String firstName;
|
||||
public String lastName;
|
||||
|
||||
public Name() {
|
||||
|
||||
}
|
||||
|
||||
public Name(final String firstName, final String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreType;
|
||||
|
||||
public class UserWithIgnoreType {
|
||||
public int id;
|
||||
|
||||
public Name name;
|
||||
|
||||
public UserWithIgnoreType() {
|
||||
|
||||
}
|
||||
|
||||
public UserWithIgnoreType(final int id, final Name name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@JsonIgnoreType
|
||||
public static class Name {
|
||||
public String firstName;
|
||||
public String lastName;
|
||||
|
||||
public Name() {
|
||||
|
||||
}
|
||||
|
||||
public Name(final String firstName, final String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package org.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
|
||||
public class Zoo {
|
||||
public Animal animal;
|
||||
|
||||
public Zoo() {
|
||||
|
||||
}
|
||||
|
||||
public Zoo(final Animal animal) {
|
||||
this.animal = animal;
|
||||
}
|
||||
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "type")
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = Dog.class, name = "dog"),
|
||||
@JsonSubTypes.Type(value = Cat.class, name = "cat")
|
||||
})
|
||||
public static class Animal {
|
||||
public String name;
|
||||
|
||||
public Animal() {
|
||||
}
|
||||
|
||||
public Animal(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonTypeName("dog")
|
||||
public static class Dog extends Animal {
|
||||
public double barkVolume;
|
||||
|
||||
public Dog() {
|
||||
}
|
||||
|
||||
public Dog(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonTypeName("cat")
|
||||
public static class Cat extends Animal {
|
||||
boolean likesCream;
|
||||
public int lives;
|
||||
|
||||
public Cat() {
|
||||
}
|
||||
|
||||
public Cat(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user