C
C#2mo ago
Daiko Games

How to convert a whole json file to C# classes with Newtonsoft.Json?

Hello, I am curious if there is a way to convert a whole json File with Newtonsoft.Json to C# classes without knowing anything about the file. The code should be written in C#. I tried nearly every tutorial on YouTube and every Tutorial is about serializing or deserializing only one object at a time, and I looked over on some examples on Stack - Overflow and even Bing Chat gives me the same answer. I really need this for a Project I am building on, any example with the specific topic I was writing about earlier could help!
Thanks for help!
33 Replies
Buddy
Buddy2mo ago
You mean this?
No description
Buddy
Buddy2mo ago
Or do you want to make a converter yourself that parses JSON as a C# class
Becquerel
Becquerel2mo ago
if you want to extract values etc. from json dynamically you want jobjects https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm
Daiko Games
Daiko Games2mo ago
yes I want to make a converter that parses JSON as C# classes.
ACiDCA7
ACiDCA72mo ago
can you show us example jsons? with some brief explanation of the variations
Daiko Games
Daiko Games2mo ago
I mean yeah, but I have to tell you, I want to make a converter from sb3 to C# and these json Files that are stored in the sb3 File (zip File) are one Liners which are very long and to make a converter every object must be converted sepperately on its own. Because if you want to convert a another Project which is not the same the json code will be different, so that's why I am trying to figure out how to do it, but I can give you an example of what I mean.
Daiko Games
Daiko Games2mo ago
Like I said, it is a one Liner that can not be easily read So that is why I am asking if there is a way to look up for the objects and convert them dynamically to C# classes. Is there a way?
ACiDCA7
ACiDCA72mo ago
well that it is a oneliner isnt a problem.. there are formatters that indent it for you
Daiko Games
Daiko Games2mo ago
I know, but I don´t know how to dynamically convert the json to C# classes
Kiel
Kiel2mo ago
Personally in the past I used https://quicktype.io/ to make a quick and dirty concept and then I'd refine it from there to my liking
Convert JSON to Swift, C#, TypeScript, Objective-C, Go, Java, C++ a...
quicktype generates types and helper code for reading JSON in C#, Swift, JavaScript, Flow, Python, TypeScript, Go, Rust, Objective-C, Kotlin, C++ and more. Customize online with advanced options, or download a command-line tool.
Daiko Games
Daiko Games2mo ago
I know about this, but it is not what I am looking for
ACiDCA7
ACiDCA72mo ago
where exactly does thid json come from again? there are keys that look like generated from obfuscated stuff
Daiko Games
Daiko Games2mo ago
Scratch makes the json file when you make a Project on it in a zip file you can extract the zip file (sb3 File - you can just change the name) and inside of it there is the json file. I didn´t generate it the Kids Programm I used for making a toon game made the json script. I want to use the C# class files later for something else. I just want to parse the json script I have to C# classes dynamically
ACiDCA7
ACiDCA72mo ago
well if I would do this i would analyze the filestructure. on quick looking it seems the properties are dependent on the opcodes. have a list that contains all the operations i guess as keyvaluepair or directly as a dictionary. maybe you find online documentation to the jsonfile?
Daiko Games
Daiko Games2mo ago
So that means I should make a searcher for the specific operators I have in my json and wehnever the Streamreader (maybe) finds one of the Operators I could read the name of the operator and let the operator be converted? is that a good Idea? I will need to search further, thanks for the help I will look through the chat again tomorrow. I will find a solution to the "Problem".
ACiDCA7
ACiDCA72mo ago
https://web.archive.org/web/20240920213847/https://en.scratch-wiki.info/wiki/Scratch_File_Format im just gonna drop this here.. sadly i dont have much time atm, maybe someone else can help you further
Scratch File Format
The Scratch 3.0 file format is the format used to store exported Scratch 3.0 projects and sprites. These are ZIP archives which contain information encoded in a text-based format called JSON and project media in separate files. Projects have the extension .sb3, and sprites .sprite3. The MIME type of .sb3 files is application/x.scratch.sb3.
Daiko Games
Daiko Games2mo ago
ok, good Night
ACiDCA7
ACiDCA72mo ago
gute nacht ;)
Joschi
Joschi2mo ago
What you are looking for is called polymorphic deserialization. I'm never used Newtonsoft, but System.Text.Json would support your use case. Your opcode would be the type discriminator and then you would have to design a class for each opcode and all those classes need to inherit off of the same base class. I doubt that there is a tool, that could do this for you.
Daiko Games
Daiko Games5w ago
ok, this link was pretty useful, I am currently working on the Problem. These Class names can be filtered from a FileLine, but it could be useful if someone would explain to me how to make a whole json prettier with Newtonsoft.Json Because then I could possibly make something to work with better.
ACiDCA7
ACiDCA75w ago
why does it matter how pretty it looks? the deserializer doesnt care about indentation or are you reinventing the wheel by writing your own deserializer?
Daiko Games
Daiko Games5w ago
No it is just so I know the lines where the specific objects are and then the converter reads these specific lines one by one I hate to say it but if this doesn‘t make any sense or isn’t possible just tell me. I am new to the topic
ACiDCA7
ACiDCA75w ago
so you want to just desrialize just a subset of the json?
Daiko Games
Daiko Games5w ago
It is just for me I can work better with a prettier json.
ACiDCA7
ACiDCA75w ago
copy the json to any tool that can prettify it. there are online sites that can do it, vs/vscode have plugins for it or can even just do it etc
Daiko Games
Daiko Games5w ago
Ok Thanks
ACiDCA7
ACiDCA75w ago
eg in vscode just open the file and then press format document and tadaa its pretty ;)
Daiko Games
Daiko Games5w ago
I think with that the question is now answered. Thanks ☺️
Joschi
Joschi5w ago
Just to be sure. You are not actually working on and parsing the content of the file line by line?
Daiko Games
Daiko Games5w ago
would it be bad?
Joschi
Joschi5w ago
Yes, because that is not how you deal with Json. You want to use deserialization. https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/deserialization
How to deserialize JSON in C# - .NET
Learn how to use the System.Text.Json namespace to deserialize from JSON in .NET. Includes sample code.
Daiko Games
Daiko Games5w ago
Ok, looks like I am doing it right. I just asked if it would be bad but I am doing it the right way
Want results from more Discord servers?
Add your server