protostuff 正在参加 2021 年度 OSC 中国开源项目评选,请投票支持!
protostuff 在 2021 年度 OSC 中国开源项目评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
2021 年度 OSC 中国开源项目评选 正在火热进行中,快来投票支持你喜欢的开源项目!
2021 年度 OSC 中国开源项目评选 >>> 中场回顾
protostuff 获得 2021 年度 OSC 中国开源项目评选「最佳人气项目」 !
授权协议 Apache
开发语言 Java
操作系统 跨平台
软件类型 开源软件
所属分类 程序开发常用工具包
开源组织
地区 不详
投 递 者 juqkai
适用人群 未知
收录时间 2010-11-01

软件简介

protostuff 是一个支持各种格式的一个序列化Java类库,包括 JSON、XML、YAML等格式。

示例代码:

public class UserSchema implements Schema<User>
{

    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<User>();
                    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<User> 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<String,Integer> fieldMap = new HashMap<String,Integer>();    
    static
    {
        fieldMap.put("email", 1);
        fieldMap.put("firstName", 2);
        fieldMap.put("lastName", 3);
        fieldMap.put("friends", 4);
    }
}
展开阅读全文

代码

评论

点击引领话题📣 发布并加入讨论🔥
暂无内容
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
暂无内容
0 评论
30 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部