Git Product home page Git Product logo

Comments (6)

MYDIH avatar MYDIH commented on June 6, 2024 1

Hi and thank you for your time,

Sorry that it wasn't clear enough,

Actually I want to serialize polymorphic JSON with a "type" attribute containing the type. I don't want to have to specify this type in the code, it should be set automatically when a class is of a given type based on the mappings I use with KnownSubType. I would prefer that this field isn't taking part of the class even though it wouldn't be blocking if it wasn't possible. My other requirement is that I want to be able to achieve that using Attributes of my classes/fields for the reason I mentioned before.

So far, when I'm using the documented method (the one you describe), the type isn't serialized along with the class. If I want to serialize it, it seems I have two options from my understanding, use the method where I need to generate a JsonSerializerSettings (which is a no go because I need to use the Attributes approach), and declaring the field type in my class (I figured that one searching the issues).

I tried adding the type field to my classes, but it is not automatically filled with the proper type when serializing. It seems I need to specify it manually. Tell me if I'm wrong of course.

What I did in the fork is creating a new constructor for the class JsonSubtypesByDiscriminatorValueConverter that sets the _serializeDiscriminatorProperty to true. I can confirm that it's working like charm 😄

I hope it makes everything clear enough ...

from jsonsubtypes.

manuc66 avatar manuc66 commented on June 6, 2024

Hi @MYDIH

I try to put all that is not "documented api" to internal so that I can have less constraint linked to public api breaking changes.

Could you explain why you're not using:

[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.FallBackSubType(typeof(UnknownExpression))]
public class Animal
{

}

from jsonsubtypes.

An2An96 avatar An2An96 commented on June 6, 2024

+1, this functional is very necessary

from jsonsubtypes.

my6521 avatar my6521 commented on June 6, 2024

+1, this functional is very necessary

from jsonsubtypes.

esbenbach avatar esbenbach commented on June 6, 2024

@manuc66 Any chance a PR will be approved if we change the ctor to become public like MYDIH suggests?
I have found myself needing this several times as well.

from jsonsubtypes.

HaptelmanovArtem avatar HaptelmanovArtem commented on June 6, 2024

+1

from jsonsubtypes.

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.