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"
This commit is contained in:
Bahaa El-Din Helmy 2023-05-20 19:00:00 +03:00 committed by GitHub
parent e80e82c746
commit 7c47447776
2 changed files with 99 additions and 0 deletions

View File

@ -16,5 +16,39 @@
<properties> <properties>
<spring.version>5.2.5.RELEASE</spring.version> <spring.version>5.2.5.RELEASE</spring.version>
</properties> </properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.36</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project> </project>

View File

@ -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<JsonElement> 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<String, String> 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<String, String> data = new HashMap<>();
data.put("CS", "Post1");
data.put("Linux", "Post1");
data.put("Kotlin", "Post1");
Gson gson = new Gson();
Type typeObject = new TypeToken<HashMap>() {
}.getType();
String gsonData = gson.toJson(data, typeObject);
Assertions.assertTrue(isValid(gsonData));
}
@Test
public void given_HashMapData_whenOrgJson_thenConvertToJsonUsing() {
Map<String, String> 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));
}
}