Package sk.softec.util.json
Interface JsonUtils
public interface JsonUtils
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordstatic final record -
Method Summary
Static MethodsModifier and TypeMethodDescriptionstatic voiddisableObjectMapperLimits(com.fasterxml.jackson.databind.ObjectMapper om) Disables limits on nesting depth, number length, and string length for the provided ObjectMapper.static JsonUtils.JsonNodeFirststatic booleanJSON(com.fasterxml.jackson.databind.ObjectMapper om) Json template processor.static StringjsonEscape(Object text) static Stringstatic JsonUtils.Rawstatic JsonUtils.RawrawEscaped(String value) static @Nullable com.fasterxml.jackson.databind.JsonNodestatic Stream<com.fasterxml.jackson.databind.JsonNode> streamOfChildren(com.fasterxml.jackson.databind.JsonNode j) static @Nullable Booleanstatic Booleanstatic @Nullable Doublestatic DoubletoDoubleOr(@Nullable com.fasterxml.jackson.databind.JsonNode j, Double or) static @Nullable Integerstatic Integerstatic @Nullable Longstatic Longstatic @Nullable Stringstatic String
-
Method Details
-
select
@Nullable static @Nullable com.fasterxml.jackson.databind.JsonNode select(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode j, Object... selectors) - Returns:
- selected json node as node or null if no match. Selectors can be
String, @{linkInteger},first(Predicate)
-
toText
@Nullable static @Nullable String toText(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode j, Object... selectors) - Returns:
- selected json node as string or null if no match. Selectors can be
String, @{linkInteger},first(Predicate)
-
toTextOr
-
toInt
@Nullable static @Nullable Integer toInt(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode j, Object... selectors) - Returns:
- selected json node as int or null if no match. Selectors can be
String, @{linkInteger},first(Predicate)
-
toIntOr
-
toLong
@Nullable static @Nullable Long toLong(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode j, Object... selectors) - Returns:
- selected json node as long or null if no match. Selectors can be
String, @{linkInteger},first(Predicate)
-
toLongOr
-
toBool
@Nullable static @Nullable Boolean toBool(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode j, Object... selectors) - Returns:
- selected json node as boolean or null if no match. Selectors can be
String, @{linkInteger},first(Predicate)
-
toBoolOr
-
toDouble
@Nullable static @Nullable Double toDouble(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode j, Object... selectors) - Returns:
- selected json node as double or null if no match. Selectors can be
String, @{linkInteger},first(Predicate)
-
toDoubleOr
-
first
-
jsonEscape
- Returns:
- properly json escaped version of the specified string (without appended quotes)
-
jsonQuote
- Returns:
- properly json escaped version of the specified string (with appended quotes)
-
JSON
static StringTemplate.ProcessorPREVIEW<String,RuntimeException> JSON(com.fasterxml.jackson.databind.ObjectMapper om) Json template processor. Takes json template and creates compact-printed valid json string or throws exception if malformed.
Allows capturing values/variables/arbitrary java expressions. These are evaluated and converted using jackson object mapper.
var x = computeX(); JSON. """ { "x": \{x} } """
Takes care of escaping, quoting and formatting values. Do not provide quotes for string values, {} for objects, [] for lists.JSON. """ { "x": \{str} // correct "x": "\{str}" // incorrect "x": \{array} // correct "x": [\{array}] // incorrect "x": \{obj} // correct "x": {\{obj}} // incorrect } """
To avoid transformation of the value to json and pass it as-is, use @{linkraw(String)} or @{linkrawEscaped(String)}. Collection ofJsonUtils.Rawis supported as well. This allows changing arbitrary part of the json.JSON."\"12\{raw("3")}45\"" // "12345" JSON."\{listOf(raw(JSON."\{1}"), raw("2"))}" // [ 1, 2 ]
The resulting string is trimmed so these are equivalent:JSON."{}" JSON. """ {} """ JSON.""" {} """ JSON.""" {}""" JSON.""" {}""" -
rawEscaped
-
raw
-
streamOfChildren
static Stream<com.fasterxml.jackson.databind.JsonNode> streamOfChildren(com.fasterxml.jackson.databind.JsonNode j) - Returns:
- stream of children json nodes of the specified json node
-
isJson
- Returns:
- whether string is valid json
-
disableObjectMapperLimits
static void disableObjectMapperLimits(com.fasterxml.jackson.databind.ObjectMapper om) Disables limits on nesting depth, number length, and string length for the provided ObjectMapper.- Parameters:
om- TheObjectMapperto modify.
-
JsonUtilswhen preview features are enabled.