[]
        
(Showing Draft Content)

IJsonSerializer

Interface IJsonSerializer


public interface IJsonSerializer
Represents a serializer and deserializer for custom types used in JSON import and export.

Implement this interface to convert application-specific objects to JSON strings and to reconstruct those objects when a workbook is converted to or from JSON. This interface is typically used with Workbook.setTagJsonSerializer(IJsonSerializer) or Workbook.setValueJsonSerializer(IJsonSerializer).


 IJsonSerializer serializer = new IJsonSerializer() {
     private final com.google.gson.Gson gson = new com.google.gson.Gson();

     @Override
     public String serialize(Object value) {
         return gson.toJson(java.util.Collections.singletonMap("value", value));
     }

     @Override
     public Object deserialize(String json) {
         return gson.fromJson(json, java.util.Map.class);
     }
 };
 Workbook.setValueJsonSerializer(serializer);
 
  • Method Summary

    Modifier and Type
    Method
    Description
    Deserializes a JSON string to an object.
    Serializes an object to a JSON string.
  • Method Details

    • serialize

      String serialize(Object value)
      Serializes an object to a JSON string.

      Implement this method to convert a custom value into JSON text that can be used during JSON import and export. The returned string should be compatible with the corresponding deserialize(String) implementation.

      
       IJsonSerializer serializer = new IJsonSerializer() {
           private final com.google.gson.Gson gson = new com.google.gson.Gson();
      
           @Override
           public String serialize(Object value) {
               return gson.toJson(java.util.Collections.singletonMap("value", value));
           }
      
           @Override
           public Object deserialize(String json) {
               return gson.fromJson(json, java.util.Map.class);
           }
       };
       String json = serializer.serialize("A");
       
      Parameters:
      value - The object to serialize. null handling is implementation-defined.
      Returns:
      The JSON string representation of the object.
    • deserialize

      Object deserialize(String json)
      Deserializes a JSON string to an object.

      Implement this method to reconstruct an application-specific object from its JSON representation. The concrete return type depends on the serializer implementation and how the JSON content is interpreted.

      
       IJsonSerializer serializer = new IJsonSerializer() {
           private final com.google.gson.Gson gson = new com.google.gson.Gson();
      
           @Override
           public String serialize(Object value) {
               return gson.toJson(java.util.Collections.singletonMap("value", value));
           }
      
           @Override
           public Object deserialize(String json) {
               return gson.fromJson(json, java.util.Map.class);
           }
       };
       Object value = serializer.deserialize("{\"value\":\"A\"}");
       
      Parameters:
      json - The JSON string to deserialize.
      Returns:
      The object created from the specified JSON string.