引入jar包
<dependency> <groupId>com.dyuproject.protostuff</groupId> <artifactId>protostuff-api</artifactId> <version>1.0.10</version> </dependency> <dependency> <groupId>com.dyuproject.protostuff</groupId> <artifactId>protostuff-core</artifactId> <version>1.0.10</version> </dependency> <dependency> <groupId>com.dyuproject.protostuff</groupId> <artifactId>protostuff-runtime</artifactId> <version>1.0.10</version> </dependency>
需要序列化的类
package demo.protostuff; import java.util.List; /** * Created by yuyufeng on 2017/8/24. */ public class User { private String firstName; private String lastName; private String email; private List<User> friends; public User() { } public User(String email) { this.email = email; } // getters and setters public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public List<User> getFriends() { return friends; } public void setFriends(List<User> friends) { this.friends = friends; } @Override public String toString() { return "User{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", email='" + email + '\'' + ", friends=" + friends + '}'; } }
开始使用
package demo.protostuff; import com.dyuproject.protostuff.LinkedBuffer; import com.dyuproject.protostuff.ProtostuffIOUtil; import com.dyuproject.protostuff.runtime.RuntimeSchema; import java.util.ArrayList; import java.util.List; /** * Created by yuyufeng on 2017/8/24. */ public class Main { private static RuntimeSchema<User> schema = RuntimeSchema.createFrom(User.class); public static void main(String[] args) { User user1 = new User(); user1.setEmail("4165@qq.com"); user1.setFirstName("yu"); user1.setLastName("yufeng"); List<User> users = new ArrayList<>(); users.add(new User("6514@qq.com")); user1.setFriends(users); byte[] bytes = ProtostuffIOUtil.toByteArray(user1,schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE)); User user2 = schema.newMessage(); ProtostuffIOUtil.mergeFrom(bytes,user2,schema); System.out.println(user2); } }
工具类的提取
package demo.protostuff;/** * Created by yuyufeng on 2017/8/24. */ import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import com.dyuproject.protostuff.LinkedBuffer;import com.dyuproject.protostuff.ProtostuffIOUtil;import com.dyuproject.protostuff.Schema;import com.dyuproject.protostuff.runtime.RuntimeSchema;public class ProtostuffUtil {private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();private static <T> Schema<T> getSchema (Class<T> clazz) {@SuppressWarnings ("unchecked" ) Schema<T> schema = (Schema<T>) cachedSchema.get(clazz);if (schema == null ) { schema = RuntimeSchema.getSchema(clazz);if (schema != null ) { cachedSchema.put(clazz, schema); } }return schema; }/** * 序列化 * * @param obj * @return */ public static <T> byte [] serializer (T obj) {@SuppressWarnings ("unchecked" ) Class<T> clazz = (Class<T>) obj.getClass(); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);try { Schema<T> schema = getSchema(clazz);return ProtostuffIOUtil.toByteArray(obj, schema, buffer); } catch (Exception e) {throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } }/** * 反序列化 * * @param data * @param clazz * @return */ public static <T> T deserializer (byte [] data, Class<T> clazz) {try { T obj = clazz.newInstance(); Schema<T> schema = getSchema(clazz); ProtostuffIOUtil.mergeFrom(data, obj, schema);return obj; } catch (Exception e) {throw new IllegalStateException(e.getMessage(), e); } } }