From 576236e2fd202f897769368c9e92cca0be347830 Mon Sep 17 00:00:00 2001 From: DiegoMarti2 <150871541+DiegoMarti2@users.noreply.github.com> Date: Mon, 18 Dec 2023 15:37:26 +0200 Subject: [PATCH 1/2] Delete json-modules/json-conversion/src/test/java/com/baeldung/bytearraytojsonandviseverse directory --- .../ByteArrayToJsonAndViseVerseUnitTest.java | 56 ------------------- 1 file changed, 56 deletions(-) delete mode 100644 json-modules/json-conversion/src/test/java/com/baeldung/bytearraytojsonandviseverse/ByteArrayToJsonAndViseVerseUnitTest.java diff --git a/json-modules/json-conversion/src/test/java/com/baeldung/bytearraytojsonandviseverse/ByteArrayToJsonAndViseVerseUnitTest.java b/json-modules/json-conversion/src/test/java/com/baeldung/bytearraytojsonandviseverse/ByteArrayToJsonAndViseVerseUnitTest.java deleted file mode 100644 index b86d6e0187..0000000000 --- a/json-modules/json-conversion/src/test/java/com/baeldung/bytearraytojsonandviseverse/ByteArrayToJsonAndViseVerseUnitTest.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.baeldung.bytearraytojsonandviseverse; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.gson.Gson; -import org.junit.jupiter.api.Test; - -import java.nio.charset.StandardCharsets; -import java.util.Arrays; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class ByteArrayToJsonAndViseVerseUnitTest { - - byte[] byteArray = {34, 123, 92, 34, 110, 97, 109, 101, 92, 34, 58, 92, 34, 65, 108, 105, 99, 101, 92, 34, 44, 92, 34, 97, 103, 101, 92, - 34, 58, 50, 53, 44, 92, 34, 105, 115, 83, 116, 117, 100, 101, 110, 116, 92, 34, 58, 116, 114, 117, 101, 44, 92, 34, - 104, 111, 98, 98, 105, 101, 115, 92, 34, 58, 91, 92, 34, 114, 101, 97, 100, 105, 110, 103, 92, 34, 44, 92, 34, 112, - 97, 105, 110, 116, 105, 110, 103, 92, 34, 93, 44, 92, 34, 97, 100, 100, 114, 101, 115, 115, 92, 34, 58, 123, 92, 34, - 99, 105, 116, 121, 92, 34, 58, 92, 34, 83, 109, 97, 108, 108, 118, 105, 108, 108, 101, 92, 34, 44, 92, 34, 122, 105, - 112, 99, 111, 100, 101, 92, 34, 58, 92, 34, 49, 50, 51, 52, 53, 92, 34, 125, 125, 34}; - - String jsonString = "{\"name\":\"Alice\",\"age\":25,\"isStudent\":true,\"hobbies\":[\"reading\",\"painting\"],\"address\":{\"city\":\"Smallville\",\"zipcode\":\"12345\"}}"; - - @Test - void givenByteArray_whenConvertingToJsonUsingJackson_thenJsonString() throws Exception { - ObjectMapper objectMapper = new ObjectMapper(); - String actualJsonString = objectMapper.readValue(byteArray, String.class); - - assertEquals(jsonString, actualJsonString); - } - - @Test - void givenByteArray_whenConvertingToJsonUsingGson_thenJsonString() { - Gson gson = new Gson(); - String jsonStringFromByteArray = new String(byteArray, StandardCharsets.UTF_8); - String actualJsonString = gson.fromJson(jsonStringFromByteArray, String.class); - - assertEquals(jsonString, actualJsonString); - } - - @Test - void givenJsonString_whenConvertingToByteArrayUsingJackson_thenByteArray() throws JsonProcessingException { - ObjectMapper objectMapper = new ObjectMapper(); - byte[] actualByteArray = objectMapper.writeValueAsBytes(jsonString); - - assertEquals(Arrays.toString(byteArray), Arrays.toString(actualByteArray)); - } - - @Test - void givenJsonString_whenConvertingToByteArrayUsingGson_thenByteArray() { - Gson gson = new Gson(); - byte[] actualByteArray = gson.toJson(jsonString).getBytes(); - - assertEquals(Arrays.toString(byteArray), Arrays.toString(actualByteArray)); - } -} From f3c579ffbe4f43c0c6425a27d558f150ee6cd72e Mon Sep 17 00:00:00 2001 From: DiegoMarti2 <150871541+DiegoMarti2@users.noreply.github.com> Date: Mon, 18 Dec 2023 15:38:39 +0200 Subject: [PATCH 2/2] baeldung-articles : BAEL-7071 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Commit for the article "Convert Byte Array to JSON and Vice Versa in Java" which adds the "ByteArrayToJsonAndViceVersaUnitTest" class to convert a byte array to JSON and vicе vеrsa in Java. --- .../ByteArrayToJsonAndViceVersaUnitTest.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 json-modules/json-conversion/src/test/java/com/baeldung/bytearraytojsonandviceversa/ByteArrayToJsonAndViceVersaUnitTest.java diff --git a/json-modules/json-conversion/src/test/java/com/baeldung/bytearraytojsonandviceversa/ByteArrayToJsonAndViceVersaUnitTest.java b/json-modules/json-conversion/src/test/java/com/baeldung/bytearraytojsonandviceversa/ByteArrayToJsonAndViceVersaUnitTest.java new file mode 100644 index 0000000000..5e33a645b2 --- /dev/null +++ b/json-modules/json-conversion/src/test/java/com/baeldung/bytearraytojsonandviceversa/ByteArrayToJsonAndViceVersaUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.bytearraytojsonandviceversa; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ByteArrayToJsonAndViceVersaUnitTest { + + byte[] byteArray = {34, 123, 92, 34, 110, 97, 109, 101, 92, 34, 58, 92, 34, 65, 108, 105, 99, 101, 92, 34, 44, 92, 34, 97, 103, 101, 92, 34, 58, 50, 53, 44, 92, 34, 105, 115, 83, 116, 117, 100, 101, 110, 116, 92, 34, 58, 116, 114, 117, 101, 44, 92, 34, 104, 111, 98, 98, 105, 101, 115, 92, 34, 58, 91, 92, 34, 114, 101, 97, 100, 105, 110, 103, 92, 34, 44, 92, 34, 112, 97, 105, 110, 116, 105, 110, 103, 92, 34, 93, 44, 92, 34, 97, 100, 100, 114, 101, 115, 115, 92, 34, 58, 123, 92, 34, 99, 105, 116, 121, 92, 34, 58, 92, 34, 83, 109, 97, 108, 108, 118, 105, 108, 108, 101, 92, 34, 44, 92, 34, 122, 105, 112, 99, 111, 100, 101, 92, 34, 58, 92, 34, 49, 50, 51, 52, 53, 92, 34, 125, 125, 34}; + String jsonString = "{\"name\":\"Alice\",\"age\":25,\"isStudent\":true,\"hobbies\":[\"reading\",\"painting\"],\"address\":{\"city\":\"Smallville\",\"zipcode\":\"12345\"}}"; + + @Test + void givenByteArray_whenConvertingToJsonUsingJackson_thenJsonString() throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + String actualJsonString = objectMapper.readValue(byteArray, String.class); + + assertEquals(jsonString, actualJsonString); + } + + @Test + void givenByteArray_whenConvertingToJsonUsingGson_thenJsonString() { + Gson gson = new Gson(); + String jsonStringFromByteArray = new String(byteArray, StandardCharsets.UTF_8); + String actualJsonString = gson.fromJson(jsonStringFromByteArray, String.class); + + assertEquals(jsonString, actualJsonString); + } + + @Test + void givenJsonString_whenConvertingToByteArrayUsingJackson_thenByteArray() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + byte[] actualByteArray = objectMapper.writeValueAsBytes(jsonString); + + assertEquals(Arrays.toString(byteArray), Arrays.toString(actualByteArray)); + } + + @Test + void givenJsonString_whenConvertingToByteArrayUsingGson_thenByteArray() { + Gson gson = new Gson(); + byte[] actualByteArray = gson.toJson(jsonString).getBytes(); + + assertEquals(Arrays.toString(byteArray), Arrays.toString(actualByteArray)); + } +}