Comments (6)
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.
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.
+1, this functional is very necessary
from jsonsubtypes.
+1, this functional is very necessary
from jsonsubtypes.
@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.
+1
from jsonsubtypes.
Related Issues (20)
- Support Enums HOT 2
- Misleading error message on deserialization when subtype is unknown HOT 1
- Allow to disable behavior introduced by fix #100 in 1.8.0 - Stop StopLookupOnMatch HOT 2
- Incorrect link to license
- New Major to depend on newer Newtonsoft.Json HOT 3
- KnownSubTypeWithProperty it would be great to support aliases HOT 3
- Support (serializing) sub types with generic type parameters when using JsonSubtypesConverterBuilder HOT 7
- MaxDepth not copied over when creating internal JsonReader
- Use in API calls?
- Example code in readme fails, just completely wrong according to visual studio HOT 1
- JsonSubtypesConverterBuilder SetFallbackSubtype HOT 1
- wrong link in v2.0.1 release notes HOT 1
- JsonReaderException does not contain a constructor that takes 5 arguments HOT 4
- Persistent settings HOT 1
- Base Class Properties not being Deserialized HOT 2
- Outdated version of NewtsonSoft.Json used
- deserializing dates with jsonsubtypes does not match same result values as stock newtonsoft
- Error on deserialization "Could not find member ... on type" when MissingMemberHandling=error and there is no discriminator field on the dto HOT 1
- Support combination of independent discriminator types per inheritance level HOT 2
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 jsonsubtypes.