C
C#2mo ago
MagicJinn

Force Newtonsoft to deserialize into certain formats

I've been using JsonFx for a bit now and am trying to migrate away to Newtonsoft, but the biggest problem I've faced is that when you deserialize into Dictionary<string,object>, and the object is another nested object, it'll become a JObject. Is there any easy way to force Newtonsoft to deserialize T or List<T> (depending on the data), instead of JObject for all nested objects?
24 Replies
MagicJinn
MagicJinnOP2mo ago
I mention T and List<T> because I plan to use this code with code other people wrote, and I don't want to refactor giant swaths of code just so it can handle JObjects.
127001
1270012mo ago
There is callbacks when deserializing or deserialized https://www.newtonsoft.com/json/help/html/SerializationCallbacks.htm Because it's generic object and doesn't carry a type in JSON, it will not able to find the right class for you automatically @MagicJinn (ping me!)
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
MagicJinn
MagicJinnOP2mo ago
For performance reasons. I've tried my very, VERY best to use Swifter.Json, which doesn't have this problem and is faster, but it doesn't work at all. I'm modding a game that has a massive bottleneck in loading times because of JsonFx.
Because it's generic object and doesn't carry a type in JSON
Well JsonFx and Newtonsoft have different behaviour in this case. JsonFx deserializes nested objects into T or List<T>, and Newtonsoft into JObject and List<JObject>. I'm not entirely sure how the wiki page you sent helps me with that.
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
MagicJinn
MagicJinnOP2mo ago
Nope. Net3.5, Unity 2018.
canton7
canton72mo ago
How would json.net know what T it should deserialize as? If you're only letting it deserialize into a Dictionary<string, object>, all it knows about each value is that it's an object
qqdev
qqdev2mo ago
I think the only thing it could do would be to stick to a dictionary (all the way down)
canton7
canton72mo ago
I mean, JObject implements IDictionary<string, JToken> anyway, so you can treat it as dictionaries all the way down already
qqdev
qqdev2mo ago
Yeah, would be nice if you got also get rid of JToken tho I think OP has two options: - Don't serialize into a Dictionary<string, object> - Work with the Newtonsoft types
canton7
canton72mo ago
(unless you actually need a concrete Dictionary) In which case, a custom deserializer is probably the easiest way to go? (But OP did say List<T>, not List<object>, so I do wonder whether we're missing some detail)
qqdev
qqdev2mo ago
Oh yeah, that's impossibru
MagicJinn
MagicJinnOP2mo ago
Well here's the deal, I can adjust my code to work with newtonsoft types, but I can't adjust the code of the game I'm modding, or at least I can't majorly overhaul it. The game I'm modding sadly relies on JsonFx's behaviour. I guess I could recursively cast into the desired type, but I'm not sure what the performance cost of that will be. I'll test it tho, unless anyone has a better idea.
qqdev
qqdev2mo ago
How is the game serializing the JSON? Do they use classes which you can access?
canton7
canton72mo ago
Can you clarify what JsonFx's behaviour is? How does it deserialize into a List<T> (where T is not object) without you giving it more type information?
MagicJinn
MagicJinnOP2mo ago
Yes.
qqdev
qqdev2mo ago
Did you try to deserialize using the respective type? Or are classes not compatible with Newtonsoft? (custom attributes, etc.) Let's say the game is serializing an object of type Banana. You could also use that Banana type to deserialize the JSON instead of deserializing into a dictionary
MagicJinn
MagicJinnOP2mo ago
When you specify Dictionary<string,object>, it'll convert nested objects to JObjects (or List<JObject>), that I know for sure. When I try to patch the existing methods in the game to deserialize into custom classes, I get non-descript errors, but I'm assuming that's what is happening as well. I can't be certain though, and I'd have to do more testing. I've temporarily given up on it though, since for some reason, when using Newtonsoft, seemingly unrelated methods fail spectacularly at random intervals.
canton7
canton72mo ago
Can you clarify what JsonFx's behaviour is here?
MagicJinn
MagicJinnOP2mo ago
If I'm not wrong, when you deserialize with JsonFx, all nested objects will also deserialize into that type, and arrays will deserialize into lists of that type
canton7
canton72mo ago
Into what type?
MagicJinn
MagicJinnOP2mo ago
The type specified. So new JsonReader.Read<Dictionary<string,object>> will have JsonFx attempt to deserialize every object into another Dictionary<string,object>.
canton7
canton72mo ago
That would be super-odd, and I don't see any mention of that in the readme!
MagicJinn
MagicJinnOP2mo ago
Well perhaps Dictionary<string,object> is just the default for JsonFx, as JObject is for Newtonsoft
Want results from more Discord servers?
Add your server