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

Serialising anonymous/immutable types #68

Closed

Description

@drewnoakes

Is it possible to serialise an anonymous type?

For example:

new { Foo = 1, Bar = "Hello" }

This would be very useful for us.

Unfortunately, GetSerializer<T> doesn't seem to work, presumably as the properties of anonymous types are read only. Because the returned serializer can both serialise and deserialise, it's not possible to build it. However we only need the serialiser bit.

(cc @lufka)

Activity

  1. yfakariya commented on Feb 28, 2015

    @yfakariya
    Member

    In my unstanding, anon types cannot be take away from declaring method, so I cannot imagine use case when anon type serialization is really useful. Would you tell me when and how do you use anon types serialization?

  2. drewnoakes commented on Mar 2, 2015

    @drewnoakes
    ContributorAuthor

    Anonymous classes are regular classes and their type can be known and passed around. This is especially easy when using generic methods:

    public void Log<T>(LogLevel level, T data)
    {
        _serializationContext.GetSerializer<T>()
            .Pack(_logStream, new { Level = level, Data = data });
    }
    

    That's actually pretty close to our use case. The logging API should support arbitrary data from users, so long as it only contains serialisable types. However the anonymous type simply isn't serialisable.

    The problem seems to be that msgpack-cli uses one set of restrictions across both encodable/decodable types. This means types must always have read/write properties in order to work with the lib. This seems like a significant limitation, especially when you use immutability extensively in your code.

    Of course it's not limited to anonymous types:

    public class Foo { public string Name { get; private set; } /*...*/ }
    
    _serializationContext.GetSerializer<Foo>() // bang!
    

    In this case, as with anonymous types, Foo is definitely serialisable, however it is not deserialisable. If There is definitely a strong use-case for treating serialiser and deserialiser types separately.

  3. changed the title [-]Serialising anonymous types[/-] [+]Serialising anonymous/immutable types[/+] on Mar 2, 2015
  4. yfakariya commented on Mar 2, 2015

    @yfakariya
    Member

    Thank you for great suggestion. I understand that there are use cases that need 'asymmetric' serializer which only available for serialization or deserialization. Although I'm interested in asymmetric serializers feature, I cannot tuckle to it soon because I'm working on another issue.

  5. drewnoakes commented on Mar 3, 2015

    @drewnoakes
    ContributorAuthor

    This would certainly be very useful for us. I'm not sure how hard it would be to retrofit this to the current API. I suppose the current serialiser objects could implement both IPacker<T> and IUnpacker<T> or something like that, and then it would also be possible to generate just one or the other:

    _serializationContext.GetPacker<Foo>().Pack(new Foo()); // ok
    
    _serializationContext.GetUnpacker<Foo>().Unpack(new Foo()); // bang!
    
  6. added this to the 0.7.0 milestone on Dec 23, 2015
  7. modified the milestones: future, 0.7.0 on Jul 24, 2016
  8. modified the milestones: 0.9, future on Sep 22, 2016
  9. yfakariya commented on Sep 26, 2017

    @yfakariya
    Member

    This was implemented in 0.9.

  10. drewnoakes commented on Sep 28, 2017

    @drewnoakes
    ContributorAuthor

    Great news!

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