Hi Guys,
Welcome to Java Inspires.
In this post, we will see how to serialize and de-serialize java Set (HashSet)object using Jackson api.
Here, we are using ObjectMapper class from JAckson library and writeValueAsString method to convert serialize and readValue method to deserialize.
How to Convert Java Set Object to/from JSON String using Jackson?(Serialize and Deserialize)
MainApp.java
package com.javainspires; import java.io.IOException; import java.util.HashSet; import java.util.Set; import com.fasterxml.jackson.databind.ObjectMapper; /** * * @author #JavaInspires * */ public class MainApp { public static void main(String[] args) throws IOException { // create a Set object Set<String> sampleSet = new HashSet<>(); sampleSet.add("C"); sampleSet.add("C++"); sampleSet.add("Java"); sampleSet.add("Python"); sampleSet.add("GoLang"); sampleSet.add("Kotlin"); // create object mapper class object ObjectMapper mapper = new ObjectMapper(); // convert set object to json string using this mapper String jsonStr = mapper.writeValueAsString(sampleSet); System.out.println("\nJava Set Object To JSON\n"); // print json string System.out.println(jsonStr); // now convert this json string to java set object // using object mapper Set<String> set1 = mapper.readValue(jsonStr.getBytes(), Set.class); System.out.println("\nJSON String to Java Set Object\n"); // print this list set1.forEach(System.out::println); } }
Java Set Object To JSON ["Java","C++","C","GoLang","Python","Kotlin"] JSON String to Java Set Object Java C++ C GoLang Python Kotlin
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javainspires</groupId> <artifactId>jackson-examples</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.2</version> </dependency> </dependencies> </project>
4x8 sheet metal prices near me - Titsanium Art - Tutu, China
ReplyDeleteCheck titanium wok out the latest titanium bar stock 4x8 sheet titanium watches metal prices titanium properties near me on Titsanium Art. Discover our online store for ceramic vs titanium curling iron premium art prints and other merchandise.