Giter Site home page Giter Site logo

Comments (4)

AArnott avatar AArnott commented on June 15, 2024

When using non-default constructors for deserialization, you should apply [SerializationConstructor] on the constructor. Does that resolve the issue?

from messagepack-csharp.

F2 avatar F2 commented on June 15, 2024

Thanks for the suggestion. Unfortunately no, applying [SerializationConstructor] to the constructor does not change anything - I still get the "No set accessor" exception.

However, if I add an extra constructor that is never used by me or the serialization process, then it works:

[SerializationConstructor]
private Player()
{
    // This will never be called
    throw new NotImplementedException();
}

Still, it does not make much sense to me why such a constructor is needed - and indeed, it was not necessary in previous versions of the nuget.

It makes sense in the case of deserialization when it needs a constructor to construct the object, but not serialization.

from messagepack-csharp.

AArnott avatar AArnott commented on June 15, 2024

Thanks for trying it out. I agree this seems like a regression and I'll try to find time to investigate and correct it.

I agree deserialization's requirements aren't fundamentally the same as for serialization. But the dynamic object resolver creates a formatter to serialize and deserialize at the same time, and at the moment, it enforces requirements for both scenarios when the formatter is generated. That is something we could improve on in the future.

from messagepack-csharp.

AArnott avatar AArnott commented on June 15, 2024

I can repro the failure. But now that I'm looking at it more closely, I agree with it, modulo the missing support for non-deserializable objects that I mentioned before. For future reference, here's why:

  • The only property that could be effectively serialized is your Name property.
  • Scores is ignored by an explicit attribute.
  • TotalScore is supposed to be serialized, but it doesn't store a value. It's computed from the ignored property. And it has no setter as a result. So the serialized form cannot be used to construct a deserialized object.

This is as @F2 said in the first place. I just misunderstood the nuance.
Anyway, if and when MessagePack's DynamicObjectResolver ever supports types that can only be serialized or deserialized (not both), then this is a good example of a type that should start working at that point. I imagine the way we could achieve that is for the resolver to record that an object can only go one direction, and emit a formatter that throws when going the other direction. Ideally, the exception thrown if used in the unsupported direction would match the exception that the resolver itself currently throws so that it can point the user in the right direction to fix the problem if they intended bidirectional serialization.

I suspect the fact that this "worked" in prior versions was probably due to a hole in detection of the unsupported scenario, and you got lucky. To upgrade MessagePack and keep working, you can either use the workarounds you already mentioned, or write a custom formatter for this type.

I'm going to close this issue as Won't Fix because we don't have plans to support uni-directional serialization in the DynamicObjectResolver any time soon.

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.