JSONFixSerializer.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. *
  23. * <p/>
  24. *
  25. * <pre>
  26. *
  27. * Created by IntelliJ IDEA.
  28. * User: zhenqin
  29. * Date: 13-11-13
  30. * Time: 上午8:58
  31. *
  32. * </pre>
  33. *
  34. * @author zhenqin
  35. */
  36. public class JSONFixSerializer<T> implements Serializer<T> {
  37. private static final long serialVersionUID = 1L;
  38. private final StringSerializer serializer;
  39. public JSONFixSerializer() {
  40. serializer = new StringSerializer();
  41. }
  42. public JSONFixSerializer(String charset) {
  43. this.serializer = new StringSerializer(charset);
  44. }
  45. public JSONFixSerializer(StringSerializer serializer) {
  46. this.serializer = serializer;
  47. }
  48. @Override
  49. public byte[] serialize(T o) {
  50. String clazz = o.getClass().getName();
  51. byte[] classString = clazz.getBytes(serializer.getCharset());
  52. int length = classString.length;
  53. byte[] head = Ints.toByteArray(length);
  54. byte[] body = serializer.serialize(JSON.toJSONString(o));
  55. byte[] bytes = new byte[head.length + length + body.length];
  56. System.arraycopy(head, 0, bytes, 0, head.length);
  57. System.arraycopy(classString, 0, bytes, head.length, classString.length);
  58. System.arraycopy(body, 0, bytes, head.length + length, body.length);
  59. return bytes;
  60. }
  61. @Override
  62. public T deserialize(byte[] bytes) {
  63. byte[] head = new byte[4];
  64. System.arraycopy(bytes, 0, head, 0, head.length);
  65. int length = Ints.fromBytes(bytes[0], bytes[1], bytes[2], bytes[3]);
  66. String classString = new String(bytes, 4, length);
  67. try {
  68. return (T) JSON.toJavaObject(
  69. JSON.parseObject(serializer.deserialize(bytes, 4 + length, bytes.length - 4 - length)),
  70. Class.forName(classString));
  71. } catch (ClassNotFoundException e) {
  72. throw new IllegalStateException(e);
  73. }
  74. }
  75. }