Comments (16)
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.
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.
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.
JSON.NET supports a private constructor via ConstructorHandling.AllowNonPublicDefaultConstructor
. Wire also supports it
from messagepack-csharp.
Is it OK to understand that Singleton is irrelevant?
from messagepack-csharp.
Yes, singleton was an inappropriate example. It is all about private constructor
from messagepack-csharp.
Okay.
I know how to handle private constructors and private members as fast.
I'll implement it.
from messagepack-csharp.
It does not support internal members too
from messagepack-csharp.
I think using private setters for properties is a good start.
from messagepack-csharp.
Any progress on this? This is a blocker for us using this superb library.
from messagepack-csharp.
Awesome work - Really nice job on the UTF8Json library as well.
from messagepack-csharp.
I've release v1.7.0
, it includes this feature.
you can use new resolvers, StandardResolverAllowPrivate
or ContractlessStandardResolverAllowPrivate
.
from messagepack-csharp.
Nice work. Thanks
from messagepack-csharp.
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.
SerializationConstructorAttribute does not help :(
from messagepack-csharp.
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)
- `ConvertToJson` not escaping invalid unicode control codes HOT 1
- Simply does not work properly on .NET 8 HOT 2
- 'MessagePack.IgnoreMemberAttribute' is not Inherited(after overriding property) in 2.5.140. HOT 2
- Generation of Message Pack fails with .Net 7 HOT 3
- Vs2022 ver17.8.3 mpc fail HOT 12
- .NET 8: System.Collections.Frozen not supported HOT 4
- Can't save enum in Unity HOT 4
- The serialization process does not convert the data into a binary format. HOT 1
- System.ArgumentException: The value "System.Object[]" is not of type and cannot be used in this generic collection. (Parameter 'value') HOT 1
- Seems that MessagePack doesn't work for Android [Help wanted]
- Update to .NET 8 HOT 4
- Can someone explain how to use 2.6 source generator? [Help wanted] HOT 4
- Always-on AOT HOT 7
- Source generated resolver should include hand-written formatters HOT 6
- .NET 8: (U)Int128 handling HOT 3
- Source generate formatters as nested under the types they format for private member access HOT 4
- Serialization of WeakReference<T> HOT 4
- Question: Possibility of exposing LZ4 functionality? HOT 1
- ThreadsafeTypeKeyHashTable.VolatileWrite missing nullable declaration for `value` parameter HOT 2
- MessagePack.Generator - Issues with generic
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from messagepack-csharp.