PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/telenav/tnt/benchmark/serialization/FastJsonSerializer.java

https://bitbucket.org/seanzxx/serialization-benchmark
Java | 30 lines | 21 code | 9 blank | 0 comment | 0 complexity | 3e6d73dae41c0b205ef6b2a4d2147fd1 MD5 | raw file
  1. package com.telenav.tnt.benchmark.serialization;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.parser.Feature;
  4. import com.alibaba.fastjson.serializer.SerializerFeature;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. public class FastJsonSerializer implements Serializer {
  8. public String getName() {
  9. return "FastJson";
  10. }
  11. public void serialize(Object data, OutputStream outputStream) throws Exception {
  12. byte[] bytes = JSON.toJSONBytes(data, SerializerFeature.DisableCircularReferenceDetect);
  13. outputStream.write(bytes);
  14. }
  15. public Object deserialize(Class clazz, InputStream inputStream) throws Exception {
  16. byte[] bytes = new byte[inputStream.available()];
  17. inputStream.read(bytes);
  18. Object result = JSON.parseObject(bytes, clazz);
  19. return result;
  20. }
  21. }