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

Already on GitHub? Sign in to your account

Create the tempalte for combined type when Unpacking. #59

Closed
is opened this Issue Nov 19, 2013 · 7 comments

Comments

Projects
None yet
2 participants

is commented Nov 19, 2013

I will deserialize a dict which type is
Map<String, ? extend MessagePackable> in my code.

I can't get tListTemplate with correct elementTemplate,
and I can't access msgpack.lookup function through
Unpacker object directly.

I suggest to add new read function or expose template
lookup function in Unpacker interface.

is commented Nov 19, 2013

The following code shows my temporary solution.

  @Message
  public class Entry {
    public Long code;
    public String attribute;
    public String comment;
    @Ignore
    public Object ref;
    @Ignore
    public int order;
  }

  private static Template entryListTemplate;

  static {
    TemplateRegistry registry = new TemplateRegistry(null);
    entryListTemplate = new ListTemplate(registry.lookup(Entry.class));
  }


  public void readFrom(Unpacker u) throws IOException {
    name = u.readString();
    this.entries = (List)u.read(entryListTemplate);
    build();
  }
Member

komamitsu commented Nov 29, 2013

Hi,

Sorry for my late reply.

I can't get tListTemplate with correct elementTemplate, and I can't access msgpack.lookup function through Unpacker object directly.

In order to clarify this issue, can you show us how you want to write as an example code (not your temporary solution)?

Thanks

is commented Nov 29, 2013

Hi,

I prefer to provide a looking up template by type method in Unpacker interface. then the code will look like:

class XXXXX implement MessagePackable {

......
public void readFrom(Unpacker u) throws IOException {
  int elements = u.readArrayBegin();
  Template listTmpl = Templates.tList(u.__LOOKUP__(Entry.class));
  entries = u.read(listTmpl);
  u.readArrayEnd();
}

......

}

The change will help to build complex template when unpacking, I think.

Thank you for attension..

Member

komamitsu commented Nov 30, 2013

How about using MessagePack#lookup(Class type) ?

  MessagePack msgpack = new MessagePack();
      :
  BufferUnpacker unpacker = msgpack.createBufferUnpacker(packer.toByteArray());

  Template entryListTemplate = Templates.tList(msgpack.lookup(Entry.class));

  List list = (List)unpacker.read(entryListTemplate);

is commented Nov 30, 2013

MessagePackable's readFrom gets only Unpacker instance, doesn't get the certain MessagePack instance.

In my prior comment, I have to initilize the template instance in class static init code. I don't think the means is good enough.

Member

komamitsu commented Dec 1, 2013

I see. I missed you used MessagePackable#readFrom().

I think you can write like the following code. But you want to use Unpacker#read(ListTemplate), right?

public void readFrom(Unpacker u) throws IOException {
    int n = u.readArrayBegin();
    for (int i = 0; i < n; i++)
        this.entries.add(u.read(Entry.class));
    u.readArrayEnd();
}

is commented Dec 4, 2013

Yeah, I want use ListTemplate/MapTemplate, like ObjectArrayTemplate.

@is is closed this Dec 15, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment