-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
514 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
utils/src/main/java/t5750/utils/jackson/entity/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
utils/src/main/java/t5750/utils/jackson/entity/Writer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.