Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unpacking Set of complex type #281

Open
baraknielsen opened this issue Mar 21, 2020 · 0 comments
Open

Unpacking Set of complex type #281

baraknielsen opened this issue Mar 21, 2020 · 0 comments

Comments

@baraknielsen
Copy link

Hi,

I'm trying to pack and unpack a set of a complex type which include 2 strings,

but when the unpacking occurs I get:

Exception in thread "main" org.msgpack.MessageTypeException: Array is end but readArrayEnd() is not called at org.msgpack.unpacker.UnpackerStack.checkCount(UnpackerStack.java:61) at org.msgpack.unpacker.MessagePackUnpacker.readOne(MessagePackUnpacker.java:72) at org.msgpack.unpacker.MessagePackUnpacker.readString(MessagePackUnpacker.java:502) at Main$1.read(Main.java:28) at Main$1.read(Main.java:18) at org.msgpack.template.AbstractTemplate.read(AbstractTemplate.java:31) at org.msgpack.template.SetTemplate.read(SetTemplate.java:67) at org.msgpack.template.SetTemplate.read(SetTemplate.java:28) at org.msgpack.template.AbstractTemplate.read(AbstractTemplate.java:31) at Main.main(Main.java:43)

From what I saw since its a set type the array size is inserted and when unpacking the sie is removed by one for each read and not considering that it is still on the elemnt type unpacking,

see below is a code to reproduce the issue:

`
import org.msgpack.MessagePack;
import org.msgpack.packer.BufferPacker;
import org.msgpack.packer.Packer;
import org.msgpack.template.AbstractTemplate;
import org.msgpack.template.SetTemplate;
import org.msgpack.unpacker.BufferUnpacker;
import org.msgpack.unpacker.Unpacker;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class Main {

public static void main(String[] args) throws IOException {
    MessagePack messagePack = new MessagePack();
    SetTemplate<B> template = new SetTemplate<>(new AbstractTemplate<B>() {
        @Override
        public void write(Packer pk, B v, boolean required) throws IOException {
            pk.write(v.field1);
            pk.write(v.field2);
        }

        @Override
        public B read(Unpacker u, B to, boolean required) throws IOException {
            B b = new B();
            b.setField1(u.readString());
            b.setField2(u.readString());
            return b;
        }
    });

    Set<B> bSet = new HashSet<>();
    bSet.add(new B("asdasd","asdasd"));
    bSet.add(new B("asdasd2","asdasd2"));

    BufferPacker bufferPacker = messagePack.createBufferPacker();
    template.write(bufferPacker,bSet);
    byte[] bytes = bufferPacker.toByteArray();

    BufferUnpacker bufferUnpacker = messagePack.createBufferUnpacker(bytes);
    Set<B> read = template.read(bufferUnpacker, null);
    System.out.println();

}


public static class B {
    String field1;
    String field2;

    public B(){}

    public B(String field1, String field2) {
        this.field1 = field1;
        this.field2 = field2;
    }

    public String getField1() {
        return field1;
    }

    public B setField1(String field1) {
        this.field1 = field1;
        return this;
    }

    public String getField2() {
        return field2;
    }

    public B setField2(String field2) {
        this.field2 = field2;
        return this;
    }
}

}
`
is this a bug? or am I doing it wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant