Skip to content

Commit

Permalink
Addressing review issues for better code.
Browse files Browse the repository at this point in the history
  • Loading branch information
caalador committed Feb 7, 2025
1 parent 6f31c10 commit b6ad9b4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* For internal use only. May be renamed or removed in a future release.
*
* @author Vaadin Ltd
* @since 1.0
* @since 24.7
*/
public class JacksonCodec {
/**
Expand Down Expand Up @@ -217,7 +217,8 @@ public static JsonNode encodeWithoutTypeInfo(Object value) {
/**
* Helper for decoding any "primitive" value that is directly supported in
* JSON. Supported values types are {@link String}, {@link Number},
* {@link Boolean}, {@link JsonNode}. <code>null</code> is also supported.
* {@link Boolean}, {@link JsonNode}.
* {@link com.fasterxml.jackson.databind.node.NullNode} is also supported.
*
* @param json
* the JSON value to decode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* For internal use only. May be renamed or removed in a future release.
*
* @author Vaadin Ltd
* @since 1.0
* @since 24.7
*/
public final class JacksonUtils {

Expand Down Expand Up @@ -89,7 +89,7 @@ public static ArrayNode createArrayNode() {
* Collects a stream of JSON values to a JSON array.
*
* @author Vaadin Ltd
* @since 1.0
* @since 24.7
*/
private static final class ArrayNodeCollector
implements Collector<JsonNode, ArrayNode, ArrayNode> {
Expand Down Expand Up @@ -172,18 +172,45 @@ public static boolean jsonEquals(JsonNode a, JsonNode b) {
};
}

/**
* Compare String value of two JsonNode values.
*
* @param a
* Value one
* @param b
* Value two
* @return {@code true} if text content equals
*/
public static boolean stringEqual(JsonNode a, JsonNode b) {
assert a.getNodeType() == JsonNodeType.STRING;
assert b.getNodeType() == JsonNodeType.STRING;
return a.asText().equals(b.asText());
}

/**
* Compare boolean value of two JsonNode values.
*
* @param a
* Value one
* @param b
* Value two
* @return {@code true} if text boolean equals
*/
public static boolean booleanEqual(JsonNode a, JsonNode b) {
assert a.getNodeType() == JsonNodeType.BOOLEAN;
assert b.getNodeType() == JsonNodeType.BOOLEAN;
return a.asBoolean() == b.asBoolean();
}

/**
* Compare number value of two JsonNode values.
*
* @param a
* Value one
* @param b
* Value two
* @return {@code true} if number content equals
*/
public static boolean numbersEqual(JsonNode a, JsonNode b) {
assert a.getNodeType() == JsonNodeType.NUMBER;
assert b.getNodeType() == JsonNodeType.NUMBER;
Expand Down Expand Up @@ -216,13 +243,9 @@ private static boolean jsonObjectEquals(JsonNode a, JsonNode b) {
return true;
}

public static List<String> getKeys(JsonNode a) {
public static List<String> getKeys(JsonNode node) {
List<String> keys = new ArrayList<>();
for (Iterator<Map.Entry<String, JsonNode>> it = a.fields(); it
.hasNext();) {
Map.Entry<String, JsonNode> entry = it.next();
keys.add(entry.getKey());
}
node.fieldNames().forEachRemaining(keys::add);
return keys;
}

Expand Down Expand Up @@ -391,7 +414,7 @@ public static ObjectNode mapToJson(Map<String, ?> map) {
public static <T> T readToObject(ObjectNode jsonObject, Class<T> tClass) {
Objects.requireNonNull(jsonObject, CANNOT_CONVERT_NULL_TO_OBJECT);
try {
return objectMapper.readValue(jsonObject.toString(), tClass);
return objectMapper.treeToValue(jsonObject, tClass);
} catch (JsonProcessingException e) {
throw new JsonDecodingException(
"Error converting JsonObject to " + tClass.getName(), e);
Expand All @@ -410,13 +433,7 @@ public static <T> T readToObject(ObjectNode jsonObject, Class<T> tClass) {
* type of result instance
*/
public static <T> T readValue(ObjectNode jsonValue, Class<T> tClass) {
Objects.requireNonNull(jsonValue, CANNOT_CONVERT_NULL_TO_OBJECT);
try {
return objectMapper.readValue(jsonValue.toString(), tClass);
} catch (JsonProcessingException e) {
throw new JsonDecodingException(
"Error converting ObjectNode to " + tClass.getName(), e);
}
return readToObject(jsonValue, tClass);
}

/**
Expand All @@ -434,7 +451,7 @@ public static <T> T readValue(ObjectNode jsonValue,
TypeReference<T> typeReference) {
Objects.requireNonNull(jsonValue, CANNOT_CONVERT_NULL_TO_OBJECT);
try {
return objectMapper.readValue(jsonValue.toString(), typeReference);
return objectMapper.treeToValue(jsonValue, typeReference);
} catch (JsonProcessingException e) {
throw new JsonDecodingException("Error converting ObjectNode to "
+ typeReference.getType().getTypeName(), e);
Expand Down

0 comments on commit b6ad9b4

Please # to comment.