Skip to content

ConcurrentHashMap does not register #20

Closed

Description

@ivolo

How do I serialize ConcurrentHashMap? Gives the following exception when you try to register it:

https://groups.google.com/forum/?fromgroups=#!topic/msgpack/KPx-dP3hnE0

Activity

  1. muga commented on Oct 14, 2012

    @muga
    Member

    Hi,

    Thank you for contacting us.

    You can serialize ConcurrentHashMap objects with Map template as follows.

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    import org.msgpack.MessagePack;
    import org.msgpack.packer.Packer;
    import org.msgpack.template.Template;
    import org.msgpack.template.Templates;
    import org.msgpack.unpacker.Unpacker;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            MessagePack msgpack = new MessagePack();
    
            Template<Map<String, String>> tmpl = Templates.tMap(Templates.TString, Templates.TString);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            Packer packer = msgpack.createPacker(out);
    
            ConcurrentHashMap<String, String> src = new ConcurrentHashMap<String, String>();
            src.put("k1", "v1");
            src.put("k2", "v2");
    
            tmpl.write(packer, src);
            byte[] bytes = out.toByteArray();
    
            ConcurrentHashMap<String, String> dst = new ConcurrentHashMap<String, String>();
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            Unpacker unpacker = msgpack.createUnpacker(in);
            tmpl.read(unpacker, dst);
    
            System.out.println(dst);
        }
    }

    Thanks,
    Muga

  2. ivolo commented on Oct 14, 2012

    @ivolo
    Author

    Thank you!

  3. added a commit that references this issue on May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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