
com.alibaba.fastjson.serializer.ByteBufferCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fastjson Show documentation
Show all versions of fastjson Show documentation
Fastjson is a JSON processor (JSON parser + JSON generator) written in Java
The newest version!
package com.alibaba.fastjson.serializer;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
public class ByteBufferCodec implements ObjectSerializer, ObjectDeserializer {
public final static ByteBufferCodec instance = new ByteBufferCodec();
@Override
public T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
ByteBufferBean bean = parser.parseObject(ByteBufferBean.class);
return (T) bean.byteBuffer();
}
@Override
public int getFastMatchToken() {
return JSONToken.LBRACKET;
}
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
ByteBuffer byteBuf = (ByteBuffer) object;
byte[] array = byteBuf.array();
SerializeWriter out = serializer.out;
out.write('{');
out.writeFieldName("array");
out.writeByteArray(array);
out.writeFieldValue(',', "limit", byteBuf.limit());
out.writeFieldValue(',', "position", byteBuf.position());
out.write('}');
}
public static class ByteBufferBean {
public byte[] array;
public int limit;
public int position;
public ByteBuffer byteBuffer() {
ByteBuffer buf = ByteBuffer.wrap(array);
buf.limit(limit);
buf.position(position);
return buf;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy