protostuff

protostuff是什么

Google的protobuf因为其编码后体积小,序列化/反序列化性能好,被广泛使用。但是protobuf需要编写.proto文件,再通过protobuf转换成对应的java代码,非常不好维护。

protostuff就是为了解决这个痛点而产生的。通过protostuff,不需要编写.proto文件,只需要编写普通的java bean就可以使用protobuf的序列化/反序列化。

protostuff有两个东西:Schema和Runtime Schema

Schema

包含了以下几个内容:

  • 对象的序列化
  • 对象的反序列化
  • 对象里必须的字段校验
  • 字段名称和字段序号的映射(protobuf里,消息的每个字段都有序号)

Schema的生成有两种方式:

  • 通过protostuff的Runtime,可以通过反射在运行时生成并且缓存使用
  • 自己手写(继承Schema接口)

User.java


public class User
{
    
    private String firstName;
    private String lastName;
    private String email;
    private List friends;
    
    public User()
    {
    
    }
    
    public User(String email)
    {
        this.email = email;
    }
    
    // getters and setters
}


UserSchema.java



public class UserSchema implements Schema
{

    public boolean isInitialized(User user)
    {
        return user.getEmail() != null;
    }

    public void mergeFrom(Input input, User user) throws IOException
    {
        while(true)
        {
            int number = input.readFieldNumber(this);
            switch(number)
            {
                case 0:
                    return;
                case 1:
                    user.setEmail(input.readString());
                    break;
                case 2:
                    user.setFirstName(input.readString());
                    break;
                case 3:
                    user.setLastName(input.readString());
                    break;
                case 4:
                    if(message.friends == null)
                        message.friends = new ArrayList();
                    message.friends.add(input.mergeObject(null, this));
                    break;
                default:
                    input.handleUnknownField(number, this);
            }
        }
    }

    public void writeTo(Output output, User user) throws IOException
    {
        if(user.getEmail() == null)
            throw new UninitializedMessageException(user, this);
        
        output.writeString(1, user.getEmail(), false);
        
        if(user.getFirstName() != null)
            output.writeString(2, user.getFirstName(), false);
        
        if(user.getLastName() != null)
            output.writeString(3, user.getLastName(), false);

        if(message.friends != null)
        {
            for(User friend : message.friends)
            {
                if(friend != null)
                    output.writeObject(4, friend, this, true);
            }
        }
    }

    public User newMessage()
    {
        return new User();
    }

    public Class typeClass()
    {
        return User.class;
    }

    public String messageName()
    {
        return User.class.getSimpleName();
    }
    
    public String messageFullName()
    {
        return User.class.getName();
    }
    
    // the mapping between the field names to the field numbers.
    
    public String getFieldName(int number)
    {
        switch(number)
        {
            case 1:
                return "email";
            case 2:
                return "firstName";
            case 3:
                return "lastName";
            case 4:
                return "friends";
            default:
                return null;
        }
    }

    public int getFieldNumber(String name)
    {
        Integer number = fieldMap.get(name);
        return number == null ? 0 : number.intValue();
    }
    
    private static final HashMap fieldMap = new HashMap();    
    static
    {
        fieldMap.put("email", 1);
        fieldMap.put("firstName", 2);
        fieldMap.put("lastName", 3);
        fieldMap.put("friends", 4);
    }
}

Runtime Schema

提供了java 对象与多种格式的转换(protostuff、protobuf、json、xml等等)。
下面以protobuf为例:

Foo foo = new Foo("foo", 1, 3.5);

// this is lazily created and cached by RuntimeSchema
// so its safe to call RuntimeSchema.getSchema(Foo.class) over and over
// The getSchema method is also thread-safe
Schema schema = RuntimeSchema.getSchema(Foo.class);
LinkedBuffer buffer = getApplicationBuffer();
/* -------- protobuf -------- (requires protostuff-core module) */
// ser
try
{
   byte[] protobuf = ProtobufIOUtil.toByteArray(foo, schema, buffer);
}
finally
{
   buffer.clear();
}
// deser
Foo f = schema.newMessage();
ProtobufIOUtil.mergeFrom(protobuf, f, schema);

你可能感兴趣的:(protostuff)