类 JsonUtils

java.lang.Object
io.agentscope.core.util.JsonUtils

public final class JsonUtils extends Object
Utility class for accessing the global JsonCodec instance.

This class provides a centralized way to access JSON serialization/deserialization functionality throughout the framework. By default, it uses JacksonJsonCodec, but users can replace it with a custom implementation.

Usage:


 // Basic usage
 JsonUtils.getJsonCodec().toJson(obj);
 JsonUtils.getJsonCodec().fromJson(json, MyClass.class);

 // Cache reference for frequent calls
 JsonCodec codec = JsonUtils.getJsonCodec();
 codec.toJson(obj1);
 codec.toJson(obj2);

 // Replace with custom implementation
 JsonUtils.setJsonCodec(new MyCustomJsonCodec());
 
另请参阅:
  • 方法详细资料

    • getJsonCodec

      public static JsonCodec getJsonCodec()
      Get the global JsonCodec instance.
      返回:
      the JsonCodec instance
    • setJsonCodec

      public static void setJsonCodec(JsonCodec jsonCodec)
      Set a custom JsonCodec implementation.

      This allows users to replace the default Jackson-based implementation with their own implementation (e.g., Gson, Fastjson, etc.).

      参数:
      jsonCodec - the custom JsonCodec implementation
      抛出:
      IllegalArgumentException - if jsonCodec is null
    • resetToDefault

      public static void resetToDefault()
      Reset to the default JacksonJsonCodec implementation.

      This is useful for testing or when you want to restore the default behavior.