Newtonsoft.JSON ignore a JsonProperty
Hi there! I'm struggling at making my classes to correctly be serialized. I have an interface:
And its implementation:
I'm also using this serializer:
And I'm serializing my value this way:
However, I am unable to get the type property working! It literally never shows when serializing my class.
For instance, I'm getting
{"text": "Hello There", "extra":[]}
when serializing new TextComponent("Hello There")
Can anyone help me about this? Thanks ^.^11 Replies
newtonsoft.json bad
switch to the default serializer stuff its way more reliable and saves you trouble in the futur
Ah really? At this point ?
NSJ is a very old library made when nothing was available... people that worked on it also made System.Text.Json which is a default library you can use in c# which is 1000x faster and better.
NSJ should only be used these days with either malformed json or edge cases.
How to write custom converters for JSON serialization - .NET
Learn how to create custom converters for the JSON serialization classes that are provided in the System.Text.Json namespace.
JsonStringEnumConverter Class (System.Text.Json.Serialization)
Converts enumeration values to and from strings.
Gotcha, I'm trying to convert my current code with the system lib, but I cna't get the
JsonPropertyName
attribute working, am I missing something? ;-;
And I'm getting {"componentType":"Text","children":[]}
Edit: Got it working, I have to specify the implementation's class so it scans the right class' fieldsIf you have no further questions, please use /close to mark the forum thread as answered
Ah sorry, I got a last one;
I have a custom serializer for a type:
And its goal is to serialize the fields in the code object, so not in another object with key
style
as the current code do
And I am looking for something like:
How can I achieve this?
Another question, how can I create my own ignore condition? Basically, if a list field is empty it's ignoredthere should be a [JsonIgnore] attribute I believe
but u mean if its empty dont output it to the resulting json right?
I think there is a option for that in the settings u add but I dont remember the name unfortunately
maybe JsonIgnoreCondition.WhenWritingNull https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonignorecondition?view=net-8.0
JsonIgnoreCondition Enum (System.Text.Json.Serialization)
Controls how the JsonIgnoreAttribute ignores properties on serialization and deserialization.
@Sky
but do u want that inside your custom converter? because u seem to already have something to handle it