JSONFixSerializer.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2010-2011 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.sdyc.ndmp.protobuf.serializer;
  17. import com.alibaba.fastjson.JSON;
  18. import com.google.common.primitives.Ints;
  19. /**
  20. * Java Serialization Redis strserializer.
  21. * Delegates to the default (Java based) strserializer in Spring 3.
  22. * <p/>
  23. * <pre>
  24. *
  25. * Created by IntelliJ IDEA.
  26. * User: zhenqin
  27. * Date: 13-11-13
  28. * Time: 上午8:58
  29. * To change this template use File | Settings | File Templates.
  30. *
  31. * </pre>
  32. *
  33. * @author zhenqin
  34. */
  35. public class JSONFixSerializer<T> implements Serializer<T> {
  36. private static final long serialVersionUID = 1L;
  37. private final StringSerializer serializer;
  38. public JSONFixSerializer() {
  39. serializer = new StringSerializer();
  40. }
  41. public JSONFixSerializer(String charset) {
  42. this.serializer = new StringSerializer(charset);
  43. }
  44. public JSONFixSerializer(StringSerializer serializer) {
  45. this.serializer = serializer;
  46. }
  47. @Override
  48. public byte[] serialize(T o) {
  49. String clazz = o.getClass().getName();
  50. byte[] classString = clazz.getBytes(serializer.getCharset());
  51. int length = classString.length;
  52. byte[] head = Ints.toByteArray(length);
  53. byte[] body = serializer.serialize(JSON.toJSONString(o));
  54. byte[] bytes = new byte[head.length + length + body.length];
  55. System.arraycopy(head, 0, bytes, 0, head.length);
  56. System.arraycopy(classString, 0, bytes, head.length, classString.length);
  57. System.arraycopy(body, 0, bytes, head.length + length, body.length);
  58. return bytes;
  59. }
  60. @Override
  61. public T deserialize(byte[] bytes) {
  62. byte[] head = new byte[4];
  63. System.arraycopy(bytes, 0, head, 0, head.length);
  64. int length = Ints.fromBytes(bytes[0], bytes[1], bytes[2], bytes[3]);
  65. String classString = new String(bytes, 4, length);
  66. try {
  67. return (T) JSON.toJavaObject(
  68. JSON.parseObject(serializer.deserialize(bytes, 4 + length, bytes.length - 4 - length)),
  69. Class.forName(classString));
  70. } catch (ClassNotFoundException e) {
  71. throw new IllegalStateException(e);
  72. }
  73. }
  74. }