com.fitbur.jackson.databind.ser.std.TokenBufferSerializer Maven / Gradle / Ivy
package com.fitbur.jackson.databind.ser.std;
import java.io.IOException;
import java.lang.reflect.Type;
import com.fitbur.jackson.core.*;
import com.fitbur.jackson.databind.JavaType;
import com.fitbur.jackson.databind.JsonMappingException;
import com.fitbur.jackson.databind.JsonNode;
import com.fitbur.jackson.databind.SerializerProvider;
import com.fitbur.jackson.databind.annotation.JacksonStdImpl;
import com.fitbur.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fitbur.jackson.databind.jsontype.TypeSerializer;
import com.fitbur.jackson.databind.util.TokenBuffer;
/**
* We also want to directly support serialization of {@link TokenBuffer};
* and since it is part of core package, it can not implement
* {@link com.fitbur.jackson.databind.JsonSerializable}
* (which is only included in the mapper package)
*/
@JacksonStdImpl
@SuppressWarnings("serial")
public class TokenBufferSerializer
extends StdSerializer
{
public TokenBufferSerializer() { super(TokenBuffer.class); }
@Override
public void serialize(TokenBuffer value, JsonGenerator jgen, SerializerProvider provider)
throws IOException
{
value.serialize(jgen);
}
/**
* Implementing typed output for contents of a TokenBuffer is very tricky,
* since we do not know for sure what its contents might look like (or, rather,
* we do know when serializing, but not necessarily when deserializing!)
* One possibility would be to check the current token, and use that to
* determine if we would output JSON Array, Object or scalar value.
*
* Note that we just claim it is scalar; this should work ok and is simpler
* than doing introspection on both serialization and deserialization.
*/
@Override
public final void serializeWithType(TokenBuffer value, JsonGenerator jgen, SerializerProvider provider,
TypeSerializer typeSer) throws IOException
{
typeSer.writeTypePrefixForScalar(value, jgen);
serialize(value, jgen, provider);
typeSer.writeTypeSuffixForScalar(value, jgen);
}
@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint)
{
// Not 100% sure what we should say here: type is basically not known.
// This seems like closest approximation
return createSchemaNode("any", true);
}
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
throws JsonMappingException
{
// Not 100% sure what we should say here: type is basically not known.
// This seems like closest approximation
visitor.expectAnyFormat(typeHint);
}
}