0

我是使用 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");
            }
        }
    }

我现在的任务是跳过读取数据,该数据的模式已经被删除。我正在查看接口的handleUnknownField方法Input

有没有人已经经历过这个?

4

1 回答 1

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 对其他人有益!

于 2012-07-23T04:04:12.580 回答