Skip to content

Map serialization cannot find templates for Object classes #4

@mgenov

Description

@mgenov

Hello,
It seems that map serialization fails when Map contains an Object values. Here is the full stack trace and the code that is causing this error:

org.msgpack.MessageTypeException: Cannot find template for class java.lang.Object class. Try to add @message annotation to the class or call MessagePack.register(Type).
at org.msgpack.template.TemplateRegistry.lookup(TemplateRegistry.java:220)
at org.msgpack.template.TemplateRegistry.lookupGenericTypeImpl(TemplateRegistry.java:258)
at org.msgpack.template.TemplateRegistry.lookupGenericType(TemplateRegistry.java:230)
at org.msgpack.template.TemplateRegistry.lookup(TemplateRegistry.java:180)

package com.mycompany.app;

import org.junit.Test;
import org.msgpack.MessagePack;
import org.msgpack.annotation.Message;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static junit.framework.Assert.assertEquals;

/**
 * Verifies whether object that contains map string -> object could be serialized.
 */
public class SerializeEntityWithMapTest {

  @Message
  public static class Entity {
    public String name;
    public Map<String, Object> properties = new HashMap<String, Object>();
  }

  @Test
  public void happyPath() throws IOException {
    MessagePack pack = new MessagePack();

    Entity e = new Entity();
    e.name = "test";
    e.properties.put("Test","aaaa");

    byte[] content = pack.write(e);

    Entity actual = new MessagePack().read(content, Entity.class);

    System.out.println(actual.properties);
    assertEquals(1,actual.properties.size());
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions