Giter Site home page Giter Site logo

Comments (2)

Whathecode avatar Whathecode commented on July 22, 2024 1

Related to #2245.

@elgonzo

Yes and no. (I actually read this issue before posting.)

Yes, in the responses the OP seems to basically suggest the desired behavior I outline here. The behavior which works, but only in case the converter is registered through an attribute. So maybe this has been implemented since, or the OP was unaware.

No, in that the OP started from the outset with a broader (non-generic) converter, and incorrectly implemented CanConvert.

The discrepancy in behavior between the two ways of registering is what I primarily want to highlight here. Documentation-wise (or the limited source code I browsed), I see no indication this is intentional. But, it strikes me as extremely odd (and even unlikely) such an old established, widely used, library would contain a bug in such a core feature.

Out-of-the-box support for things like this is why serialization frameworks exist. 🤔 So I still feel I may be overlooking something.

from newtonsoft.json.

elgonzo avatar elgonzo commented on July 22, 2024

Related to #2245. As commented on in that issue, the workaround for this limitation is using the non-generic JsonConverter base class and overriding the CanConvert method in your converter and compare the provided type object against both Test and Nullable<Test>. If you need to do this with several nullable value types, it is probably advisable to create an intermediate generic abstract class inheriting from JsonConverter class which would then serve as generic base class for the various nullable-capable json converters for desired value types.

Also keep in mind that WriteJson method of your converter would need to deal with Nullable<T> instances representing null in whatever way is desired, with the ReadJson method likely also needing to deal with the json input possibly containing null values instead of data for a Test instance.

(P.S. I am just a user and not associated with the Newtonsoft.Json project)

from newtonsoft.json.

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.