✅ Opening a Json file
I switched from
Newtonsoft.Json
to System.Text.Json
and i can't get it to work. it gives me an object with null attributes
38 Replies
If you want help you will have to give a lot more context than this and $paste your code for starters. This is incredibly vague and we don't know your project.
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
More code. Classes, the converter, everything related
and how is
Ogmo.Level
looking?This still helps nothing
with Newtonsoft it worked
Which is an entirely different package
my objects matchs the json file
The only suggestion I have now is that you validate that
file
actually contains the JSON you expect to deserialize. This could also be the cause of the problem.
Until that, either check that or share the code or we have absolutely no way or reason to help
Generally Newtonsoft and STJ work similary to eachother but there are plenty of factors that can influence it. The converter can be one of them. We can't help you with just pure guesswork so you should share the code or perform basic debugging.file:
my code:
I can see public fields
how's the converter looking
Pretty sure even Newtonsoft doesn't use public fields by default so this is something you configured
think so too
STJ also has this configured. They use properties by default
how should i make my objects ?
can you show the converter
generally I do public properties
wait, there is no converter
i removed it, first i just want it to load this
The correct answer would be to switch to properties since that's the correct approach. You can also pass configuration to System.Text.Json and specify fields should be used.
Include fields in serialization - .NET
Learn how to include fields when you serialize to and deserialize from JSON in .NET.
JsonSerializerOptions.IncludeFields Property (System.Text.Json)
Gets or sets a value that indicates whether fields are handled during serialization and deserialization. The default value is false.
By default, fields are ignored. You can include fields.
ah, I'm too slow
um, is there a way to make them readonly or private set ?
just to make things more clean
Make a property with a public getter but an init setter
ahhh, i can't use uint ?
public int Foo { get; init; }
i didn't know about that
I assume STJ would work fine with this. Any particular reason why you say this?
i'm using positive numbers
just change it to
uint
, don't think it's a problemshould i get back to Newtonsoft ?
Again, I don't see why it would not work. The only issue seemingly needing a fix is the fact fields are not by default used
So either switch to properties, or use the option I shared above
'The JSON value could not be converted to System.UInt32. Path: $.layers[0].entities[0]._eid | LineNumber: 21 | BytePositionInLine: 28.'
:(
where line 21 :
"_eid": "74122737",
That's because it's a string, not an integer
I assume by default Newtonsoft parses it
The correct fix is to pass a proper number
Alternatively, add the following to the list of options:
oh thanks i didn't noticed that was a string
ok this works! and runs much faster than Newtonsoft
thanks guys
now lets get into polymorphism
If you have no further questions, please use /close to mark the forum thread as answered
polymorphism in STJ is actually really nice and easy, as long as your variants are known at compile time, and you have some measure of control over the json, or it alread being wellformed (like having a discriminator value)
https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism has all the info you need
it all works perfectly, thanks!
i'm done with json