As mentioned in this other question, I'm setting my first steps in C# and JSON.
I'm working on a small application, that can read information from a DB and write it in a JSON file. In order to do this, I thought of creating the classes as they were defined in the JSON file. For that, I used Web Essentials and this has done a great job: the classes are generated completely. All this is mentioned in my previous question.
Now I was thinking of using those classes to write back to the JSON file, so I have this situation:
JSON file1 : used as an input in order to create the class diagram.
DB : contains some information I need to add to the JSON file.
JSON file2 : used as an output, based on "JSON file1", and having the
extra information, retrieved from the DB.
Very naïvely apparently, I've done this:
root.ToString(); // hoping that this would generate the JSON output.
But, I only seem to get some basic information out of that method (namespace and classname, no content).
Using intellisense, I don't see any method which generates my JSON output, but there's one flair of hope in my code (the one, generated by Web Essentials), as you can see:
public class Rootobject
{
public Project project { get; set; }
}
public class Project
{
public string commonDESCRIPTION { get; set; }
...
public Location[] locations { get; set; }
As you can see, neither Rootobject
or Project
inherit from anything (nor do they realise an interface), and I just cannot believe there isn't any class which covers this behaviour. So, I would like to know from which class I need to inherit in order to solve this issue.
I, however, don't think it might be that easy, and maybe some Web Essentials configuration editing needs to be done in order to cover this, but I don't know how to modify Visual Studio extension configuration.
Edit
Some more information on my Web Essentials installation: after having installed Web Essentials on my Visual Studio 2017 Enterprise platform, I can see Paste Special, Paste JSON as classes in my Edit menu, but I don't see Web Essentials in the menu toolbar (just File, Edit, ...) and in Tools, Options I also don't see any Web Essentials references.
Can anybody help me?
Thanks in advance
Apparently (as mentioned by Ruan), Web Essentials can turn a JSON file into a class model, but in the other direction, another way must be followed.
The way, which is mentioned (NewtonSoft), is a very good one:
using Newtonsoft.Json;
.string result = JsonConvert.SerializeObject(root);
.