C
C#2y ago
kommissar

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
Thinker
Thinker2y ago
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.
kommissar
kommissarOP2y ago
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)
Thinker
Thinker2y ago
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 fine
Doombox
Doombox2y ago
just 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
Thinker
Thinker2y ago
I think this regex would work
^\s*;;.*$
^\s*;;.*$
kommissar
kommissarOP2y ago
What can I do with the name of properties such as "string" though? Sorry I'm a little new to deserialising objects.
Thinker
Thinker2y ago
System.Text.Json handles all the serialization for you
MODiX
MODiX2y ago
thinker227#5176
REPL Result: Success
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<Person>("""
{
"Name": "John",
"Age": 34
}
""")
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<Person>("""
{
"Name": "John",
"Age": 34
}
""")
Result: Person
{
"name": "John",
"age": 34
}
{
"name": "John",
"age": 34
}
Compile: 563.092ms | Execution: 48.062ms | React with ❌ to remove this embed.
Doombox
Doombox2y ago
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 keywords
kommissar
kommissarOP2y ago
I 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 😅
Doombox
Doombox2y ago
I mean strip the comments, enclose the whole thing in { } and write appropriate containers, not much to it
kommissar
kommissarOP2y ago
I serialised and deserialised objects before for my game, but this is something different 😄
Doombox
Doombox2y ago
writing a parser is a lot of work, you'll make a mess and the tools already exist anyway
kommissar
kommissarOP2y ago
also if I have names for objects like "romans_julii" and "macedon" - how to handle those? They are essentially the class types.
Doombox
Doombox2y ago
[JsonPropertyName("romans_julii")]
public string Whatever { get; set; }
public string Macedon { get; set; }
[JsonPropertyName("romans_julii")]
public string Whatever { get; set; }
public string Macedon { get; set; }
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
kommissar
kommissarOP2y ago
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 🙂
Doombox
Doombox2y ago
kinda their fault for taking a commonly and widely used spec and bastardising it for essentially no reason whatsoever
Want results from more Discord servers?
Add your server