For a project that I am working on, I'm trying to replicate a JSON file that was created with JavaScript in C#. For this project I have to label images before I can run them through a Neural Network, and to annotate those images I use via. The resulting JSON file looks like this.
"Dog.png173732": {
"filename": "Dog.png",
"size": 173732,
"regions": [
{
"shape_attributes": {
"name": "polygon",
"all_points_x": [
189,
192,
229,
230
],
"all_points_y": [
2,
148,
148,
2
]
},
"region_attributes": {
"Animal": "Dog"
}
}
],
"file_attributes": {}
},
To replicate this in C#, I created a class with multiple getters and setters, while using the Nuget package JSON.Net, to serialize my JSON.
public class Animal
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string Filename { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public int? Size { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<Animal> Regions { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<Animal> Shape_attributes { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<int> All_points_x { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<int> All_points_y { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<Animal> Region_attributes { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string Animal { get; set; }
public void CreateJSON()
{
string output = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) +
@"\source\repos\Testing\";
var root = new Animal()
{
Filename = "Dog.png",
Size = 173732,
Regions = new List<Animal>()
{
new Animal()
{
Size = null,
Shape_attributes = new List<Animal>()
{
new Animal()
{
Name = "polygon",
All_points_x = All_points_x,
All_points_y = All_points_y
}
},
Region_attributes = new List<Animal>()
{
new Animal()
{
Animal = "Dog"
}
}
}
}
};
var json = JsonConvert.SerializeObject(root, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
File.WriteAllText(output + "testing.json", json);
}
Once I execute the following code, my output is as follows.
{
"filename": "Dog.png",
"size": 173732,
"regions": [
{
"shape_attributes": [
{
"name": "polygon",
"all_points_x": [
389,
43
],
"all_points_y": [
33,
215
]
}
],
"region_attributes": [
{
"animal": "Dog"
}
]
}
]
}
How can I adjust my code to better replicated via's Json file? Is there a better way that I could be doing things?
Animalinside region attributes and the fact that the filename and filesize are a property on the parent object that are missing (and thefile_attributes. Do you only have to write out the information, or do you also have to read it in? If you have to write it out, just addJsonProperty("Animal")and serializeIDictionary<string,object>where the dictionary has your key, and the object is the animal