How to traverse all files from a folder in Java
Mkyong.com
by mkyong
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
Visit website
Pretty Print JSON with JSON.simple
Mkyong.com
by mkyong
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
Visit website
Gson supports Java 8 date time types
Mkyong.com
by mkyong
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
Visit website
Gson custom field name with @SerializedName
Mkyong.com
by mkyong
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
Visit website
Read and Write JSON to File using Gson
Mkyong.com
by mkyong
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
Visit website
Jackson Custom field name with @JsonProperty
Mkyong.com
by mkyong
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
Visit website
Moshi java.time.LocalDate requires explicit JsonAdapter to be registered
Mkyong.com
by mkyong
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
Visit website
How to pretty print JSON using Moshi
Mkyong.com
by mkyong
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
Visit website
How to write JSON to a file using Moshi
Mkyong.com
by mkyong
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
Visit website
How to parse JSON using Moshi
Mkyong.com
by mkyong
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
Visit website

Follow Mkyong.com on FeedSpot

Continue with Google
Continue with Apple
OR