/* * Copyright 2010-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sdyc.ndmp.protobuf.serializer; import com.alibaba.fastjson.JSON; /** * Java Serialization Redis strserializer. * Delegates to the default (Java based) strserializer in Spring 3. * *
 *
 * Created by IntelliJ IDEA.
 * User: zhenqin
 * Date: 13-11-13
 * Time: 上午8:58
 * To change this template use File | Settings | File Templates.
 *
 * 
* * @author zhenqin */ public class JSONSerializer implements Serializer { private static final long serialVersionUID = 1L; /** * String byte */ private StringSerializer serializer = new StringSerializer(); /** * 类对象, 用于Object json to Object 的转换 */ private Class reverseClazz; public JSONSerializer(Class clazz) { this.reverseClazz = clazz; } public JSONSerializer(String classString) { try { this.reverseClazz = (Class)Class.forName(classString); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(e); } } @Override public byte[] serialize(T o) { return serializer.serialize(JSON.toJSONString(o)); } @Override public T deserialize(byte[] bytes) { return JSON.toJavaObject( JSON.parseObject(serializer.deserialize(bytes)), reverseClazz); } public Class getReverseClazz() { return reverseClazz; } }