Skip to content

Commit

Permalink
Update Jackson
Browse files Browse the repository at this point in the history
  • Loading branch information
T5750 committed Oct 3, 2020
1 parent 054c9f7 commit 1174496
Show file tree
Hide file tree
Showing 18 changed files with 514 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
## What's included
A ~ C | D ~ G | H ~ L | M ~ P | Q ~ S | T ~ Z
----|----|----|----|----|----
| [ASM](jdk8/README.md) | [Gson](utils/README.md) | [Javassist](jdk8/README.md) | [NIO](nio/README.md) | [Security](security/README.md) | [Threads](threads/README.md)
| [Blockchain](blockchain/README.md) | | [JDK7](jdk7/README.md) | [Patterns](patterns/README.md) | | [Utils](utils/README.md)
| [ASM](jdk8/README.md) | [Gson](utils/README.md) | [Jackson](utils/README.md) | [NIO](nio/README.md) | [Security](security/README.md) | [Threads](threads/README.md)
| [Blockchain](blockchain/README.md) | | [Javassist](jdk8/README.md) | [Patterns](patterns/README.md) | | [Utils](utils/README.md)
| | | [JDK7](jdk7/README.md) | | |
| | | [JDK8](jdk8/README.md) | | |
| | | [JVM](jvm/README.md) | | |

Expand Down
6 changes: 5 additions & 1 deletion doc/source/utils/jackson/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ Jackson
:numbered: 0

jacksonObjectMapper
jacksonJsonFilter
jacksonJsonFilter
jacksonJsonFormat
jacksonIgnoreNull
jacksonJsonNodeForEach
jacksonJsonParserJsonGenerator
12 changes: 12 additions & 0 deletions doc/source/utils/jackson/jacksonIgnoreNull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Jackson Ignore Null and Empty Fields

## Include.NON_NULL and Include.NON_EMPTY
To ignore Null fields in JSON, Jackson provides `Include.NON_NULL` and to ignore Empty fields Jackson provides `Include.NON_EMPTY`.
- `Include.NON_NULL`: Indicates that only properties with not null values will be included in JSON.
- `Include.NON_EMPTY`: Indicates that only properties that are not empty will be included in JSON. Non-empty can have different meaning for different objects such as `List` with size zero will be considered as empty. In case of `Map` to check empty `isEmpty()` is called.

## Results
- `JacksonIgnoreNullTest`

## References
- [Jackson Ignore Null and Empty Fields](https://www.concretepage.com/jackson-api/jackson-ignore-null-and-empty-fields)
3 changes: 3 additions & 0 deletions doc/source/utils/jackson/jacksonJsonFilter.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ System.out.println(jsonData);
## @JsonFilter at Property Level
We can use `@JsonFilter` annotation on fields, methods and constructor parameters since Jackson 2.3. Here we will create two filters using `@JsonFilter` at property level and filter the properties using `serializeAllExcept()` and `filterOutAllExcept()` methods.

## Results
- `JacksonJsonFilterTest`

## References
- [Jackson @JsonFilter Example](https://www.concretepage.com/jackson-api/jackson-jsonfilter-example)
18 changes: 18 additions & 0 deletions doc/source/utils/jackson/jacksonJsonFormat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Jackson @JsonFormat

## @JsonFormat
`@JsonFormat` handles the serialization of `Date`, `Enum`, `Collection` and `Number`. `@JsonFormat` decides how values of properties to be serialized. Find some of the attributes of `@JsonFormat` annotation.
- `shape`: Defines the structure to use for serialization, for example `JsonFormat.Shape.NUMBER` and `JsonFormat.Shape.STRING`.
- `pattern`: Pattern used in serialization and deserializations. For date, pattern contains `SimpleDateFormat` compatible definition.
- `locale`: `Locale` used in serialization. Default is system default locale.
- `timezone`: `TimeZone` used in serialization. Default is system default timezone.
- `lenient`: Useful in deserializations. It decides if lenient handling should be enabled or disabled.

## @JsonFormat with Enum
`@JsonFormat` can be used with Java `enum` in serialization to change between index (number) and textual name (string). `@JsonFormat` is used at `enum` level and not at property level. By default `enum` properties are serialized with its textual name as string. We can change it to property index (starting from 0) using `JsonFormat.Shape.NUMBER`.

## Results
- `JacksonJsonFormatTest`

## References
- [Jackson @JsonFormat Example](https://www.concretepage.com/jackson-api/jackson-jsonformat-example)
22 changes: 22 additions & 0 deletions doc/source/utils/jackson/jacksonJsonNodeForEach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Jackson JsonNode.forEach() with Java 8 Consumer

Jackson has provided JsonNode.forEach() method which will accept Java 8 consumer definition to iterate each node. The consumer accepts only super classes of JsonNode. It has been defined as below.
```
forEach(Consumer<? super JsonNode> arg)
```
We can define consumer only with super class of JsonNode. Now find the simple example to parse JSON and iterating it using Java 8 consumer.

```
JsonFactory jsonFactory = new JsonFactory();
JsonParser jp = jsonFactory.createJsonParser(new File("D:/cp/info.json"));
jp.setCodec(new ObjectMapper());
JsonNode jsonNode = jp.readValueAsTree();
Consumer<JsonNode> data = (JsonNode node) -> System.out.println(node.asText());
jsonNode.forEach(data);
```

## Results
- `JacksonJsonNodeForEachTest`

## References
- [Jackson JsonNode.forEach() with Java 8 Consumer](https://www.concretepage.com/jackson-api/jackson-jsonnode-foreach-with-java-8-consumer)
9 changes: 9 additions & 0 deletions doc/source/utils/jackson/jacksonJsonParserJsonGenerator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Jackson JsonParser JsonGenerator

Jackson provides faster Streaming API i.e `JsonParser` and `JsonGenerator`. `JsonParser` reads JSON file and `JsonGenerator` writes java object or map into JSON file using coded object.

## Results
- `JacksonJsonParserJsonGeneratorTest`

## References
- [Read/Write JSON Using Jackson ObjectMapper, JsonParser and JsonGenerator Example](https://www.concretepage.com/jackson-api/read-write-json-using-jackson-objectmapper-jsonparser-jsongenerator-example)
4 changes: 4 additions & 0 deletions doc/source/utils/jackson/jacksonObjectMapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,9 @@ List<Car> listCar = objectMapper.readValue(jsonCarArray, new TypeReference<List<
## Conclusion
Jackson is a solid and mature JSON serialization/deserialization library for Java.

## Results
- `SerializationDeserializationFeatureTest`
- `JavaReadWriteJsonTest`

## References
- [Intro to the Jackson ObjectMapper](https://www.baeldung.com/jackson-object-mapper-tutorial)
4 changes: 4 additions & 0 deletions utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
### Jackson
- [Jackson ObjectMapper](../doc/source/utils/jackson/jacksonObjectMapper.md)
- [Jackson @JsonFilter](../doc/source/utils/jackson/jacksonJsonFilter.md)
- [Jackson @JsonFormat](../doc/source/utils/jackson/jacksonJsonFormat.md)
- [Jackson Ignore Null and Empty Fields](../doc/source/utils/jackson/jacksonIgnoreNull.md)
- [Jackson JsonNode.forEach() with Java 8 Consumer](../doc/source/utils/jackson/jacksonJsonNodeForEach.md)
- [Jackson JsonParser JsonGenerator](../doc/source/utils/jackson/jacksonJsonParserJsonGenerator.md)

### Properties
- [Java Properties](../doc/source/utils/property/javaProperties.md)
Expand Down
50 changes: 50 additions & 0 deletions utils/src/main/java/t5750/utils/jackson/entity/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package t5750.utils.jackson.entity;

public class Book {
private Integer id;
private String name;
private String writer;
private String category;

public Book() {
}

public Book(Integer id, String name, String writer, String category) {
this.id = id;
this.name = name;
this.writer = writer;
this.category = category;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getWriter() {
return writer;
}

public void setWriter(String writer) {
this.writer = writer;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}
}
26 changes: 26 additions & 0 deletions utils/src/main/java/t5750/utils/jackson/entity/Company.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package t5750.utils.jackson.entity;

import java.util.Map;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Company {
Expand All @@ -12,6 +15,8 @@ public class Company {
@JsonFilter("addressFilter")
@JsonProperty("compAddress")
private Address address;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Map<String, String> employess;

public Company() {
}
Expand All @@ -21,4 +26,25 @@ public Company(String name, Employee ceo, Address address) {
this.ceo = ceo;
this.address = address;
}

public Company(String name, Map<String, String> employess) {
this.name = name;
this.employess = employess;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Map<String, String> getEmployess() {
return employess;
}

public void setEmployess(Map<String, String> employess) {
this.employess = employess;
}
}
70 changes: 70 additions & 0 deletions utils/src/main/java/t5750/utils/jackson/entity/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package t5750.utils.jackson.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

public class Person {
private Integer id;
private String name;
@JsonInclude(Include.NON_NULL)
private String city;
@JsonInclude(Include.NON_EMPTY)
private String country;
private Address address;

public Person() {
}

public Person(Integer id, String name, String city, String country) {
this.id = id;
this.name = name;
this.city = city;
this.country = country;
}

public Person(Integer id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}
}
50 changes: 50 additions & 0 deletions utils/src/main/java/t5750/utils/jackson/entity/Writer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package t5750.utils.jackson.entity;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Writer {
@JsonProperty("writerName")
private String name;
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
@JsonProperty("bdate")
private Date birthDate;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MMM-dd HH:mm:ss z", timezone = "GMT+8")
@JsonProperty("pubDate")
private Date recentBookPubDate;

public Writer() {
}

public Writer(String name, Date birthDate, Date recentBookPubDate) {
this.name = name;
this.birthDate = birthDate;
this.recentBookPubDate = recentBookPubDate;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Date getBirthDate() {
return birthDate;
}

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}

public Date getRecentBookPubDate() {
return recentBookPubDate;
}

public void setRecentBookPubDate(Date recentBookPubDate) {
this.recentBookPubDate = recentBookPubDate;
}
}
9 changes: 9 additions & 0 deletions utils/src/main/java/t5750/utils/jackson/enums/Code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package t5750.utils.jackson.enums;

import com.fasterxml.jackson.annotation.JsonFormat;

//@JsonFormat(shape = JsonFormat.Shape.NUMBER)
@JsonFormat(shape = JsonFormat.Shape.STRING)
public enum Code {
BLOCKING, CRITICAL, MEDIUM, LOW;
}

0 comments on commit 1174496

Please sign in to comment.