Class AgenticScopeSerializer

java.lang.Object
dev.langchain4j.agentic.scope.AgenticScopeSerializer

public class AgenticScopeSerializer extends Object
Utility class for serializing AgenticScope objects to JSON format.
  • Method Details

    • toJson

      public static String toJson(DefaultAgenticScope agenticScope)
      Serializes a AgenticScope into a JSON string.
      Parameters:
      agenticScope - AgenticScope to be serialized.
      Returns:
      A JSON string with the contents of the AgenticScope.
      See Also:
    • fromJson

      public static DefaultAgenticScope fromJson(String json)
      Deserializes a JSON string into a AgenticScope object.
      Parameters:
      json - JSON string to be deserialized.
      Returns:
      A AgenticScope object constructed from the JSON.
      See Also:
    • allowDeserializationPackagePrefix

      public static void allowDeserializationPackagePrefix(String packagePrefix)
      Registers an additional type prefix (typically a package name) to be allowed during deserialization of AgenticScope state.

      By default, the deserializer allows standard JDK types (java.util.*, java.math.*, primitive wrappers). If your agents store domain objects in the agentic scope state, you must register their package prefix before deserialization occurs:

      AgenticScopeSerializer.allowDeserializationPackagePrefix("com.acme.");
      

      Registrations are process-wide and permanent: they apply to every subsequent deserialization in the JVM and cannot be removed. In tests this means a prefix registered by one test stays allowed for all later tests.

      Registration is not retroactive: a type must be registered before the fromJson(String) call that encounters it, otherwise that call fails with UnserializableAgenticScopeException. It takes effect immediately for all subsequent deserializations.

      Parameters:
      packagePrefix - the package prefix to allow (e.g. "com.acme.")
      Throws:
      IllegalArgumentException - if the prefix is null or empty
      See Also:
    • allowDeserializationType

      public static void allowDeserializationType(Class<?> type)
      Registers a single class to be allowed during deserialization of AgenticScope state.
      AgenticScopeSerializer.allowDeserializationType(Order.class);
      

      Registrations are process-wide and permanent: they apply to every subsequent deserialization in the JVM and cannot be removed. Register the type before the fromJson(String) call that encounters it, otherwise that call fails with UnserializableAgenticScopeException.

      Parameters:
      type - the class to allow
      See Also: