add new package
This commit is contained in:
parent
99259d231b
commit
917fb8e246
Binary file not shown.
|
@ -1,5 +0,0 @@
|
|||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
|
@ -1,9 +0,0 @@
|
|||
## 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)
|
|
@ -1,50 +0,0 @@
|
|||
<?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>
|
|
@ -1,29 +0,0 @@
|
|||
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>();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
package com.ossez.jackson.bidirection.jsonview;
|
||||
|
||||
public class Views {
|
||||
public static class Public {
|
||||
}
|
||||
|
||||
public static class Internal extends Public {
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
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();
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
package com.ossez.jackson.jsonview;
|
||||
|
||||
public class Views {
|
||||
public static class Public {
|
||||
}
|
||||
|
||||
public static class Internal extends Public {
|
||||
}
|
||||
}
|
|
@ -1,133 +0,0 @@
|
|||
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));
|
||||
}
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
package com.ossez.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeId;
|
||||
|
||||
public class TypeIdBean {
|
||||
private int id;
|
||||
@JsonTypeId
|
||||
private String name;
|
||||
|
||||
public TypeIdBean(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;
|
||||
}
|
||||
}
|
|
@ -1,130 +0,0 @@
|
|||
package com.ossez.jackson.advancedannotations;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
|
||||
import com.fasterxml.jackson.databind.DatabindContext;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
|
||||
import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
|
||||
|
||||
public class TypeIdResolverStructure {
|
||||
public static class BeanContainer {
|
||||
private List<AbstractBean> beans;
|
||||
|
||||
public List<AbstractBean> getBeans() {
|
||||
return beans;
|
||||
}
|
||||
|
||||
public void setBeans(List<AbstractBean> beans) {
|
||||
this.beans = beans;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type")
|
||||
@JsonTypeIdResolver(BeanIdResolver.class)
|
||||
public static class AbstractBean {
|
||||
private int id;
|
||||
|
||||
protected AbstractBean() {
|
||||
}
|
||||
|
||||
protected AbstractBean(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
public static class FirstBean extends AbstractBean {
|
||||
String firstName;
|
||||
|
||||
public FirstBean() {
|
||||
}
|
||||
|
||||
public FirstBean(int id, String name) {
|
||||
super(id);
|
||||
setFirstName(name);
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String name) {
|
||||
firstName = name;
|
||||
}
|
||||
}
|
||||
|
||||
public static class LastBean extends AbstractBean {
|
||||
String lastName;
|
||||
|
||||
public LastBean() {
|
||||
}
|
||||
|
||||
public LastBean(int id, String name) {
|
||||
super(id);
|
||||
setLastName(name);
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String name) {
|
||||
lastName = name;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BeanIdResolver extends TypeIdResolverBase {
|
||||
private JavaType superType;
|
||||
|
||||
@Override
|
||||
public void init(JavaType baseType) {
|
||||
superType = baseType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Id getMechanism() {
|
||||
return Id.NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String idFromValue(Object obj) {
|
||||
return idFromValueAndType(obj, obj.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String idFromValueAndType(Object obj, Class<?> subType) {
|
||||
String typeId = null;
|
||||
switch (subType.getSimpleName()) {
|
||||
case "FirstBean":
|
||||
typeId = "bean1";
|
||||
break;
|
||||
case "LastBean":
|
||||
typeId = "bean2";
|
||||
}
|
||||
return typeId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaType typeFromId(DatabindContext context, String id) {
|
||||
Class<?> subType = null;
|
||||
switch (id) {
|
||||
case "bean1":
|
||||
subType = FirstBean.class;
|
||||
break;
|
||||
case "bean2":
|
||||
subType = LastBean.class;
|
||||
}
|
||||
return context.constructSpecializedType(superType, subType);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,152 +0,0 @@
|
|||
package com.ossez.jackson.bidirection;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.ossez.jackson.bidirection.jsonview.Views;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JacksonBidirectionRelationUnitTest {
|
||||
|
||||
@Test (expected = JsonMappingException.class)
|
||||
public void givenBidirectionRelation_whenSerializing_thenException() throws JsonProcessingException {
|
||||
final User user = new User(1, "John");
|
||||
final Item item = new Item(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
new ObjectMapper().writeValueAsString(item);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotationWithSerialization_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithRef user = new UserWithRef(1, "John");
|
||||
final ItemWithRef item = new ItemWithRef(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String itemJson = new ObjectMapper().writeValueAsString(item);
|
||||
final String userJson = new ObjectMapper().writeValueAsString(user);
|
||||
|
||||
assertThat(itemJson, containsString("book"));
|
||||
assertThat(itemJson, not(containsString("John")));
|
||||
|
||||
assertThat(userJson, containsString("John"));
|
||||
assertThat(userJson, containsString("userItems"));
|
||||
assertThat(userJson, containsString("book"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotationWithDeserialization_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithRef user = new UserWithRef(1, "John");
|
||||
final ItemWithRef item = new ItemWithRef(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String itemJson = new ObjectMapper().writeValueAsString(item);
|
||||
final String userJson = new ObjectMapper().writeValueAsString(user);
|
||||
|
||||
final ItemWithRef itemRead = new ObjectMapper().readValue(itemJson, ItemWithRef.class);
|
||||
final UserWithRef userRead = new ObjectMapper().readValue(userJson, UserWithRef.class);
|
||||
|
||||
assertThat(itemRead.itemName, is("book"));
|
||||
assertThat(itemRead.owner, nullValue());
|
||||
|
||||
assertThat(userRead.name, is("John"));
|
||||
assertThat(userRead.userItems.get(0).itemName, is("book"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingJsonIdentityInfo_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithIdentity user = new UserWithIdentity(1, "John");
|
||||
final ItemWithIdentity item = new ItemWithIdentity(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, containsString("userItems"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingJsonIgnore_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithIgnore user = new UserWithIgnore(1, "John");
|
||||
final ItemWithIgnore item = new ItemWithIgnore(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, not(containsString("userItems")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingCustomSerializer_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithSerializer user = new UserWithSerializer(1, "John");
|
||||
final ItemWithSerializer item = new ItemWithSerializer(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, containsString("userItems"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenDeserializingUsingIdentity_thenCorrect() throws JsonProcessingException, IOException {
|
||||
final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}";
|
||||
|
||||
final ItemWithIdentity item = new ObjectMapper().readerFor(ItemWithIdentity.class)
|
||||
.readValue(json);
|
||||
|
||||
assertEquals(2, item.id);
|
||||
assertEquals("book", item.itemName);
|
||||
assertEquals("John", item.owner.name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException {
|
||||
final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}";
|
||||
|
||||
final ItemWithSerializer item = new ObjectMapper().readerFor(ItemWithSerializer.class)
|
||||
.readValue(json);
|
||||
assertEquals(2, item.id);
|
||||
assertEquals("book", item.itemName);
|
||||
assertEquals("John", item.owner.name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingPublicJsonView_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithView user = new UserWithView(1, "John");
|
||||
final ItemWithView item = new ItemWithView(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writerWithView(Views.Public.class)
|
||||
.writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, not(containsString("userItems")));
|
||||
}
|
||||
|
||||
@Test(expected = JsonMappingException.class)
|
||||
public void givenBidirectionRelation_whenUsingInternalJsonView_thenException() throws JsonProcessingException {
|
||||
final UserWithView user = new UserWithView(1, "John");
|
||||
final ItemWithView item = new ItemWithView(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
new ObjectMapper().writerWithView(Views.Internal.class)
|
||||
.writeValueAsString(item);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
package com.ossez.jackson.format;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.data.Percentage.withPercentage;
|
||||
|
||||
/**
|
||||
* @author Jay Sridhar
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonFormatUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializedDateFormat_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
User user = new User("Jay", "Sridhar");
|
||||
|
||||
String result = new ObjectMapper().writeValueAsString(user);
|
||||
|
||||
// Expected to match: "2016-12-19@09:34:42.628+0000"
|
||||
assertThat(from(result).getString("createdDate")).matches("\\d{4}\\-\\d{2}\\-\\d{2}@\\d{2}:\\d{2}:\\d{2}\\.\\d{3}\\+\\d{4}");
|
||||
|
||||
// Expected to be close to current time
|
||||
long now = new Date().getTime();
|
||||
assertThat(from(result).getLong("dateNum")).isCloseTo(now, withPercentage(10.0));
|
||||
|
||||
}
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
package com.ossez.jackson.jsonview;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
|
||||
import com.fasterxml.jackson.databind.ser.SerializerFactory;
|
||||
|
||||
public class JacksonJsonViewUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUseJsonViewToSerialize_thenCorrect() throws JsonProcessingException {
|
||||
final User user = new User(1, "John");
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
|
||||
|
||||
final String result = mapper.writerWithView(Views.Public.class)
|
||||
.writeValueAsString(user);
|
||||
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, not(containsString("1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsePublicView_thenOnlyPublicSerialized() throws JsonProcessingException {
|
||||
final Item item = new Item(2, "book", "John");
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String result = mapper.writerWithView(Views.Public.class)
|
||||
.writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("2"));
|
||||
|
||||
assertThat(result, not(containsString("John")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseInternalView_thenAllSerialized() throws JsonProcessingException {
|
||||
final Item item = new Item(2, "book", "John");
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String result = mapper.writerWithView(Views.Internal.class)
|
||||
.writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("2"));
|
||||
|
||||
assertThat(result, containsString("John"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseJsonViewToDeserialize_thenCorrect() throws IOException {
|
||||
final String json = "{\"id\":1,\"name\":\"John\"}";
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
final User user = mapper.readerWithView(Views.Public.class)
|
||||
.forType(User.class)
|
||||
.readValue(json);
|
||||
assertEquals(1, user.getId());
|
||||
assertEquals("John", user.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseCustomJsonViewToSerialize_thenCorrect() throws JsonProcessingException {
|
||||
final User user = new User(1, "John");
|
||||
final SerializerFactory serializerFactory = BeanSerializerFactory.instance.withSerializerModifier(new MyBeanSerializerModifier());
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setSerializerFactory(serializerFactory);
|
||||
|
||||
final String result = mapper.writerWithView(Views.Public.class)
|
||||
.writeValueAsString(user);
|
||||
assertThat(result, containsString("JOHN"));
|
||||
assertThat(result, containsString("1"));
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
## Jackson 转换
|
||||
|
||||
本模块中包含有关 Jackson 转换(Jackson conversions)有关的文章。
|
||||
|
||||
### 相关文章:
|
||||
- [Mapping a Dynamic JSON Object with Jackson](https://www.baeldung.com/jackson-mapping-dynamic-object)
|
||||
- [Mapping Multiple JSON Fields to a Single Java Field](https://www.baeldung.com/json-multiple-fields-single-java-field)
|
||||
- [Convert XML to JSON Using Jackson](https://www.baeldung.com/jackson-convert-xml-json)
|
||||
- [Converting JSON to CSV in Java](https://www.baeldung.com/java-converting-json-to-csv)
|
||||
- [How to Process YAML with Jackson](https://www.baeldung.com/jackson-yaml)
|
||||
- [Jackson Streaming API](https://www.baeldung.com/jackson-streaming-api)
|
||||
- [Jackson: java.util.LinkedHashMap cannot be cast to X](https://www.baeldung.com/jackson-linkedhashmap-cannot-be-cast)
|
||||
- More articles: [[<-- prev]](../jackson-conversions)
|
|
@ -1,57 +0,0 @@
|
|||
<?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-conversions-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>jackson-conversions-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.ossez</groupId>
|
||||
<artifactId>jackson-modules</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- YAML -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-yaml</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<!-- Allow use of LocalDate -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>${jackson-datatype.version}</version>
|
||||
</dependency>
|
||||
<!-- CSV -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-csv</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>jackson-conversions-2</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<jackson-datatype.version>2.9.8</jackson-datatype.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,57 +0,0 @@
|
|||
package com.ossez.jackson.csv;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.MappingIterator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
|
||||
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
|
||||
import com.fasterxml.jackson.dataformat.csv.CsvSchema.Builder;
|
||||
|
||||
public class JsonCsvConverter {
|
||||
|
||||
public static void JsonToCsv(File jsonFile, File csvFile) throws IOException {
|
||||
JsonNode jsonTree = new ObjectMapper().readTree(jsonFile);
|
||||
|
||||
Builder csvSchemaBuilder = CsvSchema.builder();
|
||||
JsonNode firstObject = jsonTree.elements().next();
|
||||
firstObject.fieldNames().forEachRemaining(fieldName -> {csvSchemaBuilder.addColumn(fieldName);} );
|
||||
CsvSchema csvSchema = csvSchemaBuilder
|
||||
.build()
|
||||
.withHeader();
|
||||
|
||||
CsvMapper csvMapper = new CsvMapper();
|
||||
csvMapper.writerFor(JsonNode.class)
|
||||
.with(csvSchema)
|
||||
.writeValue(csvFile, jsonTree);
|
||||
}
|
||||
|
||||
public static void csvToJson(File csvFile, File jsonFile) throws IOException {
|
||||
CsvSchema orderLineSchema = CsvSchema.emptySchema().withHeader();
|
||||
CsvMapper csvMapper = new CsvMapper();
|
||||
MappingIterator<OrderLine> orderLines = csvMapper.readerFor(OrderLine.class)
|
||||
.with(orderLineSchema)
|
||||
.readValues(csvFile);
|
||||
|
||||
new ObjectMapper()
|
||||
.configure(SerializationFeature.INDENT_OUTPUT, true)
|
||||
.writeValue(jsonFile, orderLines.readAll());
|
||||
}
|
||||
|
||||
public static void JsonToFormattedCsv(File jsonFile, File csvFile) throws IOException {
|
||||
CsvMapper csvMapper = new CsvMapper();
|
||||
CsvSchema csvSchema = csvMapper
|
||||
.schemaFor(OrderLineForCsv.class)
|
||||
.withHeader();
|
||||
csvMapper.addMixIn(OrderLine.class, OrderLineForCsv.class);
|
||||
|
||||
OrderLine[] orderLines = new ObjectMapper()
|
||||
.readValue(jsonFile, OrderLine[].class);
|
||||
csvMapper.writerFor(OrderLine[].class)
|
||||
.with(csvSchema)
|
||||
.writeValue(csvFile, orderLines);
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
package com.ossez.jackson.csv;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class OrderLine {
|
||||
private String item;
|
||||
private int quantity;
|
||||
private BigDecimal unitPrice;
|
||||
|
||||
public OrderLine() {
|
||||
|
||||
}
|
||||
|
||||
public OrderLine(String item, int quantity, BigDecimal unitPrice) {
|
||||
super();
|
||||
this.item = item;
|
||||
this.quantity = quantity;
|
||||
this.unitPrice = unitPrice;
|
||||
}
|
||||
|
||||
public String getItem() {
|
||||
return item;
|
||||
}
|
||||
|
||||
public void setItem(String item) {
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public BigDecimal getUnitPrice() {
|
||||
return unitPrice;
|
||||
}
|
||||
|
||||
public void setUnitPrice(BigDecimal unitPrice) {
|
||||
this.unitPrice = unitPrice;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OrderLine [item=" + item + ", quantity=" + quantity + ", unitPrice=" + unitPrice + "]";
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package com.ossez.jackson.csv;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
|
||||
@JsonPropertyOrder({
|
||||
"count",
|
||||
"name"
|
||||
})
|
||||
public abstract class OrderLineForCsv {
|
||||
|
||||
@JsonProperty("name")
|
||||
private String item;
|
||||
|
||||
@JsonProperty("count")
|
||||
private int quantity;
|
||||
|
||||
@JsonIgnore
|
||||
private BigDecimal unitPrice;
|
||||
|
||||
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package com.ossez.jackson.dynamicobject;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
|
||||
public class Product {
|
||||
|
||||
private String name;
|
||||
private String category;
|
||||
private Map<String, Object> details = new LinkedHashMap<>();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public Map<String, Object> getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
@JsonAnySetter
|
||||
public void setDetail(String key, Object value) {
|
||||
details.put(key, value);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package com.ossez.jackson.dynamicobject;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
public class ProductJsonNode {
|
||||
|
||||
private String name;
|
||||
private String category;
|
||||
private JsonNode details;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public JsonNode getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
public void setDetails(JsonNode details) {
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package com.ossez.jackson.dynamicobject;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class ProductMap {
|
||||
|
||||
private String name;
|
||||
private String category;
|
||||
private Map<String, Object> details;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public Map<String, Object> getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
public void setDetails(Map<String, Object> details) {
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
package com.ossez.jackson.multiplefields;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAlias;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Weather {
|
||||
|
||||
@JsonProperty("location")
|
||||
@JsonAlias("place")
|
||||
private String location;
|
||||
|
||||
@JsonProperty("temp")
|
||||
@JsonAlias("temperature")
|
||||
private int temp;
|
||||
|
||||
@JsonProperty("outlook")
|
||||
@JsonAlias("weather")
|
||||
private String outlook;
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public int getTemp() {
|
||||
return temp;
|
||||
}
|
||||
|
||||
public void setTemp(int temp) {
|
||||
this.temp = temp;
|
||||
}
|
||||
|
||||
public String getOutlook() {
|
||||
return outlook;
|
||||
}
|
||||
|
||||
public void setOutlook(String outlook) {
|
||||
this.outlook = outlook;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
package com.ossez.jackson.tocollection;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Book {
|
||||
private Integer bookId;
|
||||
private String title;
|
||||
private String author;
|
||||
|
||||
public Book() {}
|
||||
|
||||
public Book(Integer bookId, String title, String author) {
|
||||
this.bookId = bookId;
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof Book)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Book book = (Book) o;
|
||||
|
||||
if (!Objects.equals(bookId, book.bookId)) {
|
||||
return false;
|
||||
}
|
||||
if (!Objects.equals(title, book.title)) {
|
||||
return false;
|
||||
}
|
||||
return Objects.equals(author, book.author);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = bookId != null ? bookId.hashCode() : 0;
|
||||
result = 31 * result + (title != null ? title.hashCode() : 0);
|
||||
result = 31 * result + (author != null ? author.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Integer getBookId() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
public void setBookId(Integer bookId) {
|
||||
this.bookId = bookId;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
package com.ossez.jackson.tocollection;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.type.CollectionType;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class JsonToCollectionUtil {
|
||||
|
||||
private JsonToCollectionUtil(){}
|
||||
|
||||
public static <T> List<T> jsonArrayToList(String json, Class<T> elementClass) throws IOException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, elementClass);
|
||||
return objectMapper.readValue(json, listType);
|
||||
}
|
||||
|
||||
public static <T> List<T> jsonArrayToList2(String json, Class<T> elementClass) throws IOException {
|
||||
return new ObjectMapper().readValue(json, new TypeReference<List<T>>() {});
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package com.ossez.jackson.xmlToJson;
|
||||
|
||||
public enum Color {
|
||||
PINK, BLUE, YELLOW, RED;
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
package com.ossez.jackson.xmlToJson;
|
||||
|
||||
public class Flower {
|
||||
|
||||
private String name;
|
||||
|
||||
private Color color;
|
||||
|
||||
private Integer petals;
|
||||
|
||||
public Flower() { }
|
||||
|
||||
public Flower(String name, Color color, Integer petals) {
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
this.petals = petals;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public Integer getPetals() {
|
||||
return petals;
|
||||
}
|
||||
|
||||
public void setPetals(Integer petals) {
|
||||
this.petals = petals;
|
||||
}
|
||||
}
|
|
@ -1,68 +0,0 @@
|
|||
package com.ossez.jackson.yaml;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Order {
|
||||
private String orderNo;
|
||||
private LocalDate date;
|
||||
private String customerName;
|
||||
private List<OrderLine> orderLines;
|
||||
|
||||
public Order() {
|
||||
|
||||
}
|
||||
|
||||
public Order(String orderNo, LocalDate date, String customerName, List<OrderLine> orderLines) {
|
||||
super();
|
||||
this.orderNo = orderNo;
|
||||
this.date = date;
|
||||
this.customerName = customerName;
|
||||
this.orderLines = orderLines;
|
||||
}
|
||||
|
||||
public String getOrderNo() {
|
||||
return orderNo;
|
||||
}
|
||||
|
||||
public void setOrderNo(String orderNo) {
|
||||
this.orderNo = orderNo;
|
||||
}
|
||||
|
||||
public LocalDate getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(LocalDate date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getCustomerName() {
|
||||
return customerName;
|
||||
}
|
||||
|
||||
public void setCustomerName(String customerName) {
|
||||
this.customerName = customerName;
|
||||
}
|
||||
|
||||
public List<OrderLine> getOrderLines() {
|
||||
if (orderLines == null) {
|
||||
orderLines = new ArrayList<>();
|
||||
}
|
||||
return orderLines;
|
||||
}
|
||||
|
||||
public void setOrderLines(List<OrderLine> orderLines) {
|
||||
if (orderLines == null) {
|
||||
orderLines = new ArrayList<>();
|
||||
}
|
||||
this.orderLines = orderLines;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Order [orderNo=" + orderNo + ", date=" + date + ", customerName=" + customerName + ", orderLines=" + orderLines + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
package com.ossez.jackson.yaml;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class OrderLine {
|
||||
private String item;
|
||||
private int quantity;
|
||||
private BigDecimal unitPrice;
|
||||
|
||||
public OrderLine() {
|
||||
|
||||
}
|
||||
|
||||
public OrderLine(String item, int quantity, BigDecimal unitPrice) {
|
||||
super();
|
||||
this.item = item;
|
||||
this.quantity = quantity;
|
||||
this.unitPrice = unitPrice;
|
||||
}
|
||||
|
||||
public String getItem() {
|
||||
return item;
|
||||
}
|
||||
|
||||
public void setItem(String item) {
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public BigDecimal getUnitPrice() {
|
||||
return unitPrice;
|
||||
}
|
||||
|
||||
public void setUnitPrice(BigDecimal unitPrice) {
|
||||
this.unitPrice = unitPrice;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OrderLine [item=" + item + ", quantity=" + quantity + ", unitPrice=" + unitPrice + "]";
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
item,quantity,unitPrice
|
||||
"No. 9 Sprockets",12,1.23
|
||||
"Widget (10mm)",4,3.45
|
|
|
@ -1,9 +0,0 @@
|
|||
[ {
|
||||
"item" : "No. 9 Sprockets",
|
||||
"quantity" : 12,
|
||||
"unitPrice" : 1.23
|
||||
}, {
|
||||
"item" : "Widget (10mm)",
|
||||
"quantity" : 4,
|
||||
"unitPrice" : 3.45
|
||||
} ]
|
|
@ -1,13 +0,0 @@
|
|||
[ {
|
||||
"bookId" : 1,
|
||||
"title" : "A Song of Ice and Fire",
|
||||
"author" : "George R. R. Martin"
|
||||
}, {
|
||||
"bookId" : 2,
|
||||
"title" : "The Hitchhiker's Guide to the Galaxy",
|
||||
"author" : "Douglas Adams"
|
||||
}, {
|
||||
"bookId" : 3,
|
||||
"title" : "Hackers And Painters",
|
||||
"author" : "Paul Graham"
|
||||
} ]
|
|
@ -1,17 +0,0 @@
|
|||
<ArrayList>
|
||||
<item>
|
||||
<bookId>1</bookId>
|
||||
<title>A Song of Ice and Fire</title>
|
||||
<author>George R. R. Martin</author>
|
||||
</item>
|
||||
<item>
|
||||
<bookId>2</bookId>
|
||||
<title>The Hitchhiker's Guide to the Galaxy</title>
|
||||
<author>Douglas Adams</author>
|
||||
</item>
|
||||
<item>
|
||||
<bookId>3</bookId>
|
||||
<title>Hackers And Painters</title>
|
||||
<author>Paul Graham</author>
|
||||
</item>
|
||||
</ArrayList>
|
|
@ -1,65 +0,0 @@
|
|||
package com.ossez.jackson.csv;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.google.common.io.Files;
|
||||
|
||||
public class CsvUnitTest {
|
||||
|
||||
private File csvFromJson;
|
||||
private File jsonFromCsv;
|
||||
private File formattedCsvFromJson;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
csvFromJson = new File("src/main/resources/csv/csvFromJson.csv");
|
||||
jsonFromCsv = new File("src/main/resources/csv/jsonFromCsv.json");
|
||||
formattedCsvFromJson = new File("src/main/resources/csv/formattedCsvFromJson.csv");
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
csvFromJson.deleteOnExit();
|
||||
jsonFromCsv.deleteOnExit();
|
||||
formattedCsvFromJson.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonInput_thenWriteCsv() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.JsonToCsv(new File("src/main/resources/csv/orderLines.json"), csvFromJson);
|
||||
|
||||
assertEquals(readFile(csvFromJson.getAbsolutePath()), readFile("src/test/resources/csv/expectedCsvFromJson.csv"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCsvInput_thenWritesJson() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.csvToJson(new File("src/main/resources/csv/orderLines.csv"), jsonFromCsv);
|
||||
|
||||
assertEquals(readFile(jsonFromCsv.getAbsolutePath()), readFile("src/test/resources/csv/expectedJsonFromCsv.json"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonInput_thenWriteFormattedCsvOutput() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.JsonToFormattedCsv(new File("src/main/resources/csv/orderLines.json"), formattedCsvFromJson);
|
||||
|
||||
assertEquals(readFile(formattedCsvFromJson.getAbsolutePath()), readFile("src/test/resources/csv/expectedFormattedCsvFromJson.csv"));
|
||||
|
||||
}
|
||||
|
||||
private List<String> readFile(String filename) throws IOException {
|
||||
return Files.readLines(new File(filename), Charset.forName("utf-8"));
|
||||
}
|
||||
|
||||
};
|
|
@ -1,69 +0,0 @@
|
|||
package com.ossez.jackson.dynamicobject;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class DynamicObjectDeserializationUnitTest {
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
objectMapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
private String readResource(String path) {
|
||||
try (Scanner scanner = new Scanner(getClass().getResourceAsStream(path), "UTF-8")) {
|
||||
return scanner.useDelimiter("\\A").next();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJsonString_whenParsingToJsonNode_thenItMustContainDynamicProperties() throws JsonParseException, JsonMappingException, IOException {
|
||||
// given
|
||||
String json = readResource("/deserialize-dynamic-object/embedded.json");
|
||||
|
||||
// when
|
||||
ProductJsonNode product = objectMapper.readValue(json, ProductJsonNode.class);
|
||||
|
||||
// then
|
||||
assertThat(product.getName()).isEqualTo("Pear yPhone 72");
|
||||
assertThat(product.getDetails().get("audioConnector").asText()).isEqualTo("none");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJsonString_whenParsingToMap_thenItMustContainDynamicProperties() throws JsonParseException, JsonMappingException, IOException {
|
||||
// given
|
||||
String json = readResource("/deserialize-dynamic-object/embedded.json");
|
||||
|
||||
// when
|
||||
ProductMap product = objectMapper.readValue(json, ProductMap.class);
|
||||
|
||||
// then
|
||||
assertThat(product.getName()).isEqualTo("Pear yPhone 72");
|
||||
assertThat(product.getDetails().get("audioConnector")).isEqualTo("none");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJsonString_whenParsingWithJsonAnySetter_thenItMustContainDynamicProperties() throws JsonParseException, JsonMappingException, IOException {
|
||||
// given
|
||||
String json = readResource("/deserialize-dynamic-object/flat.json");
|
||||
|
||||
// when
|
||||
Product product = objectMapper.readValue(json, Product.class);
|
||||
|
||||
// then
|
||||
assertThat(product.getName()).isEqualTo("Pear yPhone 72");
|
||||
assertThat(product.getDetails().get("audioConnector")).isEqualTo("none");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
package com.ossez.jackson.multiplefields;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class MapMultipleFieldsToSingleFieldUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoJsonFormats_whenDeserialized_thenWeatherObjectsCreated() throws Exception {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
Weather weather = mapper.readValue("{" +
|
||||
"\"location\": \"London\"," +
|
||||
"\"temp\": 15," +
|
||||
"\"weather\": \"Cloudy\"" +
|
||||
"}", Weather.class);
|
||||
|
||||
assertEquals("London", weather.getLocation());
|
||||
assertEquals("Cloudy", weather.getOutlook());
|
||||
assertEquals(15, weather.getTemp());
|
||||
|
||||
weather = mapper.readValue("{" +
|
||||
"\"place\": \"Lisbon\"," +
|
||||
"\"temperature\": 35," +
|
||||
"\"outlook\": \"Sunny\"" +
|
||||
"}", Weather.class);
|
||||
|
||||
assertEquals("Lisbon", weather.getLocation());
|
||||
assertEquals("Sunny", weather.getOutlook());
|
||||
assertEquals(35, weather.getTemp());
|
||||
|
||||
}
|
||||
}
|
|
@ -1,118 +0,0 @@
|
|||
package com.ossez.jackson.streaming;
|
||||
|
||||
import com.fasterxml.jackson.core.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class StreamingAPIUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenJsonGenerator_whenAppendJsonToIt_thenGenerateJson() throws IOException {
|
||||
// given
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
JsonFactory jfactory = new JsonFactory();
|
||||
JsonGenerator jGenerator = jfactory.createGenerator(stream, JsonEncoding.UTF8);
|
||||
|
||||
// when
|
||||
jGenerator.writeStartObject();
|
||||
jGenerator.writeStringField("name", "Tom");
|
||||
jGenerator.writeNumberField("age", 25);
|
||||
jGenerator.writeFieldName("address");
|
||||
jGenerator.writeStartArray();
|
||||
jGenerator.writeString("Poland");
|
||||
jGenerator.writeString("5th avenue");
|
||||
jGenerator.writeEndArray();
|
||||
jGenerator.writeEndObject();
|
||||
jGenerator.close();
|
||||
|
||||
// then
|
||||
String json = new String(stream.toByteArray(), "UTF-8");
|
||||
assertEquals(json, "{\"name\":\"Tom\",\"age\":25,\"address\":[\"Poland\",\"5th avenue\"]}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJson_whenReadItUsingStreamAPI_thenShouldCreateProperJsonObject() throws IOException {
|
||||
// given
|
||||
String json = "{\"name\":\"Tom\",\"age\":25,\"address\":[\"Poland\",\"5th avenue\"]}";
|
||||
JsonFactory jfactory = new JsonFactory();
|
||||
JsonParser jParser = jfactory.createParser(json);
|
||||
|
||||
String parsedName = null;
|
||||
Integer parsedAge = null;
|
||||
List<String> addresses = new LinkedList<>();
|
||||
|
||||
// when
|
||||
while (jParser.nextToken() != JsonToken.END_OBJECT) {
|
||||
|
||||
String fieldname = jParser.getCurrentName();
|
||||
if ("name".equals(fieldname)) {
|
||||
jParser.nextToken();
|
||||
parsedName = jParser.getText();
|
||||
|
||||
}
|
||||
|
||||
if ("age".equals(fieldname)) {
|
||||
jParser.nextToken();
|
||||
parsedAge = jParser.getIntValue();
|
||||
|
||||
}
|
||||
|
||||
if ("address".equals(fieldname)) {
|
||||
jParser.nextToken();
|
||||
|
||||
while (jParser.nextToken() != JsonToken.END_ARRAY) {
|
||||
addresses.add(jParser.getText());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
jParser.close();
|
||||
|
||||
// then
|
||||
assertEquals(parsedName, "Tom");
|
||||
assertEquals(parsedAge, (Integer) 25);
|
||||
assertEquals(addresses, Arrays.asList("Poland", "5th avenue"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJson_whenWantToExtractPartOfIt_thenShouldExtractOnlyNeededFieldWithoutGoingThroughWholeJSON() throws IOException {
|
||||
// given
|
||||
String json = "{\"name\":\"Tom\",\"age\":25,\"address\":[\"Poland\",\"5th avenue\"]}";
|
||||
JsonFactory jfactory = new JsonFactory();
|
||||
JsonParser jParser = jfactory.createParser(json);
|
||||
|
||||
String parsedName = null;
|
||||
Integer parsedAge = null;
|
||||
List<String> addresses = new LinkedList<>();
|
||||
|
||||
// when
|
||||
while (jParser.nextToken() != JsonToken.END_OBJECT) {
|
||||
|
||||
String fieldname = jParser.getCurrentName();
|
||||
|
||||
if ("age".equals(fieldname)) {
|
||||
jParser.nextToken();
|
||||
parsedAge = jParser.getIntValue();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
jParser.close();
|
||||
|
||||
// then
|
||||
assertNull(parsedName);
|
||||
assertEquals(parsedAge, (Integer) 25);
|
||||
assertTrue(addresses.isEmpty());
|
||||
|
||||
}
|
||||
}
|
|
@ -1,130 +0,0 @@
|
|||
package com.ossez.jackson.tocollection;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.type.CollectionType;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
public class DeserializeToJavaCollectionUnitTest {
|
||||
private ObjectMapper objectMapper;
|
||||
private XmlMapper xmlMapper;
|
||||
private List<Book> expectedBookList;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
objectMapper = new ObjectMapper();
|
||||
xmlMapper = new XmlMapper();
|
||||
expectedBookList = Lists.newArrayList(
|
||||
new Book(1, "A Song of Ice and Fire", "George R. R. Martin"),
|
||||
new Book(2, "The Hitchhiker's Guide to the Galaxy", "Douglas Adams"),
|
||||
new Book(3, "Hackers And Painters", "Paul Graham"));
|
||||
}
|
||||
|
||||
private String readFile(String path) {
|
||||
try (Scanner scanner = new Scanner(getClass().getResourceAsStream(path), "UTF-8")) {
|
||||
return scanner.useDelimiter("\\A").next();
|
||||
}
|
||||
}
|
||||
|
||||
/*====================
|
||||
* JSON tests
|
||||
*====================
|
||||
*/
|
||||
@Test
|
||||
void givenJsonString_whenDeserializingToList_thenThrowingClassCastException() throws JsonProcessingException {
|
||||
String jsonString = readFile("/to-java-collection/books.json");
|
||||
List<Book> bookList = objectMapper.readValue(jsonString, ArrayList.class);
|
||||
assertThat(bookList).size().isEqualTo(3);
|
||||
assertThatExceptionOfType(ClassCastException.class)
|
||||
.isThrownBy(() -> bookList.get(0).getBookId())
|
||||
.withMessageMatching(".*java.util.LinkedHashMap cannot be cast to .*com.baeldung.jackson.tocollection.Book.*");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJsonString_whenDeserializingWithTypeReference_thenGetExpectedList() throws JsonProcessingException {
|
||||
String jsonString = readFile("/to-java-collection/books.json");
|
||||
List<Book> bookList = objectMapper.readValue(jsonString, new TypeReference<List<Book>>() {});
|
||||
assertThat(bookList.get(0)).isInstanceOf(Book.class);
|
||||
assertThat(bookList).isEqualTo(expectedBookList);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJsonString_whenDeserializingWithJavaType_thenGetExpectedList() throws JsonProcessingException {
|
||||
String jsonString = readFile("/to-java-collection/books.json");
|
||||
CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, Book.class);
|
||||
List<Book> bookList = objectMapper.readValue(jsonString, listType);
|
||||
assertThat(bookList.get(0)).isInstanceOf(Book.class);
|
||||
assertThat(bookList).isEqualTo(expectedBookList);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJsonString_whenDeserializingWithConvertValueAndTypeReference_thenGetExpectedList() throws JsonProcessingException {
|
||||
String jsonString = readFile("/to-java-collection/books.json");
|
||||
JsonNode jsonNode = objectMapper.readTree(jsonString);
|
||||
List<Book> bookList = objectMapper.convertValue(jsonNode, new TypeReference<List<Book>>() {});
|
||||
assertThat(bookList.get(0)).isInstanceOf(Book.class);
|
||||
assertThat(bookList).isEqualTo(expectedBookList);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJsonString_whenDeserializingWithConvertValueAndJavaType_thenGetExpectedList() throws JsonProcessingException {
|
||||
String jsonString = readFile("/to-java-collection/books.json");
|
||||
JsonNode jsonNode = objectMapper.readTree(jsonString);
|
||||
List<Book> bookList = objectMapper.convertValue(jsonNode, objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, Book.class));
|
||||
assertThat(bookList.get(0)).isInstanceOf(Book.class);
|
||||
assertThat(bookList).isEqualTo(expectedBookList);
|
||||
}
|
||||
|
||||
/*====================
|
||||
* XML tests
|
||||
*====================
|
||||
*/
|
||||
@Test
|
||||
void givenXml_whenDeserializingToList_thenThrowingClassCastException() throws JsonProcessingException {
|
||||
String xml = readFile("/to-java-collection/books.xml");
|
||||
List<Book> bookList = xmlMapper.readValue(xml, ArrayList.class);
|
||||
assertThat(bookList).size().isEqualTo(3);
|
||||
assertThatExceptionOfType(ClassCastException.class)
|
||||
.isThrownBy(() -> bookList.get(0).getBookId())
|
||||
.withMessageMatching(".*java.util.LinkedHashMap cannot be cast to .*com.baeldung.jackson.tocollection.Book.*");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenXml_whenDeserializingWithTypeReference_thenGetExpectedList() throws JsonProcessingException {
|
||||
String xml = readFile("/to-java-collection/books.xml");
|
||||
List<Book> bookList = xmlMapper.readValue(xml, new TypeReference<List<Book>>() {});
|
||||
assertThat(bookList.get(0)).isInstanceOf(Book.class);
|
||||
assertThat(bookList).isEqualTo(expectedBookList);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenXml_whenDeserializingWithConvertValueAndTypeReference_thenGetExpectedList() throws JsonProcessingException {
|
||||
String xml = readFile("/to-java-collection/books.xml");
|
||||
List node = xmlMapper.readValue(xml, List.class);
|
||||
List<Book> bookList = xmlMapper.convertValue(node, new TypeReference<List<Book>>() {});
|
||||
assertThat(bookList.get(0)).isInstanceOf(Book.class);
|
||||
assertThat(bookList).isEqualTo(expectedBookList);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenXml_whenDeserializingWithConvertValueAndJavaType_thenGetExpectedList() throws JsonProcessingException {
|
||||
String xml = readFile("/to-java-collection/books.xml");
|
||||
List node = xmlMapper.readValue(xml, List.class);
|
||||
List<Book> bookList = xmlMapper.convertValue(node, objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, Book.class));
|
||||
assertThat(bookList.get(0)).isInstanceOf(Book.class);
|
||||
assertThat(bookList).isEqualTo(expectedBookList);
|
||||
}
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
package com.ossez.jackson.tocollection;
|
||||
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
class JsonToCollectionUtilUnitTest {
|
||||
|
||||
private List<Book> expectedBookList;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
expectedBookList = Lists.newArrayList(
|
||||
new Book(1, "A Song of Ice and Fire", "George R. R. Martin"),
|
||||
new Book(2, "The Hitchhiker's Guide to the Galaxy", "Douglas Adams"),
|
||||
new Book(3, "Hackers And Painters", "Paul Graham"));
|
||||
}
|
||||
|
||||
private String readFile(String path) {
|
||||
try (Scanner scanner = new Scanner(getClass().getResourceAsStream(path), "UTF-8")) {
|
||||
return scanner.useDelimiter("\\A").next();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJsonString_whenCalljsonArrayToList_thenGetExpectedList() throws IOException {
|
||||
String jsonString = readFile("/to-java-collection/books.json");
|
||||
List<Book> bookList = JsonToCollectionUtil.jsonArrayToList(jsonString, Book.class);
|
||||
assertThat(bookList.get(0)).isInstanceOf(Book.class);
|
||||
assertThat(bookList).isEqualTo(expectedBookList);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJsonString_whenCalljsonArrayToList2_thenGetException() throws IOException {
|
||||
String jsonString = readFile("/to-java-collection/books.json");
|
||||
List<Book> bookList = JsonToCollectionUtil.jsonArrayToList2(jsonString, Book.class);
|
||||
assertThat(bookList).size().isEqualTo(3);
|
||||
assertThatExceptionOfType(ClassCastException.class)
|
||||
.isThrownBy(() -> bookList.get(0).getBookId())
|
||||
.withMessageMatching(".*java.util.LinkedHashMap cannot be cast to .*com.baeldung.jackson.tocollection.Book.*");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
package com.ossez.jackson.xmlToJson;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class XmlToJsonUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() throws IOException{
|
||||
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
|
||||
|
||||
XmlMapper xmlMapper = new XmlMapper();
|
||||
Flower poppy = xmlMapper.readValue(flowerXML, Flower.class);
|
||||
|
||||
assertEquals(poppy.getName(), "Poppy");
|
||||
assertEquals(poppy.getColor(), Color.RED);
|
||||
assertEquals(poppy.getPetals(), new Integer(9));
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String json = mapper.writeValueAsString(poppy);
|
||||
|
||||
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() throws IOException {
|
||||
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
|
||||
|
||||
XmlMapper xmlMapper = new XmlMapper();
|
||||
JsonNode node = xmlMapper.readTree(flowerXML.getBytes());
|
||||
|
||||
ObjectMapper jsonMapper = new ObjectMapper();
|
||||
String json = jsonMapper.writeValueAsString(node);
|
||||
|
||||
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}");
|
||||
}
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
package com.ossez.jackson.yaml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerationException;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
|
||||
|
||||
public class YamlUnitTest {
|
||||
private ObjectMapper mapper;
|
||||
private File orderOutput;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER));
|
||||
mapper.findAndRegisterModules();
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
orderOutput = new File("src/test/resources/yaml/orderOutput.yaml");
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
orderOutput.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenYamlInput_ObjectCreated() throws JsonParseException, JsonMappingException, IOException {
|
||||
Order order = mapper.readValue(new File("src/test/resources/yaml/orderInput.yaml"), Order.class);
|
||||
assertEquals("A001", order.getOrderNo());
|
||||
assertEquals(LocalDate.parse("2019-04-17", DateTimeFormatter.ISO_DATE), order.getDate());
|
||||
assertEquals("Customer, Joe", order.getCustomerName());
|
||||
assertEquals(2, order.getOrderLines()
|
||||
.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenYamlObject_FileWritten() throws JsonGenerationException, JsonMappingException, IOException {
|
||||
List<OrderLine> lines = new ArrayList<>();
|
||||
lines.add(new OrderLine("Copper Wire (200ft)", 1, new BigDecimal(50.67).setScale(2, RoundingMode.HALF_UP)));
|
||||
lines.add(new OrderLine("Washers (1/4\")", 24, new BigDecimal(.15).setScale(2, RoundingMode.HALF_UP)));
|
||||
Order order = new Order(
|
||||
"B-9910",
|
||||
LocalDate.parse("2019-04-18", DateTimeFormatter.ISO_DATE),
|
||||
"Customer, Jane",
|
||||
lines);
|
||||
mapper.writeValue(orderOutput, order);
|
||||
|
||||
File outputYaml = new File(orderOutput.getAbsolutePath());
|
||||
assertTrue(outputYaml.exists());
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
item,quantity,unitPrice
|
||||
"No. 9 Sprockets",12,1.23
|
||||
"Widget (10mm)",4,3.45
|
|
|
@ -1,3 +0,0 @@
|
|||
count,name
|
||||
12,"No. 9 Sprockets"
|
||||
4,"Widget (10mm)"
|
|
|
@ -1,9 +0,0 @@
|
|||
[ {
|
||||
"item" : "No. 9 Sprockets",
|
||||
"quantity" : 12,
|
||||
"unitPrice" : 1.23
|
||||
}, {
|
||||
"item" : "Widget (10mm)",
|
||||
"quantity" : 4,
|
||||
"unitPrice" : 3.45
|
||||
} ]
|
|
@ -1,8 +0,0 @@
|
|||
{
|
||||
"name": "Pear yPhone 72",
|
||||
"category": "cellphone",
|
||||
"details": {
|
||||
"displayAspectRatio": "97:3",
|
||||
"audioConnector": "none"
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
{
|
||||
"name": "Pear yPhone 72",
|
||||
"category": "cellphone",
|
||||
"displayAspectRatio": "97:3",
|
||||
"audioConnector": "none"
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
orderNo: A001
|
||||
date: 2019-04-17
|
||||
customerName: Customer, Joe
|
||||
orderLines:
|
||||
- item: No. 9 Sprockets
|
||||
quantity: 12
|
||||
unitPrice: 1.23
|
||||
- item: Widget (10mm)
|
||||
quantity: 4
|
||||
unitPrice: 3.45
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
## Jackson 转换
|
||||
|
||||
这个模块主要是针对 Jackson 转换(Jackson Conversions) 有关的文章。
|
||||
|
||||
### Relevant Articles:
|
||||
- [Jackson – Unmarshall to Collection/Array](https://www.baeldung.com/jackson-collection-array)
|
||||
- [Jackson Date](https://www.baeldung.com/jackson-serialize-dates)
|
||||
- [Jackson – Working with Maps and Nulls](https://www.baeldung.com/jackson-map-null-values-or-null-key)
|
||||
- [Jackson – Decide What Fields Get Serialized/Deserialized](https://www.baeldung.com/jackson-field-serializable-deserializable-or-not)
|
||||
- [XML Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-xml-serialization-and-deserialization)
|
||||
- [Map Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-map)
|
||||
- [How to Serialize and Deserialize Enums with Jackson](https://www.baeldung.com/jackson-serialize-enums)
|
||||
- [使用 Jackson – 将字符串转换为 JsonNode 对象](https://www.ossez.com/t/jackson-jsonnode/13724)
|
||||
- [Mapping Nested Values with Jackson](https://www.baeldung.com/jackson-nested-values)
|
||||
- [Deserialize Immutable Objects with Jackson](https://www.baeldung.com/jackson-deserialize-immutable-objects)
|
||||
- More articles: [[next -->]](../jackson-conversions-2)
|
|
@ -1,39 +0,0 @@
|
|||
<?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-conversions</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>jackson-conversions</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.ossez</groupId>
|
||||
<artifactId>jackson-modules</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-joda</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>jackson-conversions</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -1,36 +0,0 @@
|
|||
package com.ossez.jackson.date;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
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 CustomDateDeserializer extends StdDeserializer<Date> {
|
||||
|
||||
private static final long serialVersionUID = -5451717385630622729L;
|
||||
private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
|
||||
|
||||
public CustomDateDeserializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomDateDeserializer(final Class<?> vc) {
|
||||
super(vc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException {
|
||||
final String date = jsonparser.getText();
|
||||
try {
|
||||
return formatter.parse(date);
|
||||
} catch (final ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
package com.ossez.jackson.date;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
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 CustomDateSerializer extends StdSerializer<Date> {
|
||||
|
||||
private static final long serialVersionUID = -2894356342227378312L;
|
||||
private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
|
||||
|
||||
public CustomDateSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomDateSerializer(final Class<Date> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final Date value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException {
|
||||
gen.writeString(formatter.format(value));
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
package com.ossez.jackson.date;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
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 CustomDateTimeSerializer extends StdSerializer<DateTime> {
|
||||
|
||||
private static final long serialVersionUID = -3927232057990121460L;
|
||||
|
||||
public CustomDateTimeSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomDateTimeSerializer(final Class<DateTime> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
private static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
|
||||
|
||||
@Override
|
||||
public void serialize(final DateTime value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException {
|
||||
gen.writeString(formatter.print(value));
|
||||
}
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
package com.ossez.jackson.date;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
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 CustomLocalDateTimeSerializer extends StdSerializer<LocalDateTime> {
|
||||
|
||||
private static final long serialVersionUID = -7449444168934819290L;
|
||||
private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
||||
|
||||
public CustomLocalDateTimeSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomLocalDateTimeSerializer(final Class<LocalDateTime> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final LocalDateTime value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException {
|
||||
gen.writeString(formatter.format(value));
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package com.ossez.jackson.date;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Event {
|
||||
public String name;
|
||||
public Date eventDate;
|
||||
|
||||
public Event() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Event(final String name, final Date eventDate) {
|
||||
this.name = name;
|
||||
this.eventDate = eventDate;
|
||||
}
|
||||
|
||||
public Date getEventDate() {
|
||||
return eventDate;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
package com.ossez.jackson.date;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
public class EventWithFormat {
|
||||
public String name;
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
|
||||
public Date eventDate;
|
||||
|
||||
public EventWithFormat() {
|
||||
super();
|
||||
}
|
||||
|
||||
public EventWithFormat(final String name, final Date eventDate) {
|
||||
this.name = name;
|
||||
this.eventDate = eventDate;
|
||||
}
|
||||
|
||||
public Date getEventDate() {
|
||||
return eventDate;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
package com.ossez.jackson.date;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
public class EventWithJodaTime {
|
||||
public String name;
|
||||
|
||||
@JsonSerialize(using = CustomDateTimeSerializer.class)
|
||||
public DateTime eventDate;
|
||||
|
||||
public EventWithJodaTime() {
|
||||
super();
|
||||
}
|
||||
|
||||
public EventWithJodaTime(final String name, final DateTime eventDate) {
|
||||
this.name = name;
|
||||
this.eventDate = eventDate;
|
||||
}
|
||||
|
||||
public DateTime getEventDate() {
|
||||
return eventDate;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
package com.ossez.jackson.date;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class EventWithLocalDate {
|
||||
public String name;
|
||||
|
||||
@JsonDeserialize(using = LocalDateDeserializer.class)
|
||||
@JsonSerialize(using = LocalDateSerializer.class)
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
|
||||
public LocalDate eventDate;
|
||||
|
||||
public EventWithLocalDate() {}
|
||||
|
||||
public EventWithLocalDate(final String name, final LocalDate eventDate) {
|
||||
this.name = name;
|
||||
this.eventDate = eventDate;
|
||||
}
|
||||
|
||||
public LocalDate getEventDate() {
|
||||
return eventDate;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
package com.ossez.jackson.date;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
public class EventWithLocalDateTime {
|
||||
public String name;
|
||||
|
||||
@JsonSerialize(using = CustomLocalDateTimeSerializer.class)
|
||||
public LocalDateTime eventDate;
|
||||
|
||||
public EventWithLocalDateTime() {
|
||||
super();
|
||||
}
|
||||
|
||||
public EventWithLocalDateTime(final String name, final LocalDateTime eventDate) {
|
||||
this.name = name;
|
||||
this.eventDate = eventDate;
|
||||
}
|
||||
|
||||
public LocalDateTime getEventDate() {
|
||||
return eventDate;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
package com.ossez.jackson.date;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
public class EventWithSerializer {
|
||||
public String name;
|
||||
|
||||
@JsonDeserialize(using = CustomDateDeserializer.class)
|
||||
@JsonSerialize(using = CustomDateSerializer.class)
|
||||
public Date eventDate;
|
||||
|
||||
public EventWithSerializer() {
|
||||
super();
|
||||
}
|
||||
|
||||
public EventWithSerializer(final String name, final Date eventDate) {
|
||||
this.name = name;
|
||||
this.eventDate = eventDate;
|
||||
}
|
||||
|
||||
public Date getEventDate() {
|
||||
return eventDate;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.ossez.jackson.enums.deserialization;
|
||||
|
||||
public class City {
|
||||
|
||||
private Distance distance;
|
||||
|
||||
public Distance getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(Distance distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
package com.ossez.jackson.enums.deserialization;
|
||||
|
||||
public enum Distance {
|
||||
|
||||
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private double meters;
|
||||
|
||||
private Distance(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public void setMeters(double meters) {
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package com.ossez.jackson.enums.deserialization.customdeserializer;
|
||||
|
||||
public class City {
|
||||
|
||||
private Distance distance;
|
||||
|
||||
public Distance getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(Distance distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
package com.ossez.jackson.enums.deserialization.customdeserializer;
|
||||
|
||||
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.JsonNode;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
|
||||
public class CustomEnumDeserializer extends StdDeserializer<Distance> {
|
||||
|
||||
private static final long serialVersionUID = -1166032307856492833L;
|
||||
|
||||
public CustomEnumDeserializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomEnumDeserializer(Class<?> c) {
|
||||
super(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Distance deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
|
||||
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
|
||||
|
||||
String unit = node.get("unit").asText();
|
||||
double meters = node.get("meters").asDouble();
|
||||
|
||||
for (Distance distance : Distance.values()) {
|
||||
|
||||
if (distance.getUnit().equals(unit) &&
|
||||
Double.compare(distance.getMeters(), meters) == 0) {
|
||||
|
||||
return distance;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
package com.ossez.jackson.enums.deserialization.customdeserializer;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
@JsonDeserialize(using = CustomEnumDeserializer.class)
|
||||
public enum Distance {
|
||||
|
||||
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private double meters;
|
||||
|
||||
private Distance(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public void setMeters(double meters) {
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.ossez.jackson.enums.deserialization.jsoncreator;
|
||||
|
||||
public class City {
|
||||
|
||||
private Distance distance;
|
||||
|
||||
public Distance getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(Distance distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
package com.ossez.jackson.enums.deserialization.jsoncreator;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public enum Distance {
|
||||
|
||||
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private double meters;
|
||||
|
||||
private Distance(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public void setMeters(double meters) {
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
@JsonCreator
|
||||
public static Distance forValues(@JsonProperty("unit") String unit, @JsonProperty("meters") double meters) {
|
||||
|
||||
for (Distance distance : Distance.values()) {
|
||||
if (distance.unit.equals(unit) && Double.compare(distance.meters, meters) == 0) {
|
||||
|
||||
return distance;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package com.ossez.jackson.enums.deserialization.jsonproperty;
|
||||
|
||||
public class City {
|
||||
|
||||
private Distance distance;
|
||||
|
||||
public Distance getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(Distance distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
package com.ossez.jackson.enums.deserialization.jsonproperty;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public enum Distance {
|
||||
|
||||
@JsonProperty("distance-in-km")
|
||||
KILOMETER("km", 1000),
|
||||
|
||||
@JsonProperty("distance-in-miles")
|
||||
MILE("miles", 1609.34),
|
||||
|
||||
@JsonProperty("distance-in-meters")
|
||||
METER("meters", 1),
|
||||
|
||||
@JsonProperty("distance-in-inches")
|
||||
INCH("inches", 0.0254),
|
||||
|
||||
@JsonProperty("distance-in-cm")
|
||||
CENTIMETER("cm", 0.01),
|
||||
|
||||
@JsonProperty("distance-in-mm")
|
||||
MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private double meters;
|
||||
|
||||
private Distance(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public void setMeters(double meters) {
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package com.ossez.jackson.enums.deserialization.jsonvalue;
|
||||
|
||||
public class City {
|
||||
|
||||
private Distance distance;
|
||||
|
||||
public Distance getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(Distance distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package com.ossez.jackson.enums.deserialization.jsonvalue;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum Distance {
|
||||
|
||||
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private double meters;
|
||||
|
||||
private Distance(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public void setMeters(double meters) {
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
package com.ossez.jackson.enums.serialization;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
/**
|
||||
* Use @JsonFormat to handle representation of Enum as JSON (available since Jackson 2.1.2)
|
||||
* Use @JsonSerialize to configure a custom Jackson serializer
|
||||
*/
|
||||
// @JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||
@JsonSerialize(using = DistanceSerializer.class)
|
||||
public enum Distance {
|
||||
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private final double meters;
|
||||
|
||||
private Distance(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use @JsonValue to control marshalling output for an enum
|
||||
*/
|
||||
// @JsonValue
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Usage example: Distance.MILE.convertFromMeters(1205.5);
|
||||
*/
|
||||
public double convertFromMeters(double distanceInMeters) {
|
||||
return distanceInMeters / meters;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Usage example: Distance.MILE.convertToMeters(0.5);
|
||||
*/
|
||||
public double convertToMeters(double distanceInMeters) {
|
||||
return distanceInMeters * meters;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
package com.ossez.jackson.enums.serialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
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 DistanceSerializer extends StdSerializer<Distance> {
|
||||
|
||||
private static final long serialVersionUID = 1376504304439963619L;
|
||||
|
||||
public DistanceSerializer() {
|
||||
super(Distance.class);
|
||||
}
|
||||
|
||||
public DistanceSerializer(Class<Distance> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
public void serialize(Distance distance, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
generator.writeStartObject();
|
||||
generator.writeFieldName("name");
|
||||
generator.writeString(distance.name());
|
||||
generator.writeFieldName("unit");
|
||||
generator.writeString(distance.getUnit());
|
||||
generator.writeFieldName("meters");
|
||||
generator.writeNumber(distance.getMeters());
|
||||
generator.writeEndObject();
|
||||
}
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
package com.ossez.jackson.enums.withEnum;
|
||||
|
||||
public enum DistanceEnumSimple {
|
||||
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private final double meters;
|
||||
|
||||
private DistanceEnumSimple(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
package com.ossez.jackson.enums.withEnum;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||
public enum DistanceEnumWithJsonFormat {
|
||||
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private final double meters;
|
||||
|
||||
private DistanceEnumWithJsonFormat(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
package com.ossez.jackson.enums.withEnum;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum DistanceEnumWithValue {
|
||||
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private final double meters;
|
||||
|
||||
private DistanceEnumWithValue(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
package com.ossez.jackson.enums.withEnum;
|
||||
|
||||
import com.ossez.jackson.enums.serialization.Distance;
|
||||
|
||||
public class MyDtoWithEnumCustom {
|
||||
|
||||
private String stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
private Distance type;
|
||||
|
||||
public MyDtoWithEnumCustom() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyDtoWithEnumCustom(final String stringValue, final int intValue, final boolean booleanValue, final Distance type) {
|
||||
super();
|
||||
|
||||
this.stringValue = stringValue;
|
||||
this.intValue = intValue;
|
||||
this.booleanValue = booleanValue;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public String getStringValue() {
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
public void setStringValue(final String stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public int getIntValue() {
|
||||
return intValue;
|
||||
}
|
||||
|
||||
public void setIntValue(final int intValue) {
|
||||
this.intValue = intValue;
|
||||
}
|
||||
|
||||
public boolean isBooleanValue() {
|
||||
return booleanValue;
|
||||
}
|
||||
|
||||
public void setBooleanValue(final boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
public Distance getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(final Distance type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
package com.ossez.jackson.enums.withEnum;
|
||||
|
||||
public class MyDtoWithEnumJsonFormat {
|
||||
|
||||
private String stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
private DistanceEnumWithJsonFormat distanceType;
|
||||
|
||||
public MyDtoWithEnumJsonFormat() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyDtoWithEnumJsonFormat(final String stringValue, final int intValue, final boolean booleanValue, final DistanceEnumWithJsonFormat type) {
|
||||
super();
|
||||
|
||||
this.stringValue = stringValue;
|
||||
this.intValue = intValue;
|
||||
this.booleanValue = booleanValue;
|
||||
this.distanceType = type;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public String getStringValue() {
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
public void setStringValue(final String stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public int getIntValue() {
|
||||
return intValue;
|
||||
}
|
||||
|
||||
public void setIntValue(final int intValue) {
|
||||
this.intValue = intValue;
|
||||
}
|
||||
|
||||
public boolean isBooleanValue() {
|
||||
return booleanValue;
|
||||
}
|
||||
|
||||
public void setBooleanValue(final boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
public DistanceEnumWithJsonFormat getDistanceType() {
|
||||
return distanceType;
|
||||
}
|
||||
|
||||
public void setDistanceType(final DistanceEnumWithJsonFormat type) {
|
||||
this.distanceType = type;
|
||||
}
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue