我是使用Protostuff读取/写入数据的团队的一员。
这是从mergeFrom
类扩展的类的Schema
方法:
@Override
public void mergeFrom(Input input, Entity entity) throws IOException {
for(int number = input.readFieldNumber(this); ; number = input.readFieldNumber(this))
{
if ( number == 0 ) {
break;
} else if ( number == FIELD_DEFINITION_ID ) {
Schema idmSchema = entity.getSchema();
// Definition id
int definitionId = input.readUInt32();
NodeDefinition defn = idmSchema.getById(definitionId);
if ( defn == null ) {
throw new ProtostuffException("Invalid definition id "+definitionId);
}
Node<?> node = defn.createNode();
entity.add(node);
// Node
readAndCheckFieldNumber(input, FIELD_NODE);
input.mergeObject(node, getSchema(node.getClass()));
} else if ( number == FIELD_CHILD_NODE_STATE ){
//Node state
int intState = input.readInt32();
State state = State.parseState(intState);
readAndCheckFieldNumber(input, FIELD_CHILD_DEFINITION_ID);
int childDefnId = input.readInt32();
Schema schema = entity.getSchema();
NodeDefinition childDefn = schema.getById(childDefnId);
entity.childStates.put(childDefn.getName(), state);
} else {
throw new ProtostuffException("Unexpected field number");
}
}
}
我目前的任务是skipp读取数据,该数据的架构已被删除。我正在查看Input
接口的handleUnknownField方法。
有没有人经历过这个?
答案 0 :(得分:1)
解决方案确实非常基础。查看此参考:Schema,并检查mergeFrom代码:
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);
}
}
}
我想我应该使用input.handleUnknownField(number, this);
。并且它正确地解决了我的问题:
...
} else if ( number == FIELD_DEFINITION_ID ) {
Schema idmSchema = entity.getSchema();
// Definition id
int definitionId = input.readUInt32();
NodeDefinition defn = idmSchema.getById(definitionId);
if ( defn == null ) {
input.handleUnknownField(number, this);
continue;
}
Node<?> node = defn.createNode();
entity.add(node);
// Node
readAndCheckFieldNumber(input, FIELD_NODE);
input.mergeObject(node, getSchema(node.getClass()));
}
...
谢谢,我希望这个QA对其他人有益!