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:
    • withClassLoader

      public static void withClassLoader(ClassLoader classloader)
      Sets the ClassLoader used to resolve types during deserialization of AgenticScope state.

      By default, the deserializer uses the parent ClassLoader. In environments where domain types are loaded by a different classloader , you must set the appropriate ClassLoader before calling fromJson(String), otherwise deserialization will fail with a ClassNotFoundException.

      The setting is process-wide: it applies to every subsequent deserialization in the JVM.

      Parameters:
      classloader - the class loader to use for resolving types
      See Also:
    • registerForDeserializationPackageOf

      public static void registerForDeserializationPackageOf(Class<?> type)
      Convenience method that registers the package of the given class for deserialization and sets the class loader used to resolve types.

      This is equivalent to calling:

      AgenticScopeSerializer.allowDeserializationPackagePrefix(type.getPackageName() + ".");
      AgenticScopeSerializer.withClassLoader(type.getClassLoader());
      

      Use this when your agents store domain objects in the AgenticScope state and you want to allow all types in the same package with a single call:

      AgenticScopeSerializer.registerForDeserializationPackageOf(Order.class);
      

      Registrations are process-wide and permanent. Register the package before the fromJson(String) call that encounters its types, otherwise that call fails with UnserializableAgenticScopeException.

      Parameters:
      type - a class whose package will be allowed and whose class loader will be used for type resolution
      See Also: