Class JsonOps

java.lang.Object
com.github.darksoulq.abyssallib.common.serialization.DynamicOps<com.fasterxml.jackson.databind.JsonNode>
com.github.darksoulq.abyssallib.common.serialization.ops.JsonOps

public class JsonOps extends DynamicOps<com.fasterxml.jackson.databind.JsonNode>
An implementation of DynamicOps for Jackson's JsonNode tree model. This class allows Codecs to serialize and deserialize Java objects to and from JSON structures.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final JsonOps
    Singleton instance of JsonOps to avoid unnecessary allocations.
    final com.fasterxml.jackson.databind.ObjectMapper
     
  • Method Summary

    Modifier and Type
    Method
    Description
    com.fasterxml.jackson.databind.JsonNode
    createBoolean(boolean value)
    Creates a BooleanNode from a boolean.
    com.fasterxml.jackson.databind.JsonNode
    createDouble(double value)
    Creates a DoubleNode from a double.
    com.fasterxml.jackson.databind.JsonNode
    createFloat(float value)
    Creates a FloatNode from a float.
    com.fasterxml.jackson.databind.JsonNode
    createInt(int value)
    Creates an IntNode from an integer.
    com.fasterxml.jackson.databind.JsonNode
    createList(List<com.fasterxml.jackson.databind.JsonNode> elements)
    Creates an ArrayNode from a list of JSON nodes.
    com.fasterxml.jackson.databind.JsonNode
    createLong(long value)
    Creates a LongNode from a long.
    com.fasterxml.jackson.databind.JsonNode
    createMap(Map<com.fasterxml.jackson.databind.JsonNode, com.fasterxml.jackson.databind.JsonNode> map)
    Creates an ObjectNode from a map of JSON nodes.
    com.fasterxml.jackson.databind.JsonNode
    Creates a TextNode from a String.
    com.fasterxml.jackson.databind.JsonNode
    Returns the representation of a null value in JSON.
    getBooleanValue(com.fasterxml.jackson.databind.JsonNode input)
    Extracts a Boolean from a JSON node if it is a boolean.
    getDoubleValue(com.fasterxml.jackson.databind.JsonNode input)
    Extracts a Double from a JSON node if it is a number.
    getFloatValue(com.fasterxml.jackson.databind.JsonNode input)
    Extracts a Float from a JSON node if it is a number.
    getIntValue(com.fasterxml.jackson.databind.JsonNode input)
    Extracts an Integer from a JSON node if it is a number.
    Optional<List<com.fasterxml.jackson.databind.JsonNode>>
    getList(com.fasterxml.jackson.databind.JsonNode input)
    Converts a JSON array node into a List of nodes.
    getLongValue(com.fasterxml.jackson.databind.JsonNode input)
    Extracts a Long from a JSON node if it is a number.
    Optional<Map<com.fasterxml.jackson.databind.JsonNode, com.fasterxml.jackson.databind.JsonNode>>
    getMap(com.fasterxml.jackson.databind.JsonNode input)
    Converts a JSON object node into a Map of nodes.
    getStringValue(com.fasterxml.jackson.databind.JsonNode input)
    Extracts a String from a JSON node if it is textual.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • INSTANCE

      public static final JsonOps INSTANCE
      Singleton instance of JsonOps to avoid unnecessary allocations.
    • mapper

      public final com.fasterxml.jackson.databind.ObjectMapper mapper
  • Method Details

    • createString

      public com.fasterxml.jackson.databind.JsonNode createString(String value)
      Creates a TextNode from a String.
      Specified by:
      createString in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      value - The string value.
      Returns:
      A JSON text node.
    • createInt

      public com.fasterxml.jackson.databind.JsonNode createInt(int value)
      Creates an IntNode from an integer.
      Specified by:
      createInt in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      value - The int value.
      Returns:
      A JSON integer node.
    • createLong

      public com.fasterxml.jackson.databind.JsonNode createLong(long value)
      Creates a LongNode from a long.
      Specified by:
      createLong in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      value - The long value.
      Returns:
      A JSON long node.
    • createFloat

      public com.fasterxml.jackson.databind.JsonNode createFloat(float value)
      Creates a FloatNode from a float.
      Specified by:
      createFloat in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      value - The float value.
      Returns:
      A JSON float node.
    • createDouble

      public com.fasterxml.jackson.databind.JsonNode createDouble(double value)
      Creates a DoubleNode from a double.
      Specified by:
      createDouble in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      value - The double value.
      Returns:
      A JSON double node.
    • createBoolean

      public com.fasterxml.jackson.databind.JsonNode createBoolean(boolean value)
      Creates a BooleanNode from a boolean.
      Specified by:
      createBoolean in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      value - The boolean value.
      Returns:
      A JSON boolean node.
    • createList

      public com.fasterxml.jackson.databind.JsonNode createList(List<com.fasterxml.jackson.databind.JsonNode> elements)
      Creates an ArrayNode from a list of JSON nodes.
      Specified by:
      createList in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      elements - The list of nodes to include.
      Returns:
      A JSON array node.
    • createMap

      public com.fasterxml.jackson.databind.JsonNode createMap(Map<com.fasterxml.jackson.databind.JsonNode, com.fasterxml.jackson.databind.JsonNode> map)
      Creates an ObjectNode from a map of JSON nodes. Keys are converted to strings via JsonNode.asText().
      Specified by:
      createMap in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      map - The map of key-value nodes.
      Returns:
      A JSON object node.
    • getStringValue

      public Optional<String> getStringValue(com.fasterxml.jackson.databind.JsonNode input)
      Extracts a String from a JSON node if it is textual.
      Specified by:
      getStringValue in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      input - The JSON node.
      Returns:
      An Optional containing the string, or empty.
    • getIntValue

      public Optional<Integer> getIntValue(com.fasterxml.jackson.databind.JsonNode input)
      Extracts an Integer from a JSON node if it is a number.
      Specified by:
      getIntValue in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      input - The JSON node.
      Returns:
      An Optional containing the integer, or empty.
    • getLongValue

      public Optional<Long> getLongValue(com.fasterxml.jackson.databind.JsonNode input)
      Extracts a Long from a JSON node if it is a number.
      Specified by:
      getLongValue in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      input - The JSON node.
      Returns:
      An Optional containing the long, or empty.
    • getFloatValue

      public Optional<Float> getFloatValue(com.fasterxml.jackson.databind.JsonNode input)
      Extracts a Float from a JSON node if it is a number.
      Specified by:
      getFloatValue in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      input - The JSON node.
      Returns:
      An Optional containing the float, or empty.
    • getDoubleValue

      public Optional<Double> getDoubleValue(com.fasterxml.jackson.databind.JsonNode input)
      Extracts a Double from a JSON node if it is a number.
      Specified by:
      getDoubleValue in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      input - The JSON node.
      Returns:
      An Optional containing the double, or empty.
    • getBooleanValue

      public Optional<Boolean> getBooleanValue(com.fasterxml.jackson.databind.JsonNode input)
      Extracts a Boolean from a JSON node if it is a boolean.
      Specified by:
      getBooleanValue in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      input - The JSON node.
      Returns:
      An Optional containing the boolean, or empty.
    • getList

      public Optional<List<com.fasterxml.jackson.databind.JsonNode>> getList(com.fasterxml.jackson.databind.JsonNode input)
      Converts a JSON array node into a List of nodes.
      Specified by:
      getList in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      input - The JSON node.
      Returns:
      An Optional containing the list, or empty if not an array.
    • getMap

      public Optional<Map<com.fasterxml.jackson.databind.JsonNode, com.fasterxml.jackson.databind.JsonNode>> getMap(com.fasterxml.jackson.databind.JsonNode input)
      Converts a JSON object node into a Map of nodes. Keys are wrapped in TextNodes.
      Specified by:
      getMap in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Parameters:
      input - The JSON node.
      Returns:
      An Optional containing the map, or empty if not an object.
    • empty

      public com.fasterxml.jackson.databind.JsonNode empty()
      Returns the representation of a null value in JSON.
      Specified by:
      empty in class DynamicOps<com.fasterxml.jackson.databind.JsonNode>
      Returns:
      NullNode.instance.