Package com.cercalia.sdk.util
Class CercaliaResponseParser
java.lang.Object
com.cercalia.sdk.util.CercaliaResponseParser
Utility class for parsing Cercalia API JSON responses.
Cercalia's JSON format has several idiosyncrasies due to its XML heritage:
- Attributes are prefixed with '@' (e.g., "@id").
- Values can be primitive strings or objects with keys like "value" or "$valor".
- Single-item lists are often returned as objects instead of arrays.
-
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable com.fasterxml.jackson.databind.JsonNodegetArrayElement(@Nullable com.fasterxml.jackson.databind.JsonNode node, int index) Get an element from an array node at the specified index.static intgetArraySize(@Nullable com.fasterxml.jackson.databind.JsonNode node) Get the size of an array node, treating single objects as arrays of size 1.static @Nullable StringgetCercaliaAttr(@Nullable com.fasterxml.jackson.databind.JsonNode node, String key) Extract attribute value from Cercalia object.static @Nullable StringgetCercaliaValue(@Nullable com.fasterxml.jackson.databind.JsonNode node) Extract value from Cercalia value object.static booleanisArrayOrSingle(@Nullable com.fasterxml.jackson.databind.JsonNode node) Check if a JSON node represents an array or should be treated as one.static doubleparseCoordinate(@Nullable String value, String name) Parse a coordinate value from string to double.static @Nullable DoubleparseDoubleOrNull(@Nullable String value) Safely parse a double value, returning null if parsing fails.static @Nullable IntegerparseIntOrNull(@Nullable String value) Safely parse an integer value, returning null if parsing fails.static @Nullable LongparseLongOrNull(@Nullable String value) Safely parse a long value, returning null if parsing fails.
-
Method Details
-
getCercaliaAttr
@Nullable public static @Nullable String getCercaliaAttr(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode node, String key) Extract attribute value from Cercalia object. Handles both @attr and attr formats.- Parameters:
node- the JSON node to extract from.key- the attribute key (without @ prefix).- Returns:
- the attribute value, or
nullif not found.
-
getCercaliaValue
@Nullable public static @Nullable String getCercaliaValue(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode node) Extract value from Cercalia value object. Handles multiple formats: { "value": "..." }, { "$valor": "..." }, { "@value": "..." }, or plain string.- Parameters:
node- the JSON node to extract from.- Returns:
- the value, or
nullif not found.
-
parseCoordinate
Parse a coordinate value from string to double. Throws an exception if the value is null (strict coordinates rule).- Parameters:
value- the string value to parse.name- the coordinate name (for error messages).- Returns:
- the parsed double value.
- Throws:
IllegalArgumentException- if value is null or invalid.
-
parseDoubleOrNull
Safely parse a double value, returning null if parsing fails.- Parameters:
value- the string value to parse.- Returns:
- the parsed Double, or
nullif parsing fails.
-
parseIntOrNull
Safely parse an integer value, returning null if parsing fails.- Parameters:
value- the string value to parse.- Returns:
- the parsed Integer, or
nullif parsing fails.
-
parseLongOrNull
Safely parse a long value, returning null if parsing fails.- Parameters:
value- the string value to parse.- Returns:
- the parsed Long, or
nullif parsing fails.
-
isArrayOrSingle
public static boolean isArrayOrSingle(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode node) Check if a JSON node represents an array or should be treated as one.- Parameters:
node- the JSON node to check.- Returns:
trueif the node is an array or a single object.
-
getArraySize
public static int getArraySize(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode node) Get the size of an array node, treating single objects as arrays of size 1.- Parameters:
node- the JSON node.- Returns:
- the array size, or 0 if null.
-
getArrayElement
@Nullable public static @Nullable com.fasterxml.jackson.databind.JsonNode getArrayElement(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode node, int index) Get an element from an array node at the specified index. Handles both array nodes and single object nodes.- Parameters:
node- the JSON node.index- the index.- Returns:
- the element at the index, or
nullif out of bounds.
-