初始化项目设置中有关 Jackson 类库
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
## Jackson Cookbooks and Examples
|
||||
|
||||
This module contains articles about Jackson.
|
||||
|
||||
### The Course
|
||||
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Using Optional with Jackson](https://www.baeldung.com/jackson-optional)
|
||||
- [Compare Two JSON Objects with Jackson](https://www.baeldung.com/jackson-compare-two-json-objects)
|
||||
- [Jackson vs Gson](https://www.baeldung.com/jackson-vs-gson)
|
||||
- [Inheritance with Jackson](https://www.baeldung.com/jackson-inheritance)
|
||||
- [Working with Tree Model Nodes in Jackson](https://www.baeldung.com/jackson-json-node-tree-model)
|
||||
- [Get all the Keys in a JSON String Using JsonNode](https://www.baeldung.com/java-jsonnode-get-keys)
|
||||
- [Difference Between asText() and toString() in JsonNode](https://www.baeldung.com/java-jsonnode-astext-vs-tostring)
|
||||
- [Deserialize Generic Type with Jackson](https://www.baeldung.com/java-deserialize-generic-type-with-jackson)
|
||||
- [Setting Default Values to Null Fields in Jackson Mapping](https://www.baeldung.com/java-jackson-mapping-default-values-null-fields)
|
||||
- [Removing JSON Elements With Jackson](https://www.baeldung.com/java-jackson-remove-json-elements)
|
||||
@@ -1,67 +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-core</artifactId>
|
||||
<name>jackson-core</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>jackson-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- marshalling -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-joda</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.module</groupId>
|
||||
<artifactId>jackson-module-jsonSchema</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jdk8</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>json-schema-validator</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>json-path</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>jackson-core</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<rest-assured.version>5.4.0</rest-assured.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
-43
@@ -1,43 +0,0 @@
|
||||
package com.baeldung.jackson.deserialization.jacksoninject;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonInject;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class Person {
|
||||
|
||||
@JacksonInject
|
||||
private UUID id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Person() {
|
||||
|
||||
}
|
||||
|
||||
public Person(String firstName, String lastName) {
|
||||
this.id = UUID.randomUUID();
|
||||
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;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
package com.baeldung.jackson.deserialization.jsonanysetter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
|
||||
public class Inventory {
|
||||
|
||||
private Map<String, Float> countryDeliveryCost = new HashMap<>();
|
||||
|
||||
public Map<String, Float> getCountryDeliveryCost() {
|
||||
return countryDeliveryCost;
|
||||
}
|
||||
|
||||
@JsonAnySetter
|
||||
public void addCountryDeliveryCost(String country, Float cost) {
|
||||
countryDeliveryCost.put(country, cost);
|
||||
}
|
||||
}
|
||||
-75
@@ -1,75 +0,0 @@
|
||||
package com.baeldung.jackson.deserialization.jsondeserialize;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
public class Book {
|
||||
|
||||
private UUID id;
|
||||
private String title;
|
||||
private float price;
|
||||
private String ISBN;
|
||||
|
||||
@JsonDeserialize(using = CustomDateDeserializer.class)
|
||||
private Date published;
|
||||
private BigDecimal pages;
|
||||
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Book(String title) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getISBN() {
|
||||
return ISBN;
|
||||
}
|
||||
|
||||
public void setISBN(String ISBN) {
|
||||
this.ISBN = ISBN;
|
||||
}
|
||||
|
||||
public Date getPublished() {
|
||||
return published;
|
||||
}
|
||||
|
||||
public void setPublished(Date published) {
|
||||
this.published = published;
|
||||
}
|
||||
|
||||
public BigDecimal getPages() {
|
||||
return pages;
|
||||
}
|
||||
|
||||
public void setPages(BigDecimal pages) {
|
||||
this.pages = pages;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(float price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
package com.baeldung.jackson.deserialization.jsondeserialize;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class CustomDateDeserializer extends StdDeserializer<Date> {
|
||||
|
||||
private static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
|
||||
|
||||
public CustomDateDeserializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomDateDeserializer(Class<?> vc) {
|
||||
super(vc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date deserialize(JsonParser jsonparser, DeserializationContext context) throws IOException {
|
||||
String date = jsonparser.getText();
|
||||
try {
|
||||
return formatter.parse(date);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.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;
|
||||
}
|
||||
}
|
||||
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.jackson.inheritance;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "eventType")
|
||||
abstract public class Event {
|
||||
private final String id;
|
||||
private final Long timestamp;
|
||||
|
||||
@JsonCreator
|
||||
public Event(@JsonProperty("id") String id, @JsonProperty("timestamp") Long timestamp) {
|
||||
this.id = id;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public Long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
-96
@@ -1,96 +0,0 @@
|
||||
package com.baeldung.jackson.inheritance;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
public class IgnoranceAnnotationStructure {
|
||||
public static abstract class Vehicle {
|
||||
private String make;
|
||||
private String model;
|
||||
|
||||
protected Vehicle() {
|
||||
}
|
||||
|
||||
protected Vehicle(String make, String model) {
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public void setMake(String make) {
|
||||
this.make = make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonIgnoreProperties({ "model", "seatingCapacity" })
|
||||
public static abstract class Car extends Vehicle {
|
||||
private int seatingCapacity;
|
||||
@JsonIgnore
|
||||
private double topSpeed;
|
||||
|
||||
protected Car() {
|
||||
}
|
||||
|
||||
protected Car(String make, String model, int seatingCapacity, double topSpeed) {
|
||||
super(make, model);
|
||||
this.seatingCapacity = seatingCapacity;
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
|
||||
public int getSeatingCapacity() {
|
||||
return seatingCapacity;
|
||||
}
|
||||
|
||||
public void setSeatingCapacity(int seatingCapacity) {
|
||||
this.seatingCapacity = seatingCapacity;
|
||||
}
|
||||
|
||||
public double getTopSpeed() {
|
||||
return topSpeed;
|
||||
}
|
||||
|
||||
public void setTopSpeed(double topSpeed) {
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Sedan extends Car {
|
||||
public Sedan() {
|
||||
}
|
||||
|
||||
public Sedan(String make, String model, int seatingCapacity, double topSpeed) {
|
||||
super(make, model, seatingCapacity, topSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Crossover extends Car {
|
||||
private double towingCapacity;
|
||||
|
||||
public Crossover() {
|
||||
}
|
||||
|
||||
public Crossover(String make, String model, int seatingCapacity, double topSpeed, double towingCapacity) {
|
||||
super(make, model, seatingCapacity, topSpeed);
|
||||
this.towingCapacity = towingCapacity;
|
||||
}
|
||||
|
||||
public double getTowingCapacity() {
|
||||
return towingCapacity;
|
||||
}
|
||||
|
||||
public void setTowingCapacity(double towingCapacity) {
|
||||
this.towingCapacity = towingCapacity;
|
||||
}
|
||||
}
|
||||
}
|
||||
-91
@@ -1,91 +0,0 @@
|
||||
package com.baeldung.jackson.inheritance;
|
||||
|
||||
public class IgnoranceMixinOrIntrospection {
|
||||
public static abstract class Vehicle {
|
||||
private String make;
|
||||
private String model;
|
||||
|
||||
protected Vehicle() {
|
||||
}
|
||||
|
||||
protected Vehicle(String make, String model) {
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public void setMake(String make) {
|
||||
this.make = make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class Car extends Vehicle {
|
||||
private int seatingCapacity;
|
||||
private double topSpeed;
|
||||
|
||||
protected Car() {
|
||||
}
|
||||
|
||||
protected Car(String make, String model, int seatingCapacity, double topSpeed) {
|
||||
super(make, model);
|
||||
this.seatingCapacity = seatingCapacity;
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
|
||||
public int getSeatingCapacity() {
|
||||
return seatingCapacity;
|
||||
}
|
||||
|
||||
public void setSeatingCapacity(int seatingCapacity) {
|
||||
this.seatingCapacity = seatingCapacity;
|
||||
}
|
||||
|
||||
public double getTopSpeed() {
|
||||
return topSpeed;
|
||||
}
|
||||
|
||||
public void setTopSpeed(double topSpeed) {
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Sedan extends Car {
|
||||
public Sedan() {
|
||||
}
|
||||
|
||||
public Sedan(String make, String model, int seatingCapacity, double topSpeed) {
|
||||
super(make, model, seatingCapacity, topSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Crossover extends Car {
|
||||
private double towingCapacity;
|
||||
|
||||
public Crossover() {
|
||||
}
|
||||
|
||||
public Crossover(String make, String model, int seatingCapacity, double topSpeed, double towingCapacity) {
|
||||
super(make, model, seatingCapacity, topSpeed);
|
||||
this.towingCapacity = towingCapacity;
|
||||
}
|
||||
|
||||
public double getTowingCapacity() {
|
||||
return towingCapacity;
|
||||
}
|
||||
|
||||
public void setTowingCapacity(double towingCapacity) {
|
||||
this.towingCapacity = towingCapacity;
|
||||
}
|
||||
}
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.jackson.inheritance;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
|
||||
@JsonTypeName("itemIdAddedToUser")
|
||||
@JsonIgnoreProperties("id")
|
||||
public class ItemIdAddedToUser extends Event {
|
||||
private final String itemId;
|
||||
private final Long quantity;
|
||||
|
||||
@JsonCreator
|
||||
public ItemIdAddedToUser(@JsonProperty("id") String id, @JsonProperty("timestamp") Long timestamp, @JsonProperty("itemId") String itemId, @JsonProperty("quantity") Long quantity) {
|
||||
super(id, timestamp);
|
||||
this.itemId = itemId;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public String getItemId() {
|
||||
return itemId;
|
||||
}
|
||||
|
||||
public Long getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.jackson.inheritance;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
|
||||
@JsonTypeName("itemIdRemovedFromUser")
|
||||
public class ItemIdRemovedFromUser extends Event {
|
||||
private final String itemId;
|
||||
private final Long quantity;
|
||||
|
||||
@JsonCreator
|
||||
public ItemIdRemovedFromUser(@JsonProperty("id") String id, @JsonProperty("timestamp") Long timestamp, @JsonProperty("itemId") String itemId, @JsonProperty("quantity") Long quantity) {
|
||||
super(id, timestamp);
|
||||
this.itemId = itemId;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public String getItemId() {
|
||||
return itemId;
|
||||
}
|
||||
|
||||
public Long getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
}
|
||||
-92
@@ -1,92 +0,0 @@
|
||||
package com.baeldung.jackson.inheritance;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class SubTypeConstructorStructure {
|
||||
public static class Fleet {
|
||||
private List<Vehicle> vehicles;
|
||||
|
||||
public List<Vehicle> getVehicles() {
|
||||
return vehicles;
|
||||
}
|
||||
|
||||
public void setVehicles(List<Vehicle> vehicles) {
|
||||
this.vehicles = vehicles;
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class Vehicle {
|
||||
private String make;
|
||||
private String model;
|
||||
|
||||
protected Vehicle(String make, String model) {
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public void setMake(String make) {
|
||||
this.make = make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Car extends Vehicle {
|
||||
private int seatingCapacity;
|
||||
private double topSpeed;
|
||||
|
||||
@JsonCreator
|
||||
public Car(@JsonProperty("make") String make, @JsonProperty("model") String model, @JsonProperty("seating") int seatingCapacity, @JsonProperty("topSpeed") double topSpeed) {
|
||||
super(make, model);
|
||||
this.seatingCapacity = seatingCapacity;
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
|
||||
public int getSeatingCapacity() {
|
||||
return seatingCapacity;
|
||||
}
|
||||
|
||||
public void setSeatingCapacity(int seatingCapacity) {
|
||||
this.seatingCapacity = seatingCapacity;
|
||||
}
|
||||
|
||||
public double getTopSpeed() {
|
||||
return topSpeed;
|
||||
}
|
||||
|
||||
public void setTopSpeed(double topSpeed) {
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Truck extends Vehicle {
|
||||
private double payloadCapacity;
|
||||
|
||||
@JsonCreator
|
||||
public Truck(@JsonProperty("make") String make, @JsonProperty("model") String model, @JsonProperty("payload") double payloadCapacity) {
|
||||
super(make, model);
|
||||
this.payloadCapacity = payloadCapacity;
|
||||
}
|
||||
|
||||
public double getPayloadCapacity() {
|
||||
return payloadCapacity;
|
||||
}
|
||||
|
||||
public void setPayloadCapacity(double payloadCapacity) {
|
||||
this.payloadCapacity = payloadCapacity;
|
||||
}
|
||||
}
|
||||
}
|
||||
-87
@@ -1,87 +0,0 @@
|
||||
package com.baeldung.jackson.inheritance;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
public class SubTypeConversionStructure {
|
||||
public static abstract class Vehicle {
|
||||
private String make;
|
||||
private String model;
|
||||
|
||||
protected Vehicle() {
|
||||
}
|
||||
|
||||
protected Vehicle(String make, String model) {
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public void setMake(String make) {
|
||||
this.make = make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Car extends Vehicle {
|
||||
@JsonIgnore
|
||||
private int seatingCapacity;
|
||||
@JsonIgnore
|
||||
private double topSpeed;
|
||||
|
||||
public Car() {
|
||||
}
|
||||
|
||||
public Car(String make, String model, int seatingCapacity, double topSpeed) {
|
||||
super(make, model);
|
||||
this.seatingCapacity = seatingCapacity;
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
|
||||
public int getSeatingCapacity() {
|
||||
return seatingCapacity;
|
||||
}
|
||||
|
||||
public void setSeatingCapacity(int seatingCapacity) {
|
||||
this.seatingCapacity = seatingCapacity;
|
||||
}
|
||||
|
||||
public double getTopSpeed() {
|
||||
return topSpeed;
|
||||
}
|
||||
|
||||
public void setTopSpeed(double topSpeed) {
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Truck extends Vehicle {
|
||||
@JsonIgnore
|
||||
private double payloadCapacity;
|
||||
|
||||
public Truck() {
|
||||
}
|
||||
|
||||
public Truck(String make, String model, double payloadCapacity) {
|
||||
super(make, model);
|
||||
this.payloadCapacity = payloadCapacity;
|
||||
}
|
||||
|
||||
public double getPayloadCapacity() {
|
||||
return payloadCapacity;
|
||||
}
|
||||
|
||||
public void setPayloadCapacity(double payloadCapacity) {
|
||||
this.payloadCapacity = payloadCapacity;
|
||||
}
|
||||
}
|
||||
}
|
||||
-102
@@ -1,102 +0,0 @@
|
||||
package com.baeldung.jackson.inheritance;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
|
||||
|
||||
public class TypeInfoAnnotatedStructure {
|
||||
public static class Fleet {
|
||||
private List<Vehicle> vehicles;
|
||||
|
||||
public List<Vehicle> getVehicles() {
|
||||
return vehicles;
|
||||
}
|
||||
|
||||
public void setVehicles(List<Vehicle> vehicles) {
|
||||
this.vehicles = vehicles;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
|
||||
@JsonSubTypes({ @Type(value = Car.class, name = "car"), @Type(value = Truck.class, name = "truck") })
|
||||
public static abstract class Vehicle {
|
||||
private String make;
|
||||
private String model;
|
||||
|
||||
protected Vehicle() {
|
||||
}
|
||||
|
||||
protected Vehicle(String make, String model) {
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public void setMake(String make) {
|
||||
this.make = make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Car extends Vehicle {
|
||||
private int seatingCapacity;
|
||||
private double topSpeed;
|
||||
|
||||
public Car() {
|
||||
}
|
||||
|
||||
public Car(String make, String model, int seatingCapacity, double topSpeed) {
|
||||
super(make, model);
|
||||
this.seatingCapacity = seatingCapacity;
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
|
||||
public int getSeatingCapacity() {
|
||||
return seatingCapacity;
|
||||
}
|
||||
|
||||
public void setSeatingCapacity(int seatingCapacity) {
|
||||
this.seatingCapacity = seatingCapacity;
|
||||
}
|
||||
|
||||
public double getTopSpeed() {
|
||||
return topSpeed;
|
||||
}
|
||||
|
||||
public void setTopSpeed(double topSpeed) {
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Truck extends Vehicle {
|
||||
private double payloadCapacity;
|
||||
|
||||
public Truck() {
|
||||
}
|
||||
|
||||
public Truck(String make, String model, double payloadCapacity) {
|
||||
super(make, model);
|
||||
this.payloadCapacity = payloadCapacity;
|
||||
}
|
||||
|
||||
public double getPayloadCapacity() {
|
||||
return payloadCapacity;
|
||||
}
|
||||
|
||||
public void setPayloadCapacity(double payloadCapacity) {
|
||||
this.payloadCapacity = payloadCapacity;
|
||||
}
|
||||
}
|
||||
}
|
||||
-96
@@ -1,96 +0,0 @@
|
||||
package com.baeldung.jackson.inheritance;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TypeInfoStructure {
|
||||
public static class Fleet {
|
||||
private List<Vehicle> vehicles;
|
||||
|
||||
public List<Vehicle> getVehicles() {
|
||||
return vehicles;
|
||||
}
|
||||
|
||||
public void setVehicles(List<Vehicle> vehicles) {
|
||||
this.vehicles = vehicles;
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class Vehicle {
|
||||
private String make;
|
||||
private String model;
|
||||
|
||||
protected Vehicle() {
|
||||
}
|
||||
|
||||
protected Vehicle(String make, String model) {
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public void setMake(String make) {
|
||||
this.make = make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Car extends Vehicle {
|
||||
private int seatingCapacity;
|
||||
private double topSpeed;
|
||||
|
||||
public Car() {
|
||||
}
|
||||
|
||||
public Car(String make, String model, int seatingCapacity, double topSpeed) {
|
||||
super(make, model);
|
||||
this.seatingCapacity = seatingCapacity;
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
|
||||
public int getSeatingCapacity() {
|
||||
return seatingCapacity;
|
||||
}
|
||||
|
||||
public void setSeatingCapacity(int seatingCapacity) {
|
||||
this.seatingCapacity = seatingCapacity;
|
||||
}
|
||||
|
||||
public double getTopSpeed() {
|
||||
return topSpeed;
|
||||
}
|
||||
|
||||
public void setTopSpeed(double topSpeed) {
|
||||
this.topSpeed = topSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Truck extends Vehicle {
|
||||
private double payloadCapacity;
|
||||
|
||||
public Truck() {
|
||||
}
|
||||
|
||||
public Truck(String make, String model, double payloadCapacity) {
|
||||
super(make, model);
|
||||
this.payloadCapacity = payloadCapacity;
|
||||
}
|
||||
|
||||
public double getPayloadCapacity() {
|
||||
return payloadCapacity;
|
||||
}
|
||||
|
||||
public void setPayloadCapacity(double payloadCapacity) {
|
||||
this.payloadCapacity = payloadCapacity;
|
||||
}
|
||||
}
|
||||
}
|
||||
-61
@@ -1,61 +0,0 @@
|
||||
package com.baeldung.jackson.jacksonvsgson;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class ActorJackson {
|
||||
|
||||
private String imdbId;
|
||||
private Date dateOfBirth;
|
||||
private List<String> filmography;
|
||||
|
||||
public ActorJackson() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ActorJackson(String imdbId, Date dateOfBirth, List<String> filmography) {
|
||||
super();
|
||||
this.imdbId = imdbId;
|
||||
this.dateOfBirth = dateOfBirth;
|
||||
this.filmography = filmography;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ActorJackson [imdbId=" + imdbId + ", dateOfBirth=" + formatDateOfBirth() + ", filmography=" + filmography + "]";
|
||||
}
|
||||
|
||||
public String getImdbId() {
|
||||
return imdbId;
|
||||
}
|
||||
|
||||
public void setImdbId(String imdbId) {
|
||||
this.imdbId = imdbId;
|
||||
}
|
||||
|
||||
public Date getDateOfBirth() {
|
||||
return dateOfBirth;
|
||||
}
|
||||
|
||||
public void setDateOfBirth(Date dateOfBirth) {
|
||||
this.dateOfBirth = dateOfBirth;
|
||||
}
|
||||
|
||||
public List<String> getFilmography() {
|
||||
return filmography;
|
||||
}
|
||||
|
||||
public void setFilmography(List<String> filmography) {
|
||||
this.filmography = filmography;
|
||||
}
|
||||
|
||||
private String formatDateOfBirth() {
|
||||
final DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy", Locale.US);
|
||||
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
return formatter.format(dateOfBirth);
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.jackson.jacksonvsgson;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class ActorJacksonSerializer extends StdSerializer<ActorJackson> {
|
||||
|
||||
private SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
|
||||
|
||||
public ActorJacksonSerializer(Class t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(ActorJackson actor, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||
|
||||
jsonGenerator.writeStartObject();
|
||||
jsonGenerator.writeStringField("imdbId", actor.getImdbId());
|
||||
jsonGenerator.writeObjectField("dateOfBirth", actor.getDateOfBirth() != null ? sdf.format(actor.getDateOfBirth()) : null);
|
||||
jsonGenerator.writeNumberField("N° Film: ", actor.getFilmography() != null ? actor.getFilmography()
|
||||
.size() : null);
|
||||
jsonGenerator.writeStringField("filmography", actor.getFilmography()
|
||||
.stream()
|
||||
.collect(Collectors.joining("-")));
|
||||
jsonGenerator.writeEndObject();
|
||||
}
|
||||
}
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
package com.baeldung.jackson.jacksonvsgson;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Movie {
|
||||
|
||||
private String imdbId;
|
||||
private String director;
|
||||
private List<ActorJackson> actors;
|
||||
|
||||
public Movie(String imdbId, String director, List<ActorJackson> actors) {
|
||||
super();
|
||||
this.imdbId = imdbId;
|
||||
this.director = director;
|
||||
this.actors = actors;
|
||||
}
|
||||
|
||||
public Movie() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Movie [imdbId=" + imdbId + ", director=" + director + ", actors=" + actors + "]";
|
||||
}
|
||||
|
||||
public String getImdbId() {
|
||||
return imdbId;
|
||||
}
|
||||
|
||||
public void setImdbId(String imdbId) {
|
||||
this.imdbId = imdbId;
|
||||
}
|
||||
|
||||
public String getDirector() {
|
||||
return director;
|
||||
}
|
||||
|
||||
public void setDirector(String director) {
|
||||
this.director = director;
|
||||
}
|
||||
|
||||
public List<ActorJackson> getActors() {
|
||||
return actors;
|
||||
}
|
||||
|
||||
public void setActors(List<ActorJackson> actors) {
|
||||
this.actors = actors;
|
||||
}
|
||||
}
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.jackson.jacksonvsgson;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MovieWithNullValue {
|
||||
|
||||
private String imdbId;
|
||||
|
||||
@JsonIgnore
|
||||
private String director;
|
||||
|
||||
private List<ActorJackson> actors;
|
||||
|
||||
public MovieWithNullValue(String imdbID, String director, List<ActorJackson> actors) {
|
||||
super();
|
||||
this.imdbId = imdbID;
|
||||
this.director = director;
|
||||
this.actors = actors;
|
||||
}
|
||||
|
||||
public String getImdbID() {
|
||||
return imdbId;
|
||||
}
|
||||
|
||||
public void setImdbID(String imdbID) {
|
||||
this.imdbId = imdbID;
|
||||
}
|
||||
|
||||
public String getDirector() {
|
||||
return director;
|
||||
}
|
||||
|
||||
public void setDirector(String director) {
|
||||
this.director = director;
|
||||
}
|
||||
|
||||
public List<ActorJackson> getActors() {
|
||||
return actors;
|
||||
}
|
||||
|
||||
public void setActors(List<ActorJackson> actors) {
|
||||
this.actors = actors;
|
||||
}
|
||||
}
|
||||
-62
@@ -1,62 +0,0 @@
|
||||
package com.baeldung.jackson.node;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
public class JsonNodeIterator {
|
||||
|
||||
private static final String NEW_LINE = "\n";
|
||||
private static final String FIELD_DELIMITER = ": ";
|
||||
private static final String ARRAY_PREFIX = "- ";
|
||||
private static final String YAML_PREFIX = " ";
|
||||
|
||||
public String toYaml(JsonNode root) {
|
||||
StringBuilder yaml = new StringBuilder();
|
||||
processNode(root, yaml, 0);
|
||||
return yaml.toString();
|
||||
}
|
||||
|
||||
private void processNode(JsonNode jsonNode, StringBuilder yaml, int depth) {
|
||||
if (jsonNode.isValueNode()) {
|
||||
yaml.append(jsonNode.asText());
|
||||
}
|
||||
else if (jsonNode.isArray()) {
|
||||
for (JsonNode arrayItem : jsonNode) {
|
||||
appendNodeToYaml(arrayItem, yaml, depth, true);
|
||||
}
|
||||
}
|
||||
else if (jsonNode.isObject()) {
|
||||
appendNodeToYaml(jsonNode, yaml, depth, false);
|
||||
}
|
||||
}
|
||||
|
||||
private void appendNodeToYaml(JsonNode node, StringBuilder yaml, int depth, boolean isArrayItem) {
|
||||
Iterator<Entry<String, JsonNode>> fields = node.fields();
|
||||
boolean isFirst = true;
|
||||
while (fields.hasNext()) {
|
||||
Entry<String, JsonNode> jsonField = fields.next();
|
||||
addFieldNameToYaml(yaml, jsonField.getKey(), depth, isArrayItem && isFirst);
|
||||
processNode(jsonField.getValue(), yaml, depth+1);
|
||||
isFirst = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void addFieldNameToYaml(StringBuilder yaml, String fieldName, int depth, boolean isFirstInArray) {
|
||||
if (yaml.length()>0) {
|
||||
yaml.append(NEW_LINE);
|
||||
int requiredDepth = (isFirstInArray) ? depth-1 : depth;
|
||||
for(int i = 0; i < requiredDepth; i++) {
|
||||
yaml.append(YAML_PREFIX);
|
||||
}
|
||||
if (isFirstInArray) {
|
||||
yaml.append(ARRAY_PREFIX);
|
||||
}
|
||||
}
|
||||
yaml.append(fieldName);
|
||||
yaml.append(FIELD_DELIMITER);
|
||||
}
|
||||
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.jackson.optionalwithjackson;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Book {
|
||||
|
||||
private String title;
|
||||
private Optional<String> subTitle;
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Optional<String> getSubTitle() {
|
||||
return subTitle;
|
||||
}
|
||||
|
||||
public void setSubTitle(Optional<String> subTitle) {
|
||||
this.subTitle = subTitle;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
{
|
||||
"collection": [
|
||||
{
|
||||
"name": "Test order1",
|
||||
"detail": "ahk ks"
|
||||
},
|
||||
{
|
||||
"name": "Test order2",
|
||||
"detail": "Fisteku"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
[
|
||||
{
|
||||
"name": "Test order1",
|
||||
"detail": "ahk ks"
|
||||
},
|
||||
{
|
||||
"name": "Test order2",
|
||||
"detail": "Fisteku"
|
||||
}
|
||||
]
|
||||
@@ -1,19 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.baeldung.jackson.dtos;
|
||||
|
||||
public class Address {
|
||||
|
||||
String streetNumber;
|
||||
String streetName;
|
||||
String city;
|
||||
|
||||
public String getStreetNumber() {
|
||||
return streetNumber;
|
||||
}
|
||||
|
||||
public void setStreetNumber(String streetNumber) {
|
||||
this.streetNumber = streetNumber;
|
||||
}
|
||||
|
||||
public String getStreetName() {
|
||||
return streetName;
|
||||
}
|
||||
|
||||
public void setStreetName(String streetName) {
|
||||
this.streetName = streetName;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package com.baeldung.jackson.dtos;
|
||||
|
||||
public class MyDto {
|
||||
|
||||
private String stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
|
||||
public MyDto() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyDto(final String stringValue, final int intValue, final boolean booleanValue) {
|
||||
super();
|
||||
|
||||
this.stringValue = stringValue;
|
||||
this.intValue = intValue;
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.baeldung.jackson.dtos;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
||||
|
||||
@JacksonXmlRootElement(localName = "person")
|
||||
public final class Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private List<String> phoneNumbers = new ArrayList<>();
|
||||
private List<Address> address = new ArrayList<>();
|
||||
|
||||
public List<Address> getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(List<Address> address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public List<String> getPhoneNumbers() {
|
||||
return phoneNumbers;
|
||||
}
|
||||
|
||||
public void setPhoneNumbers(List<String> phoneNumbers) {
|
||||
this.phoneNumbers = phoneNumbers;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.jackson.dtos;
|
||||
|
||||
public class User {
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
-92
@@ -1,92 +0,0 @@
|
||||
package com.baeldung.jackson.inheritance;
|
||||
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
|
||||
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
|
||||
|
||||
public class IgnoranceUnitTest {
|
||||
private static abstract class CarMixIn {
|
||||
@JsonIgnore
|
||||
public String make;
|
||||
@JsonIgnore
|
||||
public String topSpeed;
|
||||
}
|
||||
|
||||
private static class IgnoranceIntrospector extends JacksonAnnotationIntrospector {
|
||||
private static final long serialVersionUID = 1422295680188892323L;
|
||||
|
||||
public boolean hasIgnoreMarker(AnnotatedMember m) {
|
||||
return m.getDeclaringClass() == IgnoranceMixinOrIntrospection.Vehicle.class && m.getName() == "model" || m.getDeclaringClass() == IgnoranceMixinOrIntrospection.Car.class || m.getName() == "towingCapacity" || super.hasIgnoreMarker(m);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnnotations_whenIgnoringProperties_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
IgnoranceAnnotationStructure.Sedan sedan = new IgnoranceAnnotationStructure.Sedan("Mercedes-Benz", "S500", 5, 250.0);
|
||||
IgnoranceAnnotationStructure.Crossover crossover = new IgnoranceAnnotationStructure.Crossover("BMW", "X6", 5, 250.0, 6000.0);
|
||||
|
||||
List<IgnoranceAnnotationStructure.Vehicle> vehicles = new ArrayList<>();
|
||||
vehicles.add(sedan);
|
||||
vehicles.add(crossover);
|
||||
|
||||
String jsonDataString = mapper.writeValueAsString(vehicles);
|
||||
|
||||
assertThat(jsonDataString, containsString("make"));
|
||||
assertThat(jsonDataString, not(containsString("model")));
|
||||
assertThat(jsonDataString, not(containsString("seatingCapacity")));
|
||||
assertThat(jsonDataString, not(containsString("topSpeed")));
|
||||
assertThat(jsonDataString, containsString("towingCapacity"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMixIns_whenIgnoringProperties_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.addMixIn(IgnoranceMixinOrIntrospection.Car.class, CarMixIn.class);
|
||||
|
||||
String jsonDataString = instantiateAndSerializeObjects(mapper);
|
||||
|
||||
assertThat(jsonDataString, not(containsString("make")));
|
||||
assertThat(jsonDataString, containsString("model"));
|
||||
assertThat(jsonDataString, containsString("seatingCapacity"));
|
||||
assertThat(jsonDataString, not(containsString("topSpeed")));
|
||||
assertThat(jsonDataString, containsString("towingCapacity"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntrospection_whenIgnoringProperties_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setAnnotationIntrospector(new IgnoranceIntrospector());
|
||||
|
||||
String jsonDataString = instantiateAndSerializeObjects(mapper);
|
||||
|
||||
assertThat(jsonDataString, containsString("make"));
|
||||
assertThat(jsonDataString, not(containsString("model")));
|
||||
assertThat(jsonDataString, not(containsString("seatingCapacity")));
|
||||
assertThat(jsonDataString, not(containsString("topSpeed")));
|
||||
assertThat(jsonDataString, not(containsString("towingCapacity")));
|
||||
}
|
||||
|
||||
private String instantiateAndSerializeObjects(ObjectMapper mapper) throws JsonProcessingException {
|
||||
IgnoranceMixinOrIntrospection.Sedan sedan = new IgnoranceMixinOrIntrospection.Sedan("Mercedes-Benz", "S500", 5, 250.0);
|
||||
IgnoranceMixinOrIntrospection.Crossover crossover = new IgnoranceMixinOrIntrospection.Crossover("BMW", "X6", 5, 250.0, 6000.0);
|
||||
|
||||
List<IgnoranceMixinOrIntrospection.Vehicle> vehicles = new ArrayList<>();
|
||||
vehicles.add(sedan);
|
||||
vehicles.add(crossover);
|
||||
|
||||
return mapper.writeValueAsString(vehicles);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user