Giter Site home page Giter Site logo

Comments (16)

neuecc avatar neuecc commented on May 22, 2024 1

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.

from messagepack-csharp.

qstarin avatar qstarin commented on May 22, 2024 1

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.

from messagepack-csharp.

neuecc avatar neuecc commented on May 22, 2024

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.

from messagepack-csharp.

alexvaluyskiy avatar alexvaluyskiy commented on May 22, 2024

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

from messagepack-csharp.

neuecc avatar neuecc commented on May 22, 2024

Is it OK to understand that Singleton is irrelevant?

from messagepack-csharp.

alexvaluyskiy avatar alexvaluyskiy commented on May 22, 2024

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

from messagepack-csharp.

neuecc avatar neuecc commented on May 22, 2024

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

from messagepack-csharp.

alexvaluyskiy avatar alexvaluyskiy commented on May 22, 2024

It does not support internal members too

from messagepack-csharp.

israellot avatar israellot commented on May 22, 2024

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

from messagepack-csharp.

megakid avatar megakid commented on May 22, 2024

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

from messagepack-csharp.

megakid avatar megakid commented on May 22, 2024

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

from messagepack-csharp.

neuecc avatar neuecc commented on May 22, 2024

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

from messagepack-csharp.

megakid avatar megakid commented on May 22, 2024

Nice work. Thanks

from messagepack-csharp.

tylerhartwig avatar tylerhartwig commented on May 22, 2024

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.

from messagepack-csharp.

CheeryProgrammer avatar CheeryProgrammer commented on May 22, 2024

SerializationConstructorAttribute does not help :(

from messagepack-csharp.

tylerhartwig avatar tylerhartwig commented on May 22, 2024

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).

from messagepack-csharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.