Jackson 将对象序列化为 JSON 到 base64(没有死循环)

Jackson serialize Object to JSON to base64 (without endless loop)

有没有一种简单的方法可以使用 Jackson 将对象序列化为 base64 编码 JSON? (对象 -> JSON -> base64)

我尝试使用自定义 StdSerializer,但这(当然)会导致无限循环:

class MySerializer extends StdSerializer<Foo> {
  public void serialize(Foo value, JsonGenerator gen, SerializerProvider provider) {
    StringWriter stringWriter = new StringWriter();
    JsonGenerator newGen = gen.getCodec().getFactory().createGenerator(stringWriter);
    gen.getCodec().getFactory().getCodec().writeValue(newGen, value);
    String json = stringWriter.toString();
    String base64 = new String(Base64.getEncoder().encode(json.getBytes()));
    gen.writeString(base64);
  }
}

解决方法是将所有字段复制到另一个 class 并将该 class 用于中间表示:

class TmpFoo {
  public String field1;
  public int field2;
  // ...
}

class MySerializer extends StdSerializer<Foo> {
  public void serialize(Foo value, JsonGenerator gen, SerializerProvider provider) {
    TmpFoo tmp = new TmpFoo();
    tmp.field1 = value.field1;
    tmp.field2 = value.field2;
    // etc.

    StringWriter stringWriter = new StringWriter();
    JsonGenerator newGen = gen.getCodec().getFactory().createGenerator(stringWriter);
    gen.getCodec().getFactory().getCodec().writeValue(newGen, tmp); // here "tmp" instead of "value"
    String json = stringWriter.toString();
    String base64 = new String(Base64.getEncoder().encode(json.getBytes()));
    gen.writeString(base64);
  }
}

不需要创建 new ObjectMapper,因为我需要默认 ObjectMapper 的所有已注册模块和序列化程序。

我希望有一些更简单的方法来实现这一目标。


编辑:示例

第 1 步:Java 对象

class Foo {
  String field1 = "foo";
  int field2 = 42;
}

第 2 步:JSON

{"field1":"foo","field2":42}

第 3 步:Base64

eyJmaWVsZDEiOiJmb28iLCJmaWVsZDIiOjQyfQ==

您可以将现有对象转换为地图,而不是创建新对象。就像下面的例子

import static java.nio.charset.StandardCharsets.UTF_8;

public class FooSerializer extends StdSerializer<Foo> {

    public FooSerializer() {
        super(Foo.class);
    }

    @Override
    public void serialize(Foo foo, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
        try {
            ObjectMapper mapper = (ObjectMapper) jsonGenerator.getCodec();
            var map = toMap(foo); // if you need class info for deserialization than use toMapWithClassInfo
            String json = mapper.writeValueAsString(map);
            jsonGenerator.writeString(Base64.getEncoder().encodeToString(json.getBytes(UTF_8)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Map<String, Object> toMap(Object o) throws Exception {
        Map<String, Object> result = new HashMap<>();
        Field[] declaredFields = o.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            result.put(field.getName(), field.get(o));
        }
        return result;
    }


    public static Map<String, Object> toMapWithClassInfo(Object obj) throws Exception {
        Map<String, Object> result = new HashMap<>();
        BeanInfo info = Introspector.getBeanInfo(obj.getClass());
        for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
            Method reader = pd.getReadMethod();
            if (reader != null)
                result.put(pd.getName(), reader.invoke(obj));
        }
        return result;
    }
}

我提供了 2 种转换为地图的方法:有和没有 class 信息。选择一个适用于您的问题的。

序列化对象 jackson search @JsonValue 方法。可以在Fooclass.

中添加encodedJsonString注解的@JsonValue方法

试试这个:

@Getter
@Setter
public class Foo implements Serializable {

    private static final long serialVersionUID = 1L;

    public String field1;
    public int field2;

    @JsonValue
    public String toEncodedJsonString() {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            new ObjectOutputStream(baos).writeObject(this);
            return org.apache.commons.codec.binary.Base64.encodeBase64String(baos.toByteArray());
        }catch (Exception ex){

        }
        return null;
    }
}

根据this site,有一个解决方法可以避免这个递归问题:

When we define a custom serializer, Jackson internally overrides the original BeanSerializer instance [...] our SerializerProvider finds the customized serializer every time, instead of the default one, and this causes an infinite loop.

A possible workaround is using BeanSerializerModifier to store the default serializer for the type Folder before Jackson internally overrides it.

如果我对解决方法的理解正确,您的 Serializer 应该如下所示:

class FooSerializer extends StdSerializer<Foo> {

    private final JsonSerializer<Object> defaultSerializer;

    public FooSerializer(JsonSerializer<Object> defaultSerializer) {
        super(Foo.class);
        this.defaultSerializer = defaultSerializer;
    }

    @Override
    public void serialize(Foo value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator tempGen = provider.getGenerator().getCodec().getFactory().createGenerator(stringWriter);
        defaultSerializer.serialize(value, tempGen, provider);

        tempGen.flush();

        String json = stringWriter.toString();
        String base64 = new String(Base64.getEncoder().encode(json.getBytes()));
        gen.writeString(base64);
    }
}

除了序列化器,还需要一个修饰符:

public class FooBeanSerializerModifier extends BeanSerializerModifier {

    @Override
    public JsonSerializer<?> modifySerializer(
      SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {

        if (beanDesc.getBeanClass().equals(Foo.class)) {
            return new FooSerializer((JsonSerializer<Object>) serializer);
        }
        return serializer;
    }
}

示例模块:

ObjectMapper mapper = new ObjectMapper();

SimpleModule module = new SimpleModule();
module.setSerializerModifier(new FooBeanSerializerModifier());

mapper.registerModule(module);

编辑:

我添加了 flush() 来刷新 JsonGenerator tempGen。 另外,我用 JUnit 创建了一个最小的测试环境,它用 Foo 验证了你的示例:可以找到 github 存储库 here.


编辑:备选方案 2

另一个(简单的)选项是使用带有泛型的包装器 class:

public class Base64Wrapper<T> {

    private final T wrapped;

    private Base64Wrapper(T wrapped) {
        this.wrapped = wrapped;
    }

    public T getWrapped() {
        return this.wrapped;
    }

    public static <T> Base64Wrapper<T> of(T wrapped) {
        return new Base64Wrapper<>(wrapped);
    }
}
public class Base64WrapperSerializer extends StdSerializer<Base64Wrapper> {


    public Base64WrapperSerializer() {
        super(Base64Wrapper.class);
    }

    @Override
    public void serialize(Base64Wrapper value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator tempGen = provider.getGenerator().getCodec().getFactory().createGenerator(stringWriter);
        provider.defaultSerializeValue(value.getWrapped(), tempGen);
        tempGen.flush();

        String json = stringWriter.toString();
        String base64 = new String(Base64.getEncoder().encode(json.getBytes()));
        gen.writeString(base64);
    }
}

一个示例用例是:

final ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(new Base64WrapperSerializer());
mapper.registerModule(module);

final Foo foo = new Foo();
final Base64Wrapper<Foo> base64Wrapper = Base64Wrapper.of(foo);
final String base64Json = mapper.writeValueAsString(base64Wrapper);

这个例子可以在 this GitHub (branch: wrapper) repo 中找到,用 JUnit 测试从你的 foo 例子中验证你的 BASE64 字符串。