Interface JsonUtils


public interface JsonUtils
JsonUtils relies on preview features of the Java platform:
Programs can only use JsonUtils when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
  • 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, @{link Integer}, 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, @{link Integer}, first(Predicate)
    • toTextOr

      static String toTextOr(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode j, String or)
    • 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, @{link Integer}, first(Predicate)
    • toIntOr

      static Integer toIntOr(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode j, Integer or)
    • 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, @{link Integer}, first(Predicate)
    • toLongOr

      static Long toLongOr(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode j, Long or)
    • 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, @{link Integer}, first(Predicate)
    • toBoolOr

      static Boolean toBoolOr(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode j, Boolean or)
    • 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, @{link Integer}, first(Predicate)
    • toDoubleOr

      static Double toDoubleOr(@Nullable @Nullable com.fasterxml.jackson.databind.JsonNode j, Double or)
    • first

      static JsonUtils.JsonNodeFirst first(Predicate<com.fasterxml.jackson.databind.JsonNode> test)
    • jsonEscape

      static String jsonEscape(Object text)
      Returns:
      properly json escaped version of the specified string (without appended quotes)
    • jsonQuote

      static String jsonQuote(Object text)
      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 @{link raw(String)} or @{link rawEscaped(String)}. Collection of JsonUtils.Raw is 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

      static JsonUtils.Raw rawEscaped(String value)
    • raw

      static JsonUtils.Raw raw(String value)
    • 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

      static boolean isJson(String json, com.fasterxml.jackson.databind.ObjectMapper om)
      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 - The ObjectMapper to modify.