問題

我有不止一個類,我想使用Jackson序列化生成Json,例如

 public class A{
  int id;
  String name;
  Object database;
  ... getter and setter
}
 

我需要將json的所有值編碼為base64,所以我像這樣配置物件mapper

 public class Base64Serializer<T> extends StdSerializer<T> {

    private static final long serialVersionUID = 1L;

    protected Base64Serializer(Class<?> t, boolean f) {
        super(t, f);
    }

    @Override
    public void serialize(T value, JsonGenerator jsonGenerator, SerializerProvider arg2) throws IOException {
        String ecnodedOutput = Base64.getEncoder().encodeToString(((String) value).getBytes());
        jsonGenerator.writeString(ecnodedOutput);
    }
}

//Using the base64 Serializer to configure Object mapper 
    SimpleModule module = new SimpleModule();
    module.addSerializer(new Base64Serializer(String.class, false));
    objectMapper.registerModule(module);
    objectMapper.writerWithDefaultPrettyPrinter().writeValue(outputStream,intanceOfClassA);
 

問題是它只編碼String型別,因為序列化器只接受一種型別,是否有任何方法來編碼所有欄位值,(long,int,object等..)我的意思是編碼有關它型別的字串或int的json欄位的所有值?

  最佳答案

您可以使用以下內容:

序列化器

 import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Base64;

public class Base64Serializer<T extends Serializable> extends StdSerializer<T> {

    private static final long serialVersionUID = 1L;

    protected Base64Serializer(Class<?> t, boolean f) {
        super(t, f);
    }

    @Override
    public void serialize(T value, JsonGenerator jsonGenerator, SerializerProvider arg2) throws IOException {
        String ecnodedOutput = Base64.getEncoder().encodeToString(serialize(value));
        jsonGenerator.writeString(ecnodedOutput);
    }

    public static byte[] serialize(Object obj) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(out);
        os.writeObject(obj);
        return out.toByteArray();
    }
}
 

登記和測試:

  public class SerializerTest {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        addSerializers(module, Serializable.class, int.class, double.class, float.class, char.class, byte.class, short.class);
        objectMapper.registerModule(module);
        System.out.println(objectMapper.writeValueAsString(new A(10, "test", Arrays.asList(10000L, "TTTT2"))));
    }

    private static void addSerializers(SimpleModule module, Class... classes) {
        Arrays.stream(classes).forEach(c -> module.addSerializer(new Base64Serializer(c, false)));
    }
}
 

輸出:

 {
  "id": "rO0ABXNyABFqYXZhLmxhbmcuSW50ZWdlchLioKT3gYc4AgABSQAFdmFsdWV4cgAQamF2YS5sYW5nLk51bWJlcoaslR0LlOCLAgAAeHAAAAAK",
  "name": "rO0ABXQABHRlc3Q=",
  "database": "rO0ABXNyABpqYXZhLnV0aWwuQXJyYXlzJEFycmF5TGlzdNmkPL7NiAbSAgABWwABYXQAE1tMamF2YS9sYW5nL09iamVjdDt4cHVyABdbTGphdmEuaW8uU2VyaWFsaXphYmxlO67QCaxT1+1JAgAAeHAAAAACc3IADmphdmEubGFuZy5Mb25nO4vkkMyPI98CAAFKAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAAAAAAACcQdAAFVFRUVDI="
}
 

  相同標籤的其他問題

javajsonjacksonbase64