![Mkyong.com](https://i1.feedspot.com/5270460.jpg?t=1618651691)
Mkyong.com
1,030 FOLLOWERS
Mkyong.com is providing Java and Spring tutorials and code snippets since 2008. All published articles are simple and easy to understand and well tested in our development environment.
Mkyong.com
2M ago
In Java, we can use the Files.walk() API to traverse all files from a folder and its subfolders.
Table of contents:
1. Traverse all files from a folder in Java
2. Traverse all files from a folder and its subfolders in Java
3. List all file name from a folder in Java
4. Find a file from a folder and its subfolders in Java
5. Download Source Code
6. References
P.S Tested with Java 21
1. Traverse all files from a folder in Java
Let’s assume the folder /Users/mkyong/projects/test/ contains the following files:
/Users/mkyong/projects/test/
|-- a.txt
|-- b.txt
|-- bootstrap-icons.min.css
|-- exa ..read more
Mkyong.com
2M ago
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>
2. Default Compact Print JSON
By default, JSON.simple prints all JSON in compact mode.
JsonSimple ..read more
Mkyong.com
2M ago
Gson does not directly support Java 8’s date-time APIs like LocalDate, LocalDateTime, and ZonedDateTime. However, we can create a custom JSON serializer to support Java 8 date-time types.
This article shows how to use Gson to handle the LocalDate type.
Table of contents:
1. Setup Google Gson
2. Default, Gson does not support Java 8 date-time type
3. Custom Serializer and Deserializer for LocalDate
4. Download Source Code
5. References
P.S Tested with Gson 2.10.1
1. Setup Google Gson
Declares gson in the pom.xml.
pom.xml
<dependency>
<groupId>com.google.code.gson< ..read more
Mkyong.com
2M ago
When the JSON field name doesn’t exactly match the Java object field names, we can use @SerializedName to change the JSON’s matching field name in Gson.
Table of contents:
1. Setup Google Gson
2. JSON Unrecognized Field
3. Custom field name with @SerializedName
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. JSON ..read more
Mkyong.com
2M ago
This article shows how to use Gson to write a Java object to a JSON file and read that JSON file back into a Java object.
Table of contents:
1. Download Google Gson
2. A Java Object
3. Write Java object to JSON file using Gson
4. Read JSON from a file using Gson
5. Download Source Code
6. References
P.S Tested with Gson 2.10.1
1. Download 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
Mkyong.com
2M ago
When the JSON field name doesn’t exactly match the Java field names, we can use @JsonProperty to change the name of a JSON field in Jackson.
Table of contents:
1. Setup Jackson
2. JSON Unrecognized field
3. Custom field name with @JsonProperty
4. Download Source Code
5. References
P.S Tested with Jackson 2.17.0
1. Setup Jackson
Puts jackson-databind at pom.xml.
pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
</dependency>
2. JSON Unrecognize ..read more
Mkyong.com
2M ago
Moshi does not have built-in adapters to process types like Java 8 date time APIs java.time.* or JSR-310; we need to manually provide a custom adapter to handle the Java 8 date time type.
This article shows how to use Moshi to convert the Java 8 Localdate type to JSON data.
Table of contents:
1. Download Moshi
2. LocalDate requires explicit JsonAdapter
3. Make Moshi supports Java 8 Date Time APIs
3.1 Define LocalDate Adapter
3.2 Register the Adapter with 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
&l ..read more
Mkyong.com
3M ago
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>
<artifactId>moshi</artifactId>
<version>1.15.1</version>
</dependency>
2. Compact print JSON (Default)
By default, Moshi prints JSON strings in ..read more
Mkyong.com
3M ago
This article shows how to write JSON to a file using Moshi.
Table of contents:
1. Download Moshi
2. Write JSON to a file using Moshi
3. Read JSON from a file using Moshi
4. Download Source Code
6. 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>
<artifactId>moshi</artifactId>
<version>1.15.1</version>
</dependency>
2. Write JSON to a file using Moshi
The following example uses Moshi to convert a Java object Person ..read more
Mkyong.com
3M ago
This article shows how to parse JSON using Moshi.
Table of contents:
1. Download Moshi
2. Parse JSON using Moshi
3. Parse JSON Array using Moshi
4. Convert Java Object to JSON String
5. Download Source Code
6. References
P.S Tested with Moshi 1.15.1
1. Download Moshi
Declare moshi in the pom.xml, and it will automatically pull in other necessary dependencies.
pom.xml
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>1.15.1</version>
</dependency>
Terminal
$ mvn dependen ..read more