Skip to content

support private constructor, members #40

Closed

Description

@alexvaluyskiy

Akka.NET has a lot of singleton objects with static Instance property and with a private constructor.

public sealed class RecoveryCompleted
{
    public static RecoveryCompleted Instance { get; } = new RecoveryCompleted();
    private RecoveryCompleted() {}
}

The MessagePack constructor could work with this type of objects only if I use SerializationConstructor. It is fine for my own objects, but if I try to serialize internal Akka objects with ContractlessStandardResolver, I will get an error.

Another option is to create a separate Formatter for each internal singleton message

Activity

  1. changed the title [-]Singleton objects with private constructor[/-] [+]Singleton objects with a private constructor[/+] on Apr 10, 2017
  2. neuecc commented on Apr 10, 2017

    @neuecc
    Member

    What kind of effect is expected, serializer can not deserialize that have only private constructors.
    Of course you can not set it to static private property.

    If necessary, please make custom serializer.

    public class RecoveryCompletedFormatter : IMessagePackFormatter<RecoveryCompleted>
    {
        public int Serialize(ref byte[] bytes, int offset, RecoveryCompleted value, IFormatterResolver formatterResolver)
        {
            // TODO:as you like
        }
    
        public RecoveryCompleted Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            // force create object...
            var uninitializedObject = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(RecoveryCompletedFormatter));
    
            // set from bytes...
    
            // Of course can not set, but use reflection, can set...
            // RecoveryCompleted.Instance = uninitializedObject;
    
        }
    }

    This can not be generalized.

  3. alexvaluyskiy commented on Apr 11, 2017

    @alexvaluyskiy
    Author

    JSON.NET supports a private constructor via ConstructorHandling.AllowNonPublicDefaultConstructor. Wire also supports it

  4. neuecc commented on Apr 11, 2017

    @neuecc
    Member

    Is it OK to understand that Singleton is irrelevant?

  5. alexvaluyskiy commented on Apr 11, 2017

    @alexvaluyskiy
    Author

    Yes, singleton was an inappropriate example. It is all about private constructor

  6. changed the title [-]Singleton objects with a private constructor[/-] [+]support private constructor, members[/+] on Apr 11, 2017
  7. neuecc commented on Apr 11, 2017

    @neuecc
    Member

    Okay.
    I know how to handle private constructors and private members as fast.
    I'll implement it.

  8. alexvaluyskiy commented on Jun 8, 2017

    @alexvaluyskiy
    Author

    It does not support internal members too

  9. israellot commented on Jun 13, 2017

    @israellot
    Contributor

    I think using private setters for properties is a good start.

  10. megakid commented on Oct 4, 2017

    @megakid

    Any progress on this? This is a blocker for us using this superb library.

  11. neuecc commented on Oct 5, 2017

    @neuecc
    Member

    Utf8Json supports this feature, I found how to implements completely smart and keep performance way.
    But MessagePack-Csharp needs a little refactoring.
    Anyway, I know what to do, so I will finish it.

  12. megakid commented on Oct 5, 2017

    @megakid

    Awesome work - Really nice job on the UTF8Json library as well.

  13. neuecc commented on Oct 9, 2017

    @neuecc
    Member

    I've release v1.7.0, it includes this feature.
    you can use new resolvers, StandardResolverAllowPrivate or ContractlessStandardResolverAllowPrivate.

  14. megakid commented on Oct 10, 2017

    @megakid

    Nice work. Thanks

  15. tylerhartwig commented on Oct 30, 2018

    @tylerhartwig

    Hi, I'm trying to use this library to deal with private constructors.

    I have made the following unit test:

        public class UnitTest1
        {
            public T SerializeDeserialize<T>(T obj)
            {
                var serialized = MessagePackSerializer.Serialize(obj, ContractlessStandardResolverAllowPrivate.Instance);
                var unserialized =
                    MessagePackSerializer.Deserialize<T>(serialized, ContractlessStandardResolverAllowPrivate.Instance);
                
                return unserialized;
            }
    
            public class TestType
            {
                public static TestType Create(int initial)
                {
                    var t = new TestType();
                    t.Val = initial;
                    return t;
                }
                
                private TestType() { }
                private int val;
    
                public int Val
                {
                    get { return val; }
                    private set { val = value; }
                }
            }
    
            [Fact]
            public void TestPrivateTestType()
            {
                var testType = TestType.Create(42);
    
                var actual = SerializeDeserialize(testType);
                Assert.Equal(testType.Val, actual.Val);
            }
        }
    

    This test fails with MessagePack.Internal.MessagePackDynamicObjectResolverException : can't find public constructor.

    I'm using MessagePack v1.7.3.4.

    Am I using this incorrectly? my understanding from this issue is that the TestType should serializable and deserializable.

  16. qstarin commented on Nov 17, 2018

    @qstarin

    I'm experiencing the same behavior as tylerhartwig with MessagePack v1.7.3.4.

    [MessagePackObject]
    public class Image
    {
        [SerializationConstructor] 
        private Image() { }
        
        public Image(params string[] relativePathSegments)
        {
            if (ImagePathProvider.TryGetPaths(relativePathSegments, out var lp, out var ru)) {
                Path = lp;
                Url = ru;
            }
        }
    
        [Key(0)] public string Path { get; private set; }
        [Key(1)] public string Url { get; private set; }
        [IgnoreMember] public bool Exists => Path != null;
    }
    

    MessagePackSerializer.Serialize(value, MessagePack.Resolvers.StandardResolverAllowPrivate.Instance); fails with MessagePack.Internal.MessagePackDynamicObjectResolverException : can't find matched constructor.

    If I make the constructor public, it works fine.

  17. CheeryProgrammer commented on Mar 15, 2019

    @CheeryProgrammer

    SerializationConstructorAttribute does not help :(

  18. tylerhartwig commented on Mar 22, 2019

    @tylerhartwig

    IIRC what I found was that "AllowPrivate" on this MessagePack library only means access to private fields/members, but does not check for private constructors.

    Not sure if that helps y'all, but I thought I'd add that in. I ended up making all my constructors public for the time being (though my code base is only myself, so I can enforce my own good practices).

  19. added a commit that references this issue on Apr 17, 2019
  20. added a commit that references this issue on Mar 11, 2023
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