protostuff java_protostuff

protostuff java_protostuff软件简介protostuff是一个支持各种格式的一个序列化Java类库,包括JSON、XML、YAML等格式。示例代码:publicclassUserSchemaimplementsSchema{publicbooleanisInitialized(Useruser){returnuser.getEmail()!=null;}publicvoidmergeFrom(In…

大家好,又见面了,我是你们的朋友全栈君。

软件简介

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

示例代码:

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);

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/133738.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • manifest文件使用(manifest文件作用)

    解决难以打开MANIFEST文件的问题打开MANIFEST文件的麻烦MicrosoftNotepad已删除你尝试加载MANIFEST文件并收到错误,例如“%%os%%无法打开MANIFEST文件扩展名”。通常情况下,这意味着MicrosoftNotepad没有安装在%%os%%上。由于您的操作系统不知道如何处理此文件,因此无法通过双击将其打开。提示:如果你…

    2022年4月11日
    180
  • mac连上wifi却上不了网「建议收藏」

    mac连上wifi却上不了网「建议收藏」mac连上wifi却上不了网总结起来就是先删干净wifi再重新新建一个wifi选项电脑自动分配一个地址等于是格式化wifi了

    2022年6月25日
    29
  • java内存模型JMM「建议收藏」

    java内存模型JMM「建议收藏」java内存模型jmm(javamemorymodel)规范,他规范了java虚拟机与计算机内存如何协调工作,他规定了一个线程如何及何时看到其他线程修改过的变量的值,以及在必须时,如何同步的访问共享变量。jmm内存分配的概念:堆heap:优点:运行时数据区,动态分配内存大小,有gc;,缺点:因为要在运行时动态分配,所以存取速度慢,对象存储在堆上,静态类型的变量跟着类的定义一起存储在…

    2022年5月30日
    25
  • docker部署jenkins安装使用教程_docker封装python程序

    docker部署jenkins安装使用教程_docker封装python程序前言使用docker安装jenkins环境,jenkins构建的workspace目录默认是在容器里面构建的,如果我们想执行python3的代码,需进容器内部安装python3的环境。进jenki

    2022年7月31日
    3
  • Git Windows下载安装详细教程

    Git Windows下载安装详细教程首先登录Git官网:https://git-scm.com/1.点击Download下载2.选择版本默认64位3.点击安装程序(这里是我之前下载的2.23版本)4.进入安装界面点击next到选择组件界面(这里1是在桌面创建图标,2是决定在所有控制台窗口中使用TrueType字体和是否每天检查Git是否有Windows更新的)根据需要选择,这里我没有选择第二个。点击next到选择gi…

    2022年5月29日
    50
  • Javascript Array forEach()中无法return和break,代替方法some()与every()「建议收藏」

    Javascript Array forEach()中无法return和break,代替方法some()与every()「建议收藏」我们都知道for循环里要跳出整个循环是使用break,但在数组中用forEach循环如要退出整个循环使用break会报错,使用return也不能跳出循环。使用break将会报错:vararr=[1,2,3,4,5];varnum=3;arr.forEach(function(v){if(v==num){break;}console.log

    2022年7月13日
    12

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号