镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Is it possible to deserialize a custom class from MessagePackObject #90

Closed

Description

@leonidumanskiy

Hi,
I know how to deserialize a custom class from a Stream:

var serializer = MessagePackSerializer.Get();
var value = serializer.Unpack(stream);

Is it possible to deserialize a class the same way directly from the MessagePackObject and not from the stream?

Activity

  1. yfakariya commented on Jul 5, 2015

    @yfakariya
    Member

    No, there are no Unpackers to support it. But it sounds good.

  2. leonidumanskiy commented on Jul 6, 2015

    @leonidumanskiy
    Author

    I am asking because I have a basic "rpc-like" messaging system, and I want to serialize an object from one of the parameters. So it looks like this:
    {
    "command": "create_user",
    "parameters": {
    "hash": 1234,
    "user": {"user_name": "test_user", "password": "1234", "email": "test@test.com"}
    }
    }

    So I need to receive a command first, check what it is and than deserialize a User object from the second parameter. Is there any "right" way to to it?

  3. yfakariya commented on Aug 16, 2015

    @yfakariya
    Member

    Sorry I missed the additional question. I recommend you to implement following class:

    public class Foo {
      public string Command { get; set; }
      public Dictionary<string, MessagePackObject> Parameters { get; set; }
    }

    and then, you check Parameters and use. I agree that it is not convinient at all, so I'll add ObjectUnpacker like API in future release.

  4. leonidumanskiy commented on Aug 16, 2015

    @leonidumanskiy
    Author

    @yfakariya that's what I have right now.
    And that's a problem I described in my first message - after taking MessagePackObject from the Parameters dictionary, I can't deserialize it into the class/object.
    Instead, I have to write

    var user = new User(Parameters["user_name"].AsString(), Parameters["password"].AsString(), Parameters["email"].AsString());

    which is really annoying. Especially when my class has about 20 different properties :)
    Hope to see an update asap!

  5. TSnake41 commented on Aug 17, 2015

    @TSnake41

    Hi, I've found a solution if you know the type

    MessagePackObject obj;
    Type type;
    object output;
    using (MemoryStream stream = new MemoryStream())
    {
         using (Packer packer = Packer.Create(stream))
         {
                obj.PackToMessage(packer, new PackingOptions());
                stream.Position = 0;
                output = SerializationContext.CreateClassicContext().GetSerializer(type).Unpack(stream);
          }
    }

    You got an object with the right type.

  6. added this to the future milestone on Sep 22, 2016
  7. yfakariya commented on Sep 23, 2016

    @yfakariya
    Member

    I will add helper extension methods in next release. It will be based on @TSnake41's idea. Thank you.

  8. modified the milestones: 0.9, future on Sep 24, 2016
  9. yfakariya commented on Sep 26, 2017

    @yfakariya
    Member

    This was implemented in 0.9.

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

    enhancementRequires or request to feature enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions