Serialising and deserialising json derived file
Hello. I have a json derived file (but not quite standard json) that I'd like to serialise and deserialise. Any ideas how to do it easily without writing my own parser? Honestly I wouldn't know where to start.
17 Replies
Looks like regular JSON but with trailing commas and
;;
for comments
If you ran a regex on the string to get rid of the comments, then you could use System.Text.Json to parse it into a class.It's internally called quasi-json, the main difference is that we assume an enclosing object (i.e. the first element does not have to be an object or array opening brace) and all key-value pairs MUST be ended by a comma (as opposed to true JSON where the last one in an object explicitly cannot)
interesting
Well, sounds like you could probably just surround the text with
{
and }
to create the initial object
And System.Text.Json can handle trailing commas just finejust gotta get rid of the comments
trailing commas isn't officially part of the JSON spec but it's largely supported in some fashion just cause
I think this regex would work
What can I do with the name of properties such as "string" though? Sorry I'm a little new to deserialising objects.
System.Text.Json handles all the serialization for you
thinker227#5176
REPL Result: Success
Result: Person
Compile: 563.092ms | Execution: 48.062ms | React with ❌ to remove this embed.
if your property name is a reserved keyword, you can either use
[JsonPropertyName("string")]
on the property or (i think) even @string
for the property name would work
though I wouldn't recommend that second approach even if it works, just use the attribute
but case insensitive anyway, can't think of any uppercase reserved keywordsI think I'm going to write a parser because there are so many weird variables I don't know how to handle that it might be easier 😅
I mean strip the comments, enclose the whole thing in
{ }
and write appropriate containers, not much to itI serialised and deserialised objects before for my game, but this is something different 😄
writing a parser is a lot of work, you'll make a mess and the tools already exist anyway
also if I have names for objects like "romans_julii" and "macedon" - how to handle those? They are essentially the class types.
will cover both of those, the property name tells the Deserializer where to try to put the data, the type of the property tells it how to deserialize it, and the property names are case insensitive so all you have to worry about is weird characters
I think I need to do some trial and error because honestly I'm not following. Thank you for your help though, and I won't bother you anymore because clearly I need to do some reading to understand the subject better rather than to waste people's time 😉
Basically I'm trying to make a modding tool for Rome Total War Remastered, where game / mod files can be deserialised into GUI objects, that can be edited and then serialised back into this format so that the game can read them.
It's funny that I managed to release relatively successful games on Steam, but can't deal with simple serialisation issue 🙂
kinda their fault for taking a commonly and widely used spec and bastardising it for essentially no reason whatsoever