Class JacksonUtils

java.lang.Object
uno.anahata.asi.internal.JacksonUtils

public final class JacksonUtils extends Object
A generic utility class for Jackson-based JSON operations. It uses the centrally configured ObjectMapper from SchemaProvider to ensure consistency across the application.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final com.fasterxml.jackson.databind.ObjectMapper
    A vanilla, unconfigured ObjectMapper used strictly for parsing raw, standard JSON structures.
    private static final com.fasterxml.jackson.databind.ObjectMapper
    The primary ObjectMapper instance configured via SchemaProvider for consistent serialization.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    parse(String json, Class<T> clazz)
    Parses a JSON string into a Java object of the specified class.
    static <T> T
    parse(String json, Type type)
    Parses a JSON string into a Java object of the specified type.
    static String
    Serializes an object to a pretty-printed JSON string.
    static String
    Pretty-prints a JSON string.
    static void
    purifySchema(com.fasterxml.jackson.databind.JsonNode node)
    Recursively removes redundant and token-heavy fields from a JsonNode.
    static String
    Serializes any Java object into a compact, flat JSON string using the shared mapper.
    static Object
    Converts any Java object (POJO, Map, List, Primitive) into a "pure" JSON-safe structure containing only standard Java types (Map, List, String, Number, Boolean, null).
    static <T> T
    toPojo(Object o, Type type)
    Converts a "pure" JSON-safe structure (Map, List, etc.) into a rich Java POJO.

    Methods inherited from class Object

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

    • MAPPER

      private static final com.fasterxml.jackson.databind.ObjectMapper MAPPER
      The primary ObjectMapper instance configured via SchemaProvider for consistent serialization.
    • DEFAULT_MAPPER

      private static final com.fasterxml.jackson.databind.ObjectMapper DEFAULT_MAPPER
      A vanilla, unconfigured ObjectMapper used strictly for parsing raw, standard JSON structures.
  • Constructor Details

    • JacksonUtils

      public JacksonUtils()
  • Method Details

    • toJsonPrimitives

      public static Object toJsonPrimitives(Object o)
      Converts any Java object (POJO, Map, List, Primitive) into a "pure" JSON-safe structure containing only standard Java types (Map, List, String, Number, Boolean, null).

      This is the "Final Gate" used to ensure that external libraries with "greedy" reflection-based serializers (like the Google GenAI library) never encounter a complex POJO that might trigger forbidden API calls or recursion.

      Parameters:
      o - The object to purify.
      Returns:
      A pure JSON-safe representation of the object.
    • toPojo

      public static <T> T toPojo(Object o, Type type)
      Converts a "pure" JSON-safe structure (Map, List, etc.) into a rich Java POJO.
      Type Parameters:
      T - The target type.
      Parameters:
      o - The source object (usually a Map or List from the API).
      type - The target reflection Type.
      Returns:
      The enriched POJO.
    • parse

      public static <T> T parse(String json, Type type)
      Parses a JSON string into a Java object of the specified type.
      Type Parameters:
      T - The target type.
      Parameters:
      json - The JSON string to parse.
      type - The target reflection Type.
      Returns:
      The parsed object.
      Throws:
      IllegalArgumentException - if parsing fails.
    • parse

      public static <T> T parse(String json, Class<T> clazz)
      Parses a JSON string into a Java object of the specified class.
      Type Parameters:
      T - The target type.
      Parameters:
      json - The JSON string to parse.
      clazz - The target class.
      Returns:
      The parsed object.
    • purifySchema

      public static void purifySchema(com.fasterxml.jackson.databind.JsonNode node)
      Recursively removes redundant and token-heavy fields from a JsonNode. Strips: 'exampleSetFlag', 'types', and 'jsonSchema'. Also removes child descriptions if they match the parent description (redundancy optimization).
      Parameters:
      node - The JsonNode to purify.
    • serialize

      public static String serialize(Object o)
      Serializes any Java object into a compact, flat JSON string using the shared mapper.

      Unlike prettyPrint(Object), this method does not inject newlines or indentation whitespaces, making it the mathematically accurate choice for token-counting and wire transmission.

      Parameters:
      o - The object to serialize.
      Returns:
      The flat JSON string, or a fallback error string if serialization fails.
    • prettyPrint

      public static String prettyPrint(Object o)
      Serializes an object to a pretty-printed JSON string.
      Parameters:
      o - The object to serialize.
      Returns:
      A formatted JSON string, or an error message if serialization fails.
    • prettyPrintJsonString

      public static String prettyPrintJsonString(String jsonString)
      Pretty-prints a JSON string.
      Parameters:
      jsonString - The JSON string to pretty-print.
      Returns:
      A formatted JSON string, or an error message if parsing or serialization fails.