All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.noear.nami.coder.jackson.ThrowableJsonSerializer Maven / Gradle / Ivy

There is a newer version: 3.0.5-M3
Show newest version
package org.noear.nami.coder.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

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

/**
 * @author noear
 * @since 1.4
 */
public class ThrowableJsonSerializer extends JsonSerializer {

    @Override
    public void serialize(Throwable exception, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(byteArrayOutputStream);
        oo.writeObject(exception);
        oo.flush();
        oo.close();
        byte[] bytes = byteArrayOutputStream.toByteArray();
        gen.writeRawValue("\"" + new String(bytes) + "\"");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy