From 7c4744777605be0e2c89d2825853257fbad73a3e Mon Sep 17 00:00:00 2001 From: Bahaa El-Din Helmy Date: Sat, 20 May 2023 19:00:00 +0300 Subject: [PATCH] Convert Hashmap to JSON object in Java (#14070) * Convert Hashmap to JSON object in Java This commit is related to the article entitled "Convert Hashmap to JSON object in Java" * Convert Hashmap to JSON object in Java This commit is for the article "Convert Hashmap to JSON object in Java" --- .../core-java-collections-maps-6/pom.xml | 34 ++++++++++ .../baeldung/maptojson/MapToJsonUnitTest.java | 65 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/maptojson/MapToJsonUnitTest.java diff --git a/core-java-modules/core-java-collections-maps-6/pom.xml b/core-java-modules/core-java-collections-maps-6/pom.xml index 70a04c352b..8c35dcee6e 100644 --- a/core-java-modules/core-java-collections-maps-6/pom.xml +++ b/core-java-modules/core-java-collections-maps-6/pom.xml @@ -16,5 +16,39 @@ 5.2.5.RELEASE + + + com.fasterxml.jackson.core + jackson-databind + 2.13.1 + + + org.openjdk.jmh + jmh-core + 1.36 + + + com.google.code.gson + gson + 2.8.9 + + + org.json + json + 20230227 + + + junit + junit + 4.13.1 + test + + + org.junit.jupiter + junit-jupiter + 5.8.1 + test + + \ No newline at end of file diff --git a/core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/maptojson/MapToJsonUnitTest.java b/core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/maptojson/MapToJsonUnitTest.java new file mode 100644 index 0000000000..7a9f046a94 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/maptojson/MapToJsonUnitTest.java @@ -0,0 +1,65 @@ +package com.baeldung.maptojson; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonSyntaxException; +import com.google.gson.TypeAdapter; +import com.google.gson.reflect.TypeToken; +import org.json.JSONObject; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; + +public class MapToJsonUnitTest { + final TypeAdapter strictAdapter = new Gson().getAdapter(JsonElement.class); + + public boolean isValid(String json) { + try { + strictAdapter.fromJson(json); + } catch (JsonSyntaxException | IOException e) { + return false; + } + return true; + } + + @Test + public void given_HashMapData_whenUsingJackson_thenConvertToJson() throws JsonProcessingException { + Map data = new HashMap(); + data.put("CS", "Post1"); + data.put("Linux", "Post1"); + data.put("Kotlin", "Post1"); + ObjectMapper objectMapper = new ObjectMapper(); + String jacksonData = objectMapper.writeValueAsString(data); + Assertions.assertTrue(isValid(jacksonData)); + } + + @Test + public void given_HashMapData_whenUsingGson_thenConvertToJson() { + Map data = new HashMap<>(); + data.put("CS", "Post1"); + data.put("Linux", "Post1"); + data.put("Kotlin", "Post1"); + Gson gson = new Gson(); + Type typeObject = new TypeToken() { + }.getType(); + String gsonData = gson.toJson(data, typeObject); + Assertions.assertTrue(isValid(gsonData)); + } + + @Test + public void given_HashMapData_whenOrgJson_thenConvertToJsonUsing() { + Map data = new HashMap<>(); + data.put("CS", "Post1"); + data.put("Linux", "Post1"); + data.put("Kotlin", "Post1"); + JSONObject jsonObject = new JSONObject(data); + String orgJsonData = jsonObject.toString(); + Assertions.assertTrue(isValid(orgJsonData)); + } +} \ No newline at end of file