Pretty Print JSON with JSON.simple

This article shows how to pretty print JSON with JSON.simple. Table of contents: 1. Setup JSON.simple 2. Default Compact Print JSON 3. Pretty Print JSON with JSON.simple 4. Write Pretty Print JSON to a File 5. Download Source Code 6. References P.S Tested with json-simple 4.0.1 1. Setup JSON.simple pom.xml <dependency> <groupId>com.github.cliftonlabs</groupId> <artifactId>json-simple</artifactId> <version>4.0.1</version> </dependency> …

Read more

How to exclude fields in Gson

In GSON, we can use the transient keyword, ExclusionStrategy, and @Expose to exclude fields during the JSON serialization or deserialization process. Table of contents: 1. Setup Google Gson 2. Using the "transient" Keyword 3. Using the "ExclusionStrategy" 4. Using the "ExclusionStrategy" and custom annotation 5. Using the @Expose annotation 6. Download Source Code 7. References …

Read more

Jackson Custom Serializer and Deserializer Examples

This article shows how to create a Jackson custom serializer and deserializer to parse JSON data that contains a LocalDate type. The Jackson custom serializer or deserializer is useful when we want to process a specific format that is not the default. Table of contents: 1. Setup Jackson 2. Jackson Custom Serializer for LocalDate 3. …

Read more

How to pretty print JSON using Moshi

This article shows how to use Moshi’s JsonAdaptor indent method to enable the pretty print JSON. Table of contents: 1. Download Moshi 2. Compact print JSON (Default) 3. Pretty print JSON using Moshi 4. Download Source Code 5. References P.S Tested with Moshi 1.15.1 1. Download Moshi Declare moshi in the pom.xml. pom.xml <dependency> <groupId>com.squareup.moshi</groupId> …

Read more

Jackson and Lombok examples

This article shows how to use Jackson and Lombok together. Table of contents: 1. Download Jackson and Lombok 2. Model with Lombok annotations 3. Parse JSON using Jackson 5. Download Source Code 6. References P.S Tested with Jackson 2.17.0 and Lombok 1.18.32 1. Download Jackson and Lombok Declares jackson-databind and lombok at pom.xml. pom.xml <dependency> …

Read more

How to ignore a field with Jackson

In Jackson, we can use @JsonIgnore to ignore a single field, @JsonIgnoreProperties to ignore multiple fields and @JsonIgnoreType to ignore a specified type during JSON serialization and deserialization. Table of contents: 1. Setup Jackson 2. Jackson @JsonIgnore – Ignores a field 3. Jackson @JsonIgnoreProperties – Ignores multiple fields 4. Jackson @JsonIgnoreType – Ignores a specified …

Read more

Jackson Java 8 date/time type `java.time.LocalDate` not supported by default

This article shows how Jackson can support the Java 8 date time APIs or JSR-310. Table of contents: 1. Download Jackson 2. Jackson does not support Java 8 date time APIs 3. Make Jackson support Java 8 date time APIs or JSR310 3.1 jackson-datatype-jsr310 3.1 Register JavaTimeModule() 4. Download Source Code 5. References P.S Tested …

Read more

Jackson @JsonView examples

In Jackson, we can use the annotation @JsonView to control which fields of the same resource are displayed for different users. Table of contents: 1. Download Jackson 2. Define View Classes 3. Annotate model class with @JsonView 4. Testing the Jackson @JasonView 5. Download Source Code 6. References P.S Tested with Jackson 2.17.0 1. Download …

Read more

How to parse JSON string with Jackson

This article uses the Jackson framework to parse JSON strings in Java. Table of contents: 1. Download Jackson 2. Parse JSON String with Jackson 3. Parse JSON Array with Jackson 4. Convert Java Object to JSON String 5. Convert JSON String to Java Object 6. Download Source Code 7. References P.S Tested with Jackson 2.17.0 …

Read more

How to ignore null fields with Jackson

In Jackson, we can use @JsonInclude(JsonInclude.Include.NON_NULL) to ignore the null fields during serialization. Table of contents: 1. Jackson default include null fields 2. Ignore all null fields globally 3. Ignore null fields (Class Level) 4. Ignore null fields (Field Level) 5. Download Source Code 6. References P.S Tested with Jackson 2.17.0 1. Jackson default include …

Read more

Convert JSON string to Map using Jackson

This article uses the Jackson framework to parse JSON strings to a Map in Java. Table of contents: 1. Download Jackson 2. Convert JSON string to Map 3. Convert Map to JSON string 4. Download Source Code 5. References P.S Tested with Jackson 2.17.0 1. Download Jackson Simply declare jackson-databind in the pom.xml, and it …

Read more

Read and write JSON using JSON.simple

This article shows how to read and write JSON using JSON.simple. Table of contents: 1. Setup JSON.simple 2. Write JSON to File using JSON.simple 3. Read JSON from File using JSON.simple 4. Java object to JSON using JSON.simple 5. JSON to Java Object using JSON.simple 6. Download Source Code 7. References P.S Tested with json-simple …

Read more

Gson Streaming APIs to read and write JSON

Streaming APIs are efficient ways of processing large JSON files or data without loading the entire document into memory. Gson provides JsonReader and JsonWriter classes for JSON streaming. Gson’s Streaming API JsonWriter – Write JSON as a stream. JsonReader – Read JSON as a stream. Table of contents: 1. Setup Google Gson 1. Write JSON …

Read more

Jackson Streaming API examples

This article shows how to use Jackson’s Streaming API for both reading from and writing to JSON. Jackson’s Streaming API JsonGenerator – Write JSON data JsonParser – Read and Parse JSON data Table of contents: 1. Download Jackson 2. Write JSON using JsonGenerator 2. Write JSON Array using JsonGenerator 3. Read JSON using JsonParser 4. …

Read more

How to enable pretty print JSON with Jackson

In Jackson, we can use writerWithDefaultPrettyPrinter() to pretty print the JSON strings. Table of contents: 1. Setup Jackson 2. Compact print JSON (Default) 3. Pretty print JSON with Jackson 4. Pretty print JSON with Jackson (Globally) 5. Download Source Code 6. References P.S Tested with Jackson 2.17.0 1. Setup Jackson Puts jackson-databind at pom.xml. pom.xml …

Read more

How to pretty print JSON using Gson

This article shows how to pretty print JSON using Gson. Table of contents: 1. Setup Google Gson 2. Default Compact Print JSON 3. Pretty Print JSON using Gson 4. Download Source Code 5. References P.S Tested with Gson 2.10.1 1. Setup Google Gson Declare gson in the pom.xml. pom.xml <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version> </dependency> 2. …

Read more