-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added tests for JsonObject and fixed a couple bugs that the tests found. * Added tests for JsonArray. Improved error messaging for when JsonElement type is not of desired type. * Small helpers added to JsonArray and JsonObject to convert to JsonElement. * Added JsonMapper interface with some default methods implemented for converting between JsonObject or JsonArray into java objects * SolaJson now has several methods that utilize new JsonMapper. Fixed another bug in JsonElement.
- Loading branch information
1 parent
8aa8882
commit fc4b74c
Showing
13 changed files
with
501 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package technology.sola.json; | ||
|
||
import java.util.List; | ||
|
||
public interface JsonMapper<T> { | ||
/** | ||
* Converts object of type T into a {@link JsonObject}. | ||
* | ||
* @param object the object to convert | ||
* @return the {@code JsonObject} representing the converted object | ||
*/ | ||
JsonObject toJson(T object); | ||
|
||
/** | ||
* Converts a {@link List} of T into a {@link JsonArray}. | ||
* | ||
* @param list the {@code List} to convert | ||
* @return the converted {@code JsonArray} | ||
*/ | ||
default JsonArray toJson(List<T> list) { | ||
JsonArray jsonArray = new JsonArray(list.size()); | ||
|
||
list.forEach(item -> jsonArray.add(toJson(item))); | ||
|
||
return jsonArray; | ||
} | ||
|
||
/** | ||
* Converts a {@link JsonObject} into an object of type T. | ||
* | ||
* @param jsonObject the {@code JsonObject} to convert | ||
* @return the converted object of type T | ||
*/ | ||
T toObject(JsonObject jsonObject); | ||
|
||
/** | ||
* Converts a {@link JsonArray} into a {@link List} of desired type T. This assumes that each child of {@code jsonArray} | ||
* is a {@link JsonObject} and of type T. | ||
* | ||
* @param jsonArray the {@code JsonArray} to convert | ||
* @return the converted {@code List<T>} | ||
*/ | ||
default List<T> toList(JsonArray jsonArray) { | ||
return jsonArray.stream().map(item -> toObject(item.asObject())).toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/technology/sola/json/exception/JsonElementTypeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package technology.sola.json.exception; | ||
|
||
public class JsonElementTypeException extends RuntimeException { | ||
public JsonElementTypeException(String desiredType, String actualType) { | ||
super(String.format("JsonElement type is [%s] but attempted to use as [%s]", actualType, desiredType)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package technology.sola.json; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class JsonArrayTest { | ||
private JsonArray root; | ||
|
||
@BeforeEach | ||
void setup() { | ||
root = new JsonArray(); | ||
} | ||
|
||
@Nested | ||
class convenience { | ||
@Test | ||
void objectMethods() { | ||
JsonObject expected = new JsonObject(); | ||
|
||
root.add(expected); | ||
assertEquals(expected, root.getObject(0)); | ||
|
||
root.add((JsonObject) null); | ||
assertTrue(root.isNull(1)); | ||
} | ||
|
||
@Test | ||
void arrayMethods() { | ||
JsonArray expected = new JsonArray(); | ||
|
||
root.add(expected); | ||
assertEquals(expected, root.getArray(0)); | ||
|
||
root.add((JsonArray) null); | ||
assertTrue(root.isNull(1)); | ||
} | ||
|
||
@Test | ||
void stringMethods() { | ||
String expected = ""; | ||
|
||
root.add(expected); | ||
assertEquals(expected, root.getString(0)); | ||
|
||
root.add((String) null); | ||
assertTrue(root.isNull(1)); | ||
} | ||
|
||
@Test | ||
void doubleMethods() { | ||
double expected = 0.0; | ||
|
||
root.add(expected); | ||
assertEquals(expected, root.getDouble(0)); | ||
|
||
root.add((Double) null); | ||
assertTrue(root.isNull(1)); | ||
} | ||
|
||
@Test | ||
void floatMethods() { | ||
float expected = 0.0f; | ||
|
||
root.add(expected); | ||
assertEquals(expected, root.getFloat(0)); | ||
|
||
root.add((Float) null); | ||
assertTrue(root.isNull(1)); | ||
} | ||
|
||
@Test | ||
void longMethods() { | ||
long expected = 0L; | ||
|
||
root.add(expected); | ||
assertEquals(expected, root.getLong(0)); | ||
|
||
root.add((Long) null); | ||
assertTrue(root.isNull(1)); | ||
} | ||
|
||
@Test | ||
void intMethods() { | ||
int expected = 0; | ||
|
||
root.add(expected); | ||
assertEquals(expected, root.getInt(0)); | ||
|
||
root.add((Integer) null); | ||
assertTrue(root.isNull(1)); | ||
} | ||
|
||
@Test | ||
void booleanMethods() { | ||
boolean expected = true; | ||
|
||
root.add(expected); | ||
assertEquals(expected, root.getBoolean(0)); | ||
|
||
root.add((Boolean) null); | ||
assertTrue(root.isNull(1)); | ||
} | ||
|
||
@Test | ||
void nullMethods() { | ||
root.addNull(); | ||
assertTrue(root.isNull(0)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package technology.sola.json; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import technology.sola.json.exception.JsonElementTypeException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class JsonElementTest { | ||
@Test | ||
void whenAccessingIncorrectType_shouldThrowException() { | ||
JsonElement jsonElement = new JsonElement(); | ||
|
||
assertThrows(JsonElementTypeException.class, jsonElement::asArray); | ||
} | ||
} |
Oops, something went wrong.