Init commit all code
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
## Jackson Annotations
|
||||
|
||||
This module contains articles about Jackson annotations.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Guide to @JsonFormat in Jackson](https://www.baeldung.com/jackson-jsonformat)
|
||||
- [More Jackson Annotations](https://www.baeldung.com/jackson-advanced-annotations)
|
||||
- [Jackson – Bidirectional Relationships](https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion)
|
||||
- [Jackson JSON Views](https://www.baeldung.com/jackson-json-view-annotation)
|
||||
@@ -0,0 +1,50 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>jackson-annotations</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.ossez</groupId>
|
||||
<artifactId>jackson-modules</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.module</groupId>
|
||||
<artifactId>jackson-module-jsonSchema</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>json-path</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>jackson-annotations</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<rest-assured.version>3.1.1</rest-assured.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
|
||||
public class CustomListDeserializer extends StdDeserializer<List<ItemWithSerializer>> {
|
||||
|
||||
private static final long serialVersionUID = 1095767961632979804L;
|
||||
|
||||
public CustomListDeserializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomListDeserializer(final Class<?> vc) {
|
||||
super(vc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemWithSerializer> deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException {
|
||||
return new ArrayList<ItemWithSerializer>();
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class CustomListSerializer extends StdSerializer<List<ItemWithSerializer>> {
|
||||
|
||||
private static final long serialVersionUID = 3698763098000900856L;
|
||||
|
||||
public CustomListSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomListSerializer(final Class<List<ItemWithSerializer>> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final List<ItemWithSerializer> items, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
final List<Integer> ids = new ArrayList<Integer>();
|
||||
for (final ItemWithSerializer item : items) {
|
||||
ids.add(item.id);
|
||||
}
|
||||
generator.writeObject(ids);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
public class Item {
|
||||
public int id;
|
||||
public String itemName;
|
||||
public User owner;
|
||||
|
||||
public Item() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Item(final int id, final String itemName, final User owner) {
|
||||
this.id = id;
|
||||
this.itemName = itemName;
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
|
||||
|
||||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
|
||||
public class ItemWithIdentity {
|
||||
public int id;
|
||||
public String itemName;
|
||||
public UserWithIdentity owner;
|
||||
|
||||
public ItemWithIdentity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ItemWithIdentity(final int id, final String itemName, final UserWithIdentity owner) {
|
||||
this.id = id;
|
||||
this.itemName = itemName;
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
public class ItemWithIgnore {
|
||||
public int id;
|
||||
public String itemName;
|
||||
public UserWithIgnore owner;
|
||||
|
||||
public ItemWithIgnore() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ItemWithIgnore(final int id, final String itemName, final UserWithIgnore owner) {
|
||||
this.id = id;
|
||||
this.itemName = itemName;
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||
|
||||
public class ItemWithRef {
|
||||
public int id;
|
||||
public String itemName;
|
||||
|
||||
@JsonBackReference
|
||||
public UserWithRef owner;
|
||||
|
||||
public ItemWithRef() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ItemWithRef(final int id, final String itemName, final UserWithRef owner) {
|
||||
this.id = id;
|
||||
this.itemName = itemName;
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
public class ItemWithSerializer {
|
||||
public int id;
|
||||
public String itemName;
|
||||
public UserWithSerializer owner;
|
||||
|
||||
public ItemWithSerializer() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ItemWithSerializer(final int id, final String itemName, final UserWithSerializer owner) {
|
||||
this.id = id;
|
||||
this.itemName = itemName;
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
import com.ossez.jackson.bidirection.jsonview.Views;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
|
||||
public class ItemWithView {
|
||||
@JsonView(Views.Public.class)
|
||||
public int id;
|
||||
|
||||
@JsonView(Views.Public.class)
|
||||
public String itemName;
|
||||
|
||||
@JsonView(Views.Public.class)
|
||||
public UserWithView owner;
|
||||
|
||||
public ItemWithView() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ItemWithView(final int id, final String itemName, final UserWithView owner) {
|
||||
this.id = id;
|
||||
this.itemName = itemName;
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class User {
|
||||
public int id;
|
||||
public String name;
|
||||
public List<Item> userItems;
|
||||
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
userItems = new ArrayList<Item>();
|
||||
}
|
||||
|
||||
public void addItem(final Item item) {
|
||||
userItems.add(item);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
|
||||
|
||||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
|
||||
public class UserWithIdentity {
|
||||
public int id;
|
||||
public String name;
|
||||
public List<ItemWithIdentity> userItems;
|
||||
|
||||
public UserWithIdentity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserWithIdentity(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
userItems = new ArrayList<ItemWithIdentity>();
|
||||
}
|
||||
|
||||
public void addItem(final ItemWithIdentity item) {
|
||||
userItems.add(item);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
public class UserWithIgnore {
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
@JsonIgnore
|
||||
public List<ItemWithIgnore> userItems;
|
||||
|
||||
public UserWithIgnore() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserWithIgnore(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
userItems = new ArrayList<ItemWithIgnore>();
|
||||
}
|
||||
|
||||
public void addItem(final ItemWithIgnore item) {
|
||||
userItems.add(item);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||
|
||||
public class UserWithRef {
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
@JsonManagedReference
|
||||
public List<ItemWithRef> userItems;
|
||||
|
||||
public UserWithRef() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserWithRef(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
userItems = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addItem(final ItemWithRef item) {
|
||||
userItems.add(item);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
public class UserWithSerializer {
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
@JsonSerialize(using = CustomListSerializer.class)
|
||||
@JsonDeserialize(using = CustomListDeserializer.class)
|
||||
public List<ItemWithSerializer> userItems;
|
||||
|
||||
public UserWithSerializer() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserWithSerializer(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
userItems = new ArrayList<ItemWithSerializer>();
|
||||
}
|
||||
|
||||
public void addItem(final ItemWithSerializer item) {
|
||||
userItems.add(item);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.ossez.jackson.bidirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.ossez.jackson.bidirection.jsonview.Views;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
|
||||
public class UserWithView {
|
||||
@JsonView(Views.Public.class)
|
||||
public int id;
|
||||
|
||||
@JsonView(Views.Public.class)
|
||||
public String name;
|
||||
|
||||
@JsonView(Views.Internal.class)
|
||||
public List<ItemWithView> userItems;
|
||||
|
||||
public UserWithView() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserWithView(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
userItems = new ArrayList<ItemWithView>();
|
||||
}
|
||||
|
||||
public void addItem(final ItemWithView item) {
|
||||
userItems.add(item);
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.ossez.jackson.bidirection.jsonview;
|
||||
|
||||
public class Views {
|
||||
public static class Public {
|
||||
}
|
||||
|
||||
public static class Internal extends Public {
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.ossez.jackson.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Person(String firstName, String lastName) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.ossez.jackson.format;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.ossez.jackson.domain.Person;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
/**
|
||||
* @author Jay Sridhar
|
||||
* @version 1.0
|
||||
*/
|
||||
public class User extends Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
|
||||
private Date createdDate;
|
||||
|
||||
public User(String firstName, String lastName) {
|
||||
super(firstName, lastName);
|
||||
this.createdDate = new Date();
|
||||
}
|
||||
|
||||
public Date getCreatedDate() {
|
||||
return createdDate;
|
||||
}
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ", locale = "en_GB")
|
||||
public Date getCurrentDate() {
|
||||
return new Date();
|
||||
}
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
|
||||
public Date getDateNum() {
|
||||
return new Date();
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.ossez.jackson.jsonview;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
|
||||
public class Item {
|
||||
@JsonView(Views.Public.class)
|
||||
public int id;
|
||||
|
||||
@JsonView(Views.Public.class)
|
||||
public String itemName;
|
||||
|
||||
@JsonView(Views.Internal.class)
|
||||
public String ownerName;
|
||||
|
||||
public Item() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Item(final int id, final String itemName, final String ownerName) {
|
||||
this.id = id;
|
||||
this.itemName = itemName;
|
||||
this.ownerName = ownerName;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getItemName() {
|
||||
return itemName;
|
||||
}
|
||||
|
||||
public String getOwnerName() {
|
||||
return ownerName;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.ossez.jackson.jsonview;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.databind.BeanDescription;
|
||||
import com.fasterxml.jackson.databind.SerializationConfig;
|
||||
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
|
||||
|
||||
public class MyBeanSerializerModifier extends BeanSerializerModifier {
|
||||
|
||||
@Override
|
||||
public List<BeanPropertyWriter> changeProperties(final SerializationConfig config, final BeanDescription beanDesc, final List<BeanPropertyWriter> beanProperties) {
|
||||
for (int i = 0; i < beanProperties.size(); i++) {
|
||||
final BeanPropertyWriter beanPropertyWriter = beanProperties.get(i);
|
||||
if (beanPropertyWriter.getName() == "name") {
|
||||
beanProperties.set(i, new UpperCasingWriter(beanPropertyWriter));
|
||||
}
|
||||
}
|
||||
return beanProperties;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.ossez.jackson.jsonview;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
|
||||
|
||||
public class UpperCasingWriter extends BeanPropertyWriter {
|
||||
final BeanPropertyWriter _writer;
|
||||
|
||||
public UpperCasingWriter(final BeanPropertyWriter w) {
|
||||
super(w);
|
||||
_writer = w;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serializeAsField(final Object bean, final JsonGenerator gen, final SerializerProvider prov) throws Exception {
|
||||
String value = ((User) bean).name;
|
||||
value = (value == null) ? "" : value.toUpperCase();
|
||||
gen.writeStringField("name", value);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.ossez.jackson.jsonview;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
|
||||
public class User {
|
||||
public int id;
|
||||
|
||||
@JsonView(Views.Public.class)
|
||||
public String name;
|
||||
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.ossez.jackson.jsonview;
|
||||
|
||||
public class Views {
|
||||
public static class Public {
|
||||
}
|
||||
|
||||
public static class Internal extends Public {
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
package com.ossez.jackson.advancedannotations;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.ossez.jackson.advancedannotations.AppendBeans.BeanWithAppend;
|
||||
import com.ossez.jackson.advancedannotations.AppendBeans.BeanWithoutAppend;
|
||||
import com.ossez.jackson.advancedannotations.IdentityReferenceBeans.BeanWithIdentityReference;
|
||||
import com.ossez.jackson.advancedannotations.IdentityReferenceBeans.BeanWithoutIdentityReference;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
|
||||
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
|
||||
|
||||
public class AdvancedAnnotationsUnitTest {
|
||||
@Test
|
||||
public void whenNotUsingJsonIdentityReferenceAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
BeanWithoutIdentityReference bean = new BeanWithoutIdentityReference(1, "Bean Without Identity Reference Annotation");
|
||||
String jsonString = mapper.writeValueAsString(bean);
|
||||
|
||||
assertThat(jsonString, containsString("Bean Without Identity Reference Annotation"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonIdentityReferenceAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
BeanWithIdentityReference bean = new BeanWithIdentityReference(1, "Bean With Identity Reference Annotation");
|
||||
String jsonString = mapper.writeValueAsString(bean);
|
||||
|
||||
assertEquals("1", jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotUsingJsonAppendAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
BeanWithoutAppend bean = new BeanWithoutAppend(2, "Bean Without Append Annotation");
|
||||
ObjectWriter writer = mapper.writerFor(BeanWithoutAppend.class)
|
||||
.withAttribute("version", "1.0");
|
||||
String jsonString = writer.writeValueAsString(bean);
|
||||
|
||||
assertThat(jsonString, not(containsString("version")));
|
||||
assertThat(jsonString, not(containsString("1.0")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonAppendAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
BeanWithAppend bean = new BeanWithAppend(2, "Bean With Append Annotation");
|
||||
ObjectWriter writer = mapper.writerFor(BeanWithAppend.class)
|
||||
.withAttribute("version", "1.0");
|
||||
String jsonString = writer.writeValueAsString(bean);
|
||||
|
||||
assertThat(jsonString, containsString("version"));
|
||||
assertThat(jsonString, containsString("1.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonNamingAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
NamingBean bean = new NamingBean(3, "Naming Bean");
|
||||
String jsonString = mapper.writeValueAsString(bean);
|
||||
|
||||
assertThat(jsonString, containsString("bean_name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonPropertyDescriptionAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
SchemaFactoryWrapper wrapper = new SchemaFactoryWrapper();
|
||||
mapper.acceptJsonFormatVisitor(PropertyDescriptionBean.class, wrapper);
|
||||
JsonSchema jsonSchema = wrapper.finalSchema();
|
||||
String jsonString = mapper.writeValueAsString(jsonSchema);
|
||||
System.out.println(jsonString);
|
||||
assertThat(jsonString, containsString("This is a description of the name property"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonPOJOBuilderAnnotation_thenCorrect() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String jsonString = "{\"id\":5,\"name\":\"POJO Builder Bean\"}";
|
||||
POJOBuilderBean bean = mapper.readValue(jsonString, POJOBuilderBean.class);
|
||||
|
||||
assertEquals(5, bean.getIdentity());
|
||||
assertEquals("POJO Builder Bean", bean.getBeanName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonTypeIdAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.enableDefaultTyping(DefaultTyping.NON_FINAL);
|
||||
TypeIdBean bean = new TypeIdBean(6, "Type Id Bean");
|
||||
String jsonString = mapper.writeValueAsString(bean);
|
||||
|
||||
assertThat(jsonString, containsString("Type Id Bean"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJsonTypeIdResolverAnnotation_thenCorrect() throws IOException {
|
||||
TypeIdResolverStructure.FirstBean bean1 = new TypeIdResolverStructure.FirstBean(1, "Bean 1");
|
||||
TypeIdResolverStructure.LastBean bean2 = new TypeIdResolverStructure.LastBean(2, "Bean 2");
|
||||
|
||||
List<TypeIdResolverStructure.AbstractBean> beans = new ArrayList<>();
|
||||
beans.add(bean1);
|
||||
beans.add(bean2);
|
||||
|
||||
TypeIdResolverStructure.BeanContainer serializedContainer = new TypeIdResolverStructure.BeanContainer();
|
||||
serializedContainer.setBeans(beans);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String jsonString = mapper.writeValueAsString(serializedContainer);
|
||||
assertThat(jsonString, containsString("bean1"));
|
||||
assertThat(jsonString, containsString("bean2"));
|
||||
|
||||
TypeIdResolverStructure.BeanContainer deserializedContainer = mapper.readValue(jsonString, TypeIdResolverStructure.BeanContainer.class);
|
||||
List<TypeIdResolverStructure.AbstractBean> beanList = deserializedContainer.getBeans();
|
||||
assertThat(beanList.get(0), instanceOf(TypeIdResolverStructure.FirstBean.class));
|
||||
assertThat(beanList.get(1), instanceOf(TypeIdResolverStructure.LastBean.class));
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.ossez.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonAppend;
|
||||
|
||||
public class AppendBeans {
|
||||
public static class BeanWithoutAppend {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public BeanWithoutAppend(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonAppend(attrs = { @JsonAppend.Attr(value = "version") })
|
||||
public static class BeanWithAppend {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public BeanWithAppend(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.ossez.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityReference;
|
||||
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
|
||||
|
||||
public class IdentityReferenceBeans {
|
||||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
|
||||
public static class BeanWithoutIdentityReference {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public BeanWithoutIdentityReference(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
|
||||
@JsonIdentityReference(alwaysAsId = true)
|
||||
public static class BeanWithIdentityReference {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public BeanWithIdentityReference(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.ossez.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
|
||||
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
|
||||
public class NamingBean {
|
||||
private int id;
|
||||
private String beanName;
|
||||
|
||||
public NamingBean(int id, String beanName) {
|
||||
this.id = id;
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.ossez.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
|
||||
|
||||
@JsonDeserialize(builder = POJOBuilderBean.BeanBuilder.class)
|
||||
public class POJOBuilderBean {
|
||||
private int identity;
|
||||
private String beanName;
|
||||
|
||||
@JsonPOJOBuilder(buildMethodName = "createBean", withPrefix = "construct")
|
||||
public static class BeanBuilder {
|
||||
private int idValue;
|
||||
private String nameValue;
|
||||
|
||||
public BeanBuilder constructId(int id) {
|
||||
idValue = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BeanBuilder constructName(String name) {
|
||||
nameValue = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public POJOBuilderBean createBean() {
|
||||
return new POJOBuilderBean(idValue, nameValue);
|
||||
}
|
||||
}
|
||||
|
||||
public POJOBuilderBean(int identity, String beanName) {
|
||||
this.identity = identity;
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
public int getIdentity() {
|
||||
return identity;
|
||||
}
|
||||
|
||||
public void setIdentity(int identity) {
|
||||
this.identity = identity;
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.ossez.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
|
||||
|
||||
public class PropertyDescriptionBean {
|
||||
private int id;
|
||||
@JsonPropertyDescription("This is a description of the name property")
|
||||
private String name;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user