[BAEL-12185] - Removed copied classes from other repositor

This commit is contained in:
amit2103 2019-03-09 20:08:17 +05:30
parent c69690d22c
commit 3d5e52cde0
62 changed files with 40 additions and 2577 deletions

View File

@ -1,26 +0,0 @@
package com.baeldung.jackson.deserialization.jacksoninject;
import com.baeldung.jackson.domain.Item;
import java.util.ArrayList;
import java.util.List;
public class Author extends Person {
List<Item> items = new ArrayList<>();
public Author() {
}
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,24 +1,14 @@
package com.baeldung.jackson.deserialization.jsonanysetter;
import com.baeldung.jackson.domain.Author;
import com.baeldung.jackson.domain.Item;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnySetter;
public class Inventory {
private Map<Author, Item> stock = new HashMap<>();
private Map<String, Float> countryDeliveryCost = new HashMap<>();
@JsonIgnore
public Map<Author, Item> getStock() {
return stock;
}
public Map<String, Float> getCountryDeliveryCost() {
return countryDeliveryCost;
}

View File

@ -1,27 +0,0 @@
package com.baeldung.jackson.deserialization.jsoncreator;
import com.baeldung.jackson.domain.Person;
import com.baeldung.jackson.domain.Item;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
public class Author extends Person {
List<Item> items = new ArrayList<>();
@JsonCreator
public Author(@JsonProperty("christianName") String firstName, @JsonProperty("surname") String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,24 +0,0 @@
package com.baeldung.jackson.deserialization.jsondeserialize;
import com.baeldung.jackson.domain.Item;
import com.baeldung.jackson.domain.Person;
import java.util.ArrayList;
import java.util.List;
public class Author extends Person {
List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,12 +1,16 @@
package com.baeldung.jackson.deserialization.jsondeserialize;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;
public class Book extends Item {
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)
@ -16,8 +20,9 @@ public class Book extends Item {
public Book() {
}
public Book(String title, Author author) {
super(title, author);
public Book(String title) {
this.id = UUID.randomUUID();
this.title = title;
}
public String getISBN() {
@ -43,4 +48,28 @@ public class Book extends Item {
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;
}
}

View File

@ -1,62 +0,0 @@
package com.baeldung.jackson.deserialization.jsondeserialize;
import com.baeldung.jackson.domain.Person;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Item {
private UUID id;
private String title;
private List<Person> authors = new ArrayList<>();
private float price;
public Item() {
}
public Item(String title, Author author) {
this.id = UUID.randomUUID();
this.title = title;
this.authors.add(author);
}
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 List<Person> getAuthors() {
return authors;
}
public void setAuthors(List<Person> authors) {
this.authors = authors;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}

View File

@ -1,37 +0,0 @@
package com.baeldung.jackson.deserialization.jsonsetter;
import com.baeldung.jackson.domain.Item;
import com.baeldung.jackson.domain.Person;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSetter;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Author extends Person {
private List<Item> items = new ArrayList<>();
public Author() {
}
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
@JsonIgnore
public List<Item> getItems() {
return items;
}
@JsonSetter("publications")
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,27 +0,0 @@
package com.baeldung.jackson.domain;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Author extends Person {
private List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,48 +0,0 @@
package com.baeldung.jackson.domain;
import java.math.BigDecimal;
import java.util.Date;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Book extends Item {
private String ISBN;
private Date published;
private BigDecimal pages;
public Book() {
}
public Book(String title, Author author) {
super(title, author);
}
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;
}
}

View File

@ -1,73 +0,0 @@
package com.baeldung.jackson.domain;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Course extends Item {
public enum Medium {
CLASSROOM, ONLINE
}
public enum Level {
BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3);
private String name;
private int number;
Level(String name, int number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
}
private float duration;
private Medium medium;
private Level level;
private List<Course> prerequisite;
public Course(String title, Author author) {
super(title, author);
}
public float getDuration() {
return duration;
}
public void setDuration(float duration) {
this.duration = duration;
}
public Medium getMedium() {
return medium;
}
public void setMedium(Medium medium) {
this.medium = medium;
}
public Level getLevel() {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
public List<Course> getPrerequisite() {
return prerequisite;
}
public void setPrerequisite(List<Course> prerequisite) {
this.prerequisite = prerequisite;
}
}

View File

@ -1,24 +0,0 @@
package com.baeldung.jackson.domain;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Customer extends Person {
private String configuration;
public Customer(String firstName, String lastName) {
super(firstName, lastName);
}
public String getConfiguration() {
return configuration;
}
public void setConfiguration(String configuration) {
this.configuration = configuration;
}
}

View File

@ -1,29 +0,0 @@
package com.baeldung.jackson.domain;
import java.util.HashMap;
import java.util.Map;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Inventory {
private Map<Author, Item> stock;
private Map<String, Float> countryDeliveryCost = new HashMap<>();
public Map<Author, Item> getStock() {
return stock;
}
public void setStock(Map<Author, Item> stock) {
this.stock = stock;
}
public Map<String, Float> getCountryDeliveryCost() {
return countryDeliveryCost;
}
}

View File

@ -1,60 +0,0 @@
package com.baeldung.jackson.domain;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Item {
private UUID id;
private String title;
private List<Person> authors = new ArrayList<>();
private float price;
public Item() {
}
public Item(String title, Author author) {
this.id = UUID.randomUUID();
this.title = title;
this.authors.add(author);
}
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 List<Person> getAuthors() {
return authors;
}
public void setAuthors(List<Person> authors) {
this.authors = authors;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}

View File

@ -1,50 +0,0 @@
package com.baeldung.jackson.domain;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Order {
private UUID id;
private Type type;
private int internalAudit;
public static class Type {
public long id;
public String name;
}
public Order() {
this.id = UUID.randomUUID();
}
public Order(Type type) {
this();
this.type = type;
}
public Order(int internalAudit) {
this();
this.type = new Type();
this.type.id = 20;
this.type.name = "Order";
this.internalAudit = internalAudit;
}
public UUID getId() {
return id;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
}

View File

@ -1,24 +1,12 @@
package com.baeldung.jackson.domain;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Person {
private UUID id;
private String firstName;
private String lastName;
public Person() {
}
public Person(String firstName, String lastName) {
this.id = UUID.randomUUID();
super();
this.firstName = firstName;
this.lastName = lastName;
}
@ -38,8 +26,5 @@ public class Person {
public void setLastName(String lastName) {
this.lastName = lastName;
}
public UUID getId() {
return id;
}
}

View File

@ -1,29 +0,0 @@
package com.baeldung.jackson.inclusion.jsonignore;
import com.baeldung.jackson.domain.Item;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Author extends Person {
private List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,48 +0,0 @@
package com.baeldung.jackson.inclusion.jsonignore;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Person {
@JsonIgnore
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;
}
}

View File

@ -1,78 +0,0 @@
package com.baeldung.jackson.inclusion.jsonignoreproperties;
import com.baeldung.jackson.domain.Author;
import com.baeldung.jackson.domain.Item;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@JsonIgnoreProperties({ "medium" })
public class Course extends Item {
public enum Medium {
CLASSROOM, ONLINE
}
public enum Level {
BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3);
private String name;
private int number;
Level(String name, int number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
}
private float duration;
private Medium medium;
private Level level;
private List<Course> prerequisite;
public Course(String title, Author author) {
super(title, author);
}
public float getDuration() {
return duration;
}
public void setDuration(float duration) {
this.duration = duration;
}
public Medium getMedium() {
return medium;
}
public void setMedium(Medium medium) {
this.medium = medium;
}
public Level getLevel() {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
public List<Course> getPrerequisite() {
return prerequisite;
}
public void setPrerequisite(List<Course> prerequisite) {
this.prerequisite = prerequisite;
}
}

View File

@ -1,34 +0,0 @@
package com.baeldung.jackson.inclusion.jsoninclude;
import com.baeldung.jackson.domain.Person;
import com.baeldung.jackson.domain.Item;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.ArrayList;
import java.util.List;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@JsonInclude(NON_NULL)
public class Author extends Person {
private List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,76 +0,0 @@
package com.baeldung.jackson.miscellaneous.custom;
import com.baeldung.jackson.domain.Author;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@CustomCourseAnnotation
public class Course extends Item {
public enum Medium {
CLASSROOM, ONLINE
}
public enum Level {
BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3);
private String name;
private int number;
Level(String name, int number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
}
private float duration;
private Medium medium;
private Level level;
private List<Course> prerequisite;
public Course(String title, Author author) {
super(title, author);
}
public float getDuration() {
return duration;
}
public void setDuration(float duration) {
this.duration = duration;
}
public Medium getMedium() {
return medium;
}
public void setMedium(Medium medium) {
this.medium = medium;
}
public Level getLevel() {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
public List<Course> getPrerequisite() {
return prerequisite;
}
public void setPrerequisite(List<Course> prerequisite) {
this.prerequisite = prerequisite;
}
}

View File

@ -1,20 +0,0 @@
package com.baeldung.jackson.miscellaneous.custom;
import com.fasterxml.jackson.annotation.*;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "title", "price", "id", "duration", "authors", "level" })
@JsonIgnoreProperties({ "prerequisite" })
public @interface CustomCourseAnnotation {
}

View File

@ -1,63 +0,0 @@
package com.baeldung.jackson.miscellaneous.custom;
import com.baeldung.jackson.domain.Author;
import com.baeldung.jackson.domain.Person;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Item {
private UUID id;
private String title;
private List<Person> authors = new ArrayList<>();
private float price;
public Item() {
}
public Item(String title, Author author) {
this.id = UUID.randomUUID();
this.title = title;
this.authors.add(author);
}
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 List<Person> getAuthors() {
return authors;
}
public void setAuthors(List<Person> authors) {
this.authors = authors;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}

View File

@ -1,36 +0,0 @@
package com.baeldung.jackson.miscellaneous.disable;
import com.baeldung.jackson.domain.Item;
import com.baeldung.jackson.domain.Person;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "lastName", "items", "firstName", "id" })
public class Author extends Person {
@JsonIgnore
private List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,30 +0,0 @@
package com.baeldung.jackson.miscellaneous.mixin;
import com.baeldung.jackson.domain.Item;
import com.baeldung.jackson.domain.Person;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Author extends Person {
private List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,13 +0,0 @@
package com.baeldung.jackson.miscellaneous.mixin;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@JsonIgnoreType
public class IgnoreListMixIn {
}

View File

@ -1,43 +0,0 @@
package com.baeldung.jackson.serialization.jsonanygetter;
import com.baeldung.jackson.domain.Author;
import com.baeldung.jackson.domain.Item;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.HashMap;
import java.util.Map;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Inventory {
private String location;
private Map<Author, Item> stock = new HashMap<>();
private Map<String, Float> countryDeliveryCost = new HashMap<>();
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@JsonIgnore
public Map<Author, Item> getStock() {
return stock;
}
@JsonAnyGetter
public Map<String, Float> getCountryDeliveryCost() {
return countryDeliveryCost;
}
}

View File

@ -1,32 +0,0 @@
package com.baeldung.jackson.serialization.jsongetter;
import com.baeldung.jackson.domain.Item;
import com.baeldung.jackson.domain.Person;
import com.fasterxml.jackson.annotation.JsonGetter;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Author extends Person {
List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
@JsonGetter("publications")
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,31 +0,0 @@
package com.baeldung.jackson.serialization.jsonpropertyorder;
import com.baeldung.jackson.domain.Item;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@JsonPropertyOrder({ "items", "firstName", "lastName", "id" })
public class Author extends Person {
List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,42 +0,0 @@
package com.baeldung.jackson.serialization.jsonpropertyorder;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Person {
private UUID id;
private String firstName;
private String lastName;
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;
}
}

View File

@ -1,28 +0,0 @@
package com.baeldung.jackson.serialization.jsonrawvalue;
import com.baeldung.jackson.domain.Person;
import com.fasterxml.jackson.annotation.JsonRawValue;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Customer extends Person {
@JsonRawValue
private String configuration;
public Customer(String firstName, String lastName) {
super(firstName, lastName);
}
public String getConfiguration() {
return configuration;
}
public void setConfiguration(String configuration) {
this.configuration = configuration;
}
}

View File

@ -1,32 +0,0 @@
package com.baeldung.jackson.serialization.jsonrootname;
import com.baeldung.jackson.domain.Item;
import com.baeldung.jackson.domain.Person;
import com.fasterxml.jackson.annotation.JsonRootName;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@JsonRootName(value = "writer", namespace = "book")
public class Author extends Person {
List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,29 +0,0 @@
package com.baeldung.jackson.serialization.jsonserialize;
import com.baeldung.jackson.domain.Person;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Author extends Person {
List<com.baeldung.jackson.domain.Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<com.baeldung.jackson.domain.Item> getItems() {
return items;
}
public void setItems(List<com.baeldung.jackson.domain.Item> items) {
this.items = items;
}
}

View File

@ -1,52 +0,0 @@
package com.baeldung.jackson.serialization.jsonserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.math.BigDecimal;
import java.util.Date;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Book extends Item {
private String ISBN;
@JsonSerialize(using = CustomDateSerializer.class)
private Date published;
private BigDecimal pages;
public Book() {
}
public Book(String title, Author author) {
super(title, author);
}
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;
}
}

View File

@ -1,34 +0,0 @@
package com.baeldung.jackson.serialization.jsonserialize;
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;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class CustomDateSerializer extends StdSerializer<Date> {
private static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
public CustomDateSerializer() {
this(null);
}
public CustomDateSerializer(Class<Date> t) {
super(t);
}
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider arg2) throws IOException, JsonProcessingException {
gen.writeString(formatter.format(value));
}
}

View File

@ -1,62 +0,0 @@
package com.baeldung.jackson.serialization.jsonserialize;
import com.baeldung.jackson.domain.Person;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Item {
private UUID id;
private String title;
private List<Person> authors = new ArrayList<>();
private float price;
public Item() {
}
public Item(String title, Author author) {
this.id = UUID.randomUUID();
this.title = title;
this.authors.add(author);
}
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 List<Person> getAuthors() {
return authors;
}
public void setAuthors(List<Person> authors) {
this.authors = authors;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}

View File

@ -1,78 +0,0 @@
package com.baeldung.jackson.serialization.jsonvalue;
import com.baeldung.jackson.domain.Author;
import com.baeldung.jackson.domain.Item;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Course extends Item {
public enum Medium {
CLASSROOM, ONLINE
}
public enum Level {
BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3);
private String name;
private int number;
Level(String name, int number) {
this.name = name;
this.number = number;
}
@JsonValue
public String getName() {
return name;
}
}
private float duration;
private Medium medium;
private Level level;
private List<Course> prerequisite;
public Course(String title, Author author) {
super(title, author);
}
public float getDuration() {
return duration;
}
public void setDuration(float duration) {
this.duration = duration;
}
public Medium getMedium() {
return medium;
}
public void setMedium(Medium medium) {
this.medium = medium;
}
public Level getLevel() {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
public List<Course> getPrerequisite() {
return prerequisite;
}
public void setPrerequisite(List<Course> prerequisite) {
this.prerequisite = prerequisite;
}
}

View File

@ -1,46 +0,0 @@
package com.baeldung.jackson.deserialization.jacksoninject;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.IOException;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JacksonInjectUnitTest {
@Test
public void whenDeserializingUsingJacksonInject_thenCorrect() throws IOException {
UUID id = UUID.fromString("9616dc8c-bad3-11e6-a4a6-cec0c932ce01");
// arrange
String authorJson = "{\"firstName\": \"Alex\", \"lastName\": \"Theedom\"}";
// act
InjectableValues inject = new InjectableValues.Std().addValue(UUID.class, id);
Author author = new ObjectMapper().reader(inject)
.forType(Author.class)
.readValue(authorJson);
// assert
assertThat(author.getId()).isEqualTo(id);
/*
{
"firstName": "Alex",
"lastName": "Theedom",
"publications": []
}
*/
}
}

View File

@ -1,41 +0,0 @@
package com.baeldung.jackson.deserialization.jsoncreator;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.IOException;
import static io.restassured.path.json.JsonPath.from;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonCreatorUnitTest {
@Test
public void whenDeserializingUsingJsonCreator_thenCorrect() throws IOException {
// arrange
String authorJson = "{" + " \"christianName\": \"Alex\"," + " \"surname\": \"Theedom\"" + "}";
// act
final Author author = new ObjectMapper().readerFor(Author.class)
.readValue(authorJson);
// assert
assertThat(from(authorJson).getString("christianName")).isEqualTo(author.getFirstName());
assertThat(from(authorJson).getString("surname")).isEqualTo(author.getLastName());
/*
{
"christianName": "Alex",
"surname": "Theedom"
}
*/
}
}

View File

@ -21,7 +21,7 @@ public class JsonDeserializeUnitTest {
public void whenDeserializingUsingJsonDeserialize_thenCorrect() throws IOException {
// arrange
String bookJson = "{\"id\":\"957c43f2-fa2e-42f9-bf75-6e3d5bb6960a\",\"title\":\"Effective Java\",\"authors\":[{\"id\":\"9bcd817d-0141-42e6-8f04-e5aaab0980b6\",\"firstName\":\"Joshua\",\"lastName\":\"Bloch\"}],\"price\":0,\"published\":\"25-12-2017 13:30:25\",\"pages\":null,\"isbn\":null}";
String bookJson = "{\"id\":\"957c43f2-fa2e-42f9-bf75-6e3d5bb6960a\",\"title\":\"Effective Java\",\"price\":0,\"published\":\"25-12-2017 13:30:25\",\"pages\":null,\"isbn\":null}";
// act
Book book = new ObjectMapper().readerFor(Book.class)

View File

@ -1,35 +0,0 @@
package com.baeldung.jackson.deserialization.jsonsetter;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.IOException;
import static io.restassured.path.json.JsonPath.from;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonSetterUnitTest {
@Test
public void whenDeserializingUsingJsonSetter_thenCorrect() throws IOException {
// arrange
String json = "{\"firstName\":\"Alex\",\"lastName\":\"Theedom\",\"publications\":[{\"title\":\"Professional Java EE Design Patterns\"}]}";
// act
Author author = new ObjectMapper().readerFor(Author.class)
.readValue(json);
// assert
assertThat(from(json).getList("publications")
.size()).isEqualTo(author.getItems()
.size());
}
}

View File

@ -1,32 +0,0 @@
package com.baeldung.jackson.general.jsonfilter;
import com.baeldung.jackson.domain.Person;
import com.baeldung.jackson.domain.Item;
import com.fasterxml.jackson.annotation.JsonFilter;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@JsonFilter("authorFilter")
public class Author extends Person {
private List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,42 +0,0 @@
package com.baeldung.jackson.general.jsonfilter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import org.junit.Test;
import static io.restassured.path.json.JsonPath.from;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonFilterUnitTest {
@Test
public void whenSerializingUsingJsonFilter_thenCorrect() throws JsonProcessingException {
// arrange
Author author = new Author("Alex", "Theedom");
FilterProvider filters = new SimpleFilterProvider().addFilter("authorFilter", SimpleBeanPropertyFilter.filterOutAllExcept("lastName"));
// act
String result = new ObjectMapper().writer(filters)
.writeValueAsString(author);
// assert
assertThat(from(result).getList("items")).isNull();
/*
{
"lastName": "Theedom"
}
*/
}
}

View File

@ -1,54 +0,0 @@
package com.baeldung.jackson.general.jsonformat;
import com.baeldung.jackson.domain.Author;
import com.baeldung.jackson.domain.Item;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.math.BigDecimal;
import java.util.Date;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Book extends Item {
private String ISBN;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss")
private Date published;
private BigDecimal pages;
public Book() {
}
public Book(String title, Author author) {
super(title, author);
}
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;
}
}

View File

@ -1,63 +0,0 @@
package com.baeldung.jackson.general.jsonformat;
import com.baeldung.jackson.domain.Author;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import static io.restassured.path.json.JsonPath.from;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonFormatUnitTest {
@Test
public void whenSerializingUsingJsonFormat_thenCorrect() throws JsonProcessingException, ParseException {
// arrange
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String toParse = "20-12-2014 14:30:00";
Date date = df.parse(toParse);
Book book = new Book("Design Patterns: Elements of Reusable Object-oriented Software", new Author("The", "GoF"));
book.setPublished(date);
// act
String result = new ObjectMapper().writeValueAsString(book);
// assert
assertThat(from(result).getString("published")).isEqualTo(toParse);
/*
{
"id": "762b39be-fd5b-489e-8688-aeb3b9bbf019",
"title": "Design Patterns: Elements of Reusable Object-oriented Software",
"authors": [
{
"id": "6941b780-0f54-4259-adcb-85523c8f25f4",
"firstName": "The",
"lastName": "GoF",
"items": []
}
],
"price": 0,
"published": "20-12-2014 02:30:00",
"pages": null,
"isbn": null
}
*/
}
}

View File

@ -1,29 +0,0 @@
package com.baeldung.jackson.general.jsonproperty;
import com.baeldung.jackson.domain.Person;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Author extends Person {
private List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -1,61 +0,0 @@
package com.baeldung.jackson.general.jsonproperty;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.math.BigDecimal;
import java.util.Date;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Book extends Item {
private String ISBN;
private Date published;
private BigDecimal pages;
private String binding;
public Book() {
}
public Book(String title, Author author) {
super(title, author);
}
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;
}
@JsonProperty("binding")
public String coverBinding() {
return binding;
}
@JsonProperty("binding")
public void configureBinding(String binding) {
this.binding = binding;
}
}

View File

@ -1,62 +0,0 @@
package com.baeldung.jackson.general.jsonproperty;
import com.baeldung.jackson.domain.Person;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Item {
private UUID id;
private String title;
private List<Person> authors = new ArrayList<>();
private float price;
public Item() {
}
public Item(String title, Author author) {
this.id = UUID.randomUUID();
this.title = title;
this.authors.add(author);
}
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 List<Person> getAuthors() {
return authors;
}
public void setAuthors(List<Person> authors) {
this.authors = authors;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}

View File

@ -1,70 +0,0 @@
package com.baeldung.jackson.general.jsonproperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.IOException;
import static io.restassured.path.json.JsonPath.from;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonPropertyUnitTest {
@Test
public void whenSerializingUsingJsonProperty_thenCorrect() throws JsonProcessingException {
// arrange
Book book = new Book("Design Patterns: Elements of Reusable Object-oriented Software", new Author("The", "GoF"));
book.configureBinding("Hardback");
// act
String result = new ObjectMapper().writeValueAsString(book);
// assert
assertThat(from(result).getString("binding")).isEqualTo("Hardback");
/*
{
"id": "cd941587-d1ae-4c2a-9a36-29533bf50411",
"title": "Design Patterns: Elements of Reusable Object-oriented Software",
"authors": [
{
"id": "c8e26318-2f5b-4fa2-9fdc-6e99be021fca",
"firstName": "The",
"lastName": "GoF",
"items": []
}
],
"price": 0,
"published": null,
"pages": null,
"isbn": null,
"binding": "Hardback"
}
*/
}
@Test
public void whenDeserializingUsingJsonProperty_thenCorrect() throws IOException {
// arrange
String result = "{\"id\":\"cd941587-d1ae-4c2a-9a36-29533bf50411\",\"title\":\"Design Patterns: Elements of Reusable Object-oriented Software\",\"authors\":[{\"id\":\"c8e26318-2f5b-4fa2-9fdc-6e99be021fca\",\"firstName\":\"The\",\"lastName\":\"GoF\"}],\"binding\":\"Hardback\"}";
// act
Book book = new ObjectMapper().readerFor(Book.class)
.readValue(result);
// assert
assertThat(book.coverBinding()).isEqualTo("Hardback");
}
}

View File

@ -1,41 +0,0 @@
package com.baeldung.jackson.inclusion.jsonignore;
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;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonIgnoreUnitTest {
@Test
public void whenSerializingUsingJsonIgnore_thenCorrect() throws JsonProcessingException {
// arrange
Author author = new Author("Alex", "Theedom");
// act
String result = new ObjectMapper().writeValueAsString(author);
// assert
assertThat(from(result).getString("firstName")).isEqualTo("Alex");
assertThat(from(result).getString("lastName")).isEqualTo("Theedom");
assertThat(from(result).getString("id")).isNull();
/*
{
"firstName": "Alex",
"lastName": "Theedom",
"items": []
}
*/
}
}

View File

@ -1,52 +0,0 @@
package com.baeldung.jackson.inclusion.jsonignoreproperties;
import com.baeldung.jackson.domain.Author;
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;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonIgnorePropertiesUnitTest {
@Test
public void whenSerializingUsingJsonIgnoreProperties_thenCorrect() throws JsonProcessingException {
// arrange
Course course = new Course("Spring Security", new Author("Eugen", "Paraschiv"));
course.setMedium(Course.Medium.ONLINE);
// act
String result = new ObjectMapper().writeValueAsString(course);
// assert
assertThat(from(result).getString("medium")).isNull();
/*
{
"id": "ef0c8d2b-b088-409e-905c-95ac88dc0ed0",
"title": "Spring Security",
"authors": [
{
"id": "47a4f498-b0f3-4daf-909f-d2c35a0fe3c2",
"firstName": "Eugen",
"lastName": "Paraschiv",
"items": []
}
],
"price": 0,
"duration": 0,
"level": null,
"prerequisite": null
}
*/
}
}

View File

@ -1,40 +0,0 @@
package com.baeldung.jackson.inclusion.jsoninclude;
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;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonIncludeUnitTest {
@Test
public void whenSerializingUsingJsonInclude_thenCorrect() throws JsonProcessingException {
// arrange
Author author = new Author("Alex", null);
// act
String result = new ObjectMapper().writeValueAsString(author);
// assert
assertThat(from(result).getString("firstName")).isEqualTo("Alex");
assertThat(result).doesNotContain("lastName");
/*
{
"id": "e8bb4802-6e0c-4fa5-9f68-c233272399cd",
"firstName": "Alex",
"items": []
}
*/
}
}

View File

@ -1,51 +0,0 @@
package com.baeldung.jackson.miscellaneous.custom;
import com.baeldung.jackson.domain.Author;
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;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class CustomUnitTest {
@Test
public void whenSerializingUsingCustom_thenCorrect() throws JsonProcessingException {
// arrange
Course course = new Course("Spring Security", new Author("Eugen", "Paraschiv"));
course.setMedium(Course.Medium.ONLINE);
// act
String result = new ObjectMapper().writeValueAsString(course);
// assert
assertThat(from(result).getString("title")).isEqualTo("Spring Security");
/*
{
"title": "Spring Security",
"price": 0,
"id": "7dfd4db9-1175-432f-a53b-687423f7bb9b",
"duration": 0,
"authors": [
{
"id": "da0738f6-033c-4974-8d87-92820e5ccf27",
"firstName": "Eugen",
"lastName": "Paraschiv",
"items": []
}
],
"medium": "ONLINE"
}
*/
}
}

View File

@ -1,58 +0,0 @@
package com.baeldung.jackson.miscellaneous.disable;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
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;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class DisableUnitTest {
@Test
public void whenSerializingUsingDisable_thenCorrect() throws JsonProcessingException {
// arrange
Author author = new Author("Alex", "Theedom");
// act
ObjectMapper mapper = new ObjectMapper();
String result = mapper.writeValueAsString(author);
// assert
assertThat(from(result).getList("items")).isNull();
/*
{
"lastName": "Theedom",
"firstName": "Alex",
"id": "de4afbb4-b24d-45c8-bb00-fd6b9acb42f1"
}
*/
// act
mapper = new ObjectMapper();
mapper.disable(MapperFeature.USE_ANNOTATIONS);
result = mapper.writeValueAsString(author);
// assert
assertThat(from(result).getList("items")).isNotNull();
/*
{
"id": "81e6ed72-6b27-4fe9-a36f-e3171c5b55ef",
"firstName": "Alex",
"lastName": "Theedom",
"items": []
}
*/
}
}

View File

@ -1,58 +0,0 @@
package com.baeldung.jackson.miscellaneous.mixin;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.util.List;
import static io.restassured.path.json.JsonPath.from;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class MixInUnitTest {
@Test
public void whenSerializingUsingMixIn_thenCorrect() throws JsonProcessingException {
// arrange
Author author = new Author("Alex", "Theedom");
// act
String result = new ObjectMapper().writeValueAsString(author);
// assert
assertThat(from(result).getList("items")).isNotNull();
/*
{
"id": "f848b076-00a4-444a-a50b-328595dd9bf5",
"firstName": "Alex",
"lastName": "Theedom",
"items": []
}
*/
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(List.class, IgnoreListMixIn.class);
result = mapper.writeValueAsString(author);
// assert
assertThat(from(result).getList("items")).isNull();
/*
{
"id": "9ffefb7d-e56f-447c-9009-e92e142f8347",
"firstName": "Alex",
"lastName": "Theedom"
}
*/
}
}

View File

@ -1,49 +0,0 @@
package com.baeldung.jackson.serialization.jsonanygetter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.util.Map;
import static io.restassured.path.json.JsonPath.from;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonAnyGetterUnitTest {
@Test
public void whenSerializingUsingJsonAnyGetter_thenCorrect() throws JsonProcessingException {
// arrange
Inventory inventory = new Inventory();
Map<String, Float> countryDeliveryCost = inventory.getCountryDeliveryCost();
inventory.setLocation("France");
countryDeliveryCost.put("USA", 10.00f);
countryDeliveryCost.put("UK", 15.00f);
// act
String result = new ObjectMapper().writeValueAsString(inventory);
// assert
assertThat(from(result).getString("location")).isEqualTo("France");
assertThat(from(result).getFloat("USA")).isEqualTo(10.00f);
assertThat(from(result).getFloat("UK")).isEqualTo(15.00f);
/*
{
"location": "France",
"USA": 10,
"UK": 15
}
*/
}
}

View File

@ -1,40 +0,0 @@
package com.baeldung.jackson.serialization.jsongetter;
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;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonGetterUnitTest {
@Test
public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException {
// arrange
Author author = new Author("Alex", "Theedom");
// act
String result = new ObjectMapper().writeValueAsString(author);
// assert
assertThat(from(result).getList("publications")).isNotNull();
assertThat(from(result).getList("items")).isNull();
/*
{
"firstName": "Alex",
"lastName": "Theedom",
"publications": []
}
*/
}
}

View File

@ -1,42 +0,0 @@
package com.baeldung.jackson.serialization.jsonpropertyorder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonPropertyOrderUnitTest {
@Test
public void whenSerializingUsingJsonPropertyOrder_thenCorrect() throws JsonProcessingException {
// arrange
Author author = new Author("Alex", "Theedom");
// act
String result = new ObjectMapper().writeValueAsString(author);
// assert
assertThat(result, matchesJsonSchemaInClasspath("author-jsonpropertyorder-schema.json"));
// NOTE: property order is not enforced by the JSON specification.
/*
{
"items": [],
"firstName": "Alex",
"lastName": "Theedom",
"id": "fd277638-9b6e-49f7-81c1-bc52f165245b"
}
*/
}
}

View File

@ -1,40 +0,0 @@
package com.baeldung.jackson.serialization.jsonrawvalue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonRawValueUnitTest {
@Test
public void whenSerializingUsingJsonRawValue_thenCorrect() throws JsonProcessingException {
// arrange
String customerConfig = "{\"colour\":\"red\",\"device\":\"mobile\",\"orientation\":\"landscape\"}";
Customer customer = new Customer("Alex", "Theedom");
customer.setConfiguration("{\"colour\":\"red\",\"device\":\"mobile\",\"orientation\":\"landscape\"}");
// act
String result = new ObjectMapper().writeValueAsString(customer);
// assert
assertThat(result.contains(customerConfig));
/*
{
"firstName": "Alex",
"lastName": "Theedom",
"publications": []
}
*/
}
}

View File

@ -1,46 +0,0 @@
package com.baeldung.jackson.serialization.jsonrootname;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.Test;
import static io.restassured.path.json.JsonPath.from;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonRootNameUnitTest {
@Test
public void whenSerializingUsingJsonRootName_thenCorrect() throws JsonProcessingException {
// arrange
Author author = new Author("Alex", "Theedom");
// act
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
String result = mapper.writeValueAsString(author);
// assert
assertThat(from(result).getString("writer.firstName")).isEqualTo("Alex");
assertThat(from(result).getString("author.firstName")).isNull();
/*
{
"writer": {
"id": "0f50dca6-3dd7-4801-a334-fd1614276389",
"firstName": "Alex",
"lastName": "Theedom",
"items": []
}
}
*/
}
}

View File

@ -1,58 +0,0 @@
package com.baeldung.jackson.serialization.jsonserialize;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import static io.restassured.path.json.JsonPath.from;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonSerializeUnitTest {
@Test
public void whenSerializingUsingJsonSerialize_thenCorrect() throws JsonProcessingException, ParseException {
// arrange
Author joshuaBloch = new Author("Joshua", "Bloch");
Book book = new Book("Effective Java", joshuaBloch);
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String toParse = "25-12-2017 13:30:25";
book.setPublished(df.parse(toParse));
// act
String result = new ObjectMapper().writeValueAsString(book);
// assert
assertThat(from(result).getString("published")).isEqualTo(toParse);
/*
{
"id": "957c43f2-fa2e-42f9-bf75-6e3d5bb6960a",
"title": "Effective Java",
"authors": [
{
"id": "9bcd817d-0141-42e6-8f04-e5aaab0980b6",
"firstName": "Joshua",
"lastName": "Bloch",
"items": []
}
],
"price": 0,
"published": "25-12-2017 13:30:25",
"pages": null,
"isbn": null
}
*/
}
}

View File

@ -1,27 +0,0 @@
package com.baeldung.jackson.serialization.jsonvalue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class JsonValueUnitTest {
@Test
public void whenSerializingUsingJsonValue_thenCorrect() throws JsonProcessingException {
// act
String result = new ObjectMapper().writeValueAsString(Course.Level.ADVANCED);
// assert
assertThat(result).isEqualTo("\"Advanced\"");
}
}

View File

@ -39,7 +39,6 @@ import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue;
import com.baeldung.jackson.exception.UserWithRoot;
import com.baeldung.jackson.jsonview.Item;
import com.baeldung.jackson.jsonview.Views;
import com.baeldung.jackson.serialization.jsonrootname.Author;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.MapperFeature;
@ -48,7 +47,6 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class JacksonAnnotationUnitTest {
@ -388,30 +386,5 @@ public class JacksonAnnotationUnitTest {
// assert
assertThat(aliasBean.getFirstName(), is("Alex"));
}
@Test
public void whenSerializingUsingXMLRootNameWithNameSpace_thenCorrect() throws JsonProcessingException {
// arrange
Author author = new Author("Alex", "Theedom");
// act
ObjectMapper mapper = new XmlMapper();
mapper = mapper.enable(SerializationFeature.WRAP_ROOT_VALUE).enable(SerializationFeature.INDENT_OUTPUT);
String result = mapper.writeValueAsString(author);
// assert
assertThat(result, containsString("<writer xmlns=\"book\">"));
/*
<writer xmlns="book">
<id xmlns="">3006b44a-cf62-4cfe-b3d8-30dc6c46ea96</id>
<firstName xmlns="">Alex</firstName>
<lastName xmlns="">Theedom</lastName>
<items xmlns=""/>
</writer>
*/
}
}