First of all, I think I am trying to serialize and deserialize dynamically, not programmatically. Sorry if I made a mistake in the title.
I am a Software Engineering student, and I am trying to learn a bit about JSON. I created a class (I will probably use it as .dll for my projects) to serialize and deserialize.
public class JSONParser
{
public object JsonDeserialize(Type dataType, string filePath)
{
JsonSerializer jsonSerializer = new JsonSerializer();
StreamReader sr = new StreamReader(filePath);
JsonReader jsonReader = new JsonTextReader(sr);
JObject obj = jsonSerializer.Deserialize(jsonReader) as JObject;
jsonReader.Close();
sr.Close();
return obj.ToObject(dataType);
}
public void JsonSerialize(object data, string filePath)
{
JsonSerializer jsonSerializer = new JsonSerializer();
if (File.Exists(filePath))
{
File.Delete(filePath);
}
StreamWriter sw = new StreamWriter(filePath);
JsonWriter jsonWriter = new JsonTextWriter(sw);
jsonWriter.Formatting = Formatting.Indented;
jsonSerializer.Serialize(jsonWriter, data);
jsonWriter.Close();
sw.Close();
}
}
And I call it like so:
Animal animal = new Animal
{
AnimalName = AnimalNameTextBox,
AnimalBreed = AnimalBreedTextBox,
};
AnimalList Animal = new AnimalList ();
JSONParser jsonParser = new JSONParser();
if (File.Exists(_animalFilePath))
{
Animal = jsonParser.JsonDeserialize(typeof(AnimalList), _animalFilePath) as AnimalList;
}
Animal.ListOfAnimals.Add(animal);
jsonParser.JsonSerialize(Animal, _animalFilePath);
Here is Animal Class:
public class Animal
{
public string AnimalName { get; set; }
public string AnimalBreed { get; set; }
public Animal()
{
}
public Animal(string AnimalName, string AnimalBreed)
{
this.AnimalName = AnimalName;
this.AnimalBreed = AnimalBreed;
}
}
Here is AnimalList Class:
public class AnimalList
{
public List<Animal> ListOfAnimals { get; set; }
public Animal NewAnimal { get; set; }
public string AnimalName { get; set; }
public string AnimalBreed { get; set; }
public AnimalList()
{
ListOfAnimals = new List<Animal>();
}
}
It works great so far.
The JSON object I get while I deserialize it using
JObject obj = jsonSerializer.Deserialize(jsonReader) as JObject;
is
{{
"ListOfAnimals": [
{
"AnimalName": "Doggie",
"AnimalBreed": "Dog"
}
],
"NewAnimal": null,
"AnimalName": null,
"AnimalBreed": null
}}
But, I feel I shouldn't need to create an AnimalList class in order to do this, but if I don't do so, when I try to deserialize, JSON doesn't know how to interpret the information inside the JSON file. I cannot figure it out how to, dynamically, tell JSON that it is a List of Animals.
This is the content of the serialized file created with this method
{
"ListOfAnimals": [
{
"AnimalName": "Doggie",
"AnimalBreed": "Dog"
},
{
"AnimalName": "Gatito",
"AnimalBreed": "Cat"
}
],
"NewAnimal": null,
"AnimalName": null,
"AnimalBreed": null
}
I tried without the class AnimalList (just a List<> of Animal), and it creates this file
[
{
"AnimalName": "Doggie",
"AnimalBreed": "Dog"
}
]
So, it will serialize it but it doesn't know how to deserialize it (this JObject will return null).
JObject obj = jsonSerializer.Deserialize(jsonReader) as JObject;
What am I doing wrong? Sorry if it is too lengthy; I tried to be as clear as possible.
Thanks
>()`. See [this](https://www.newtonsoft.com/json/help/html/DeserializeCollection.htm) (I assume you are using json.net).
>(typeof(List), _animalFilePath) as List`;
The object still comes back null.
I think I am overcomplicating things. Am I?