Reputation: 7217
I am writing out data into JSON format to be consumed by another program. I need to serialize the large number of JSON objects into a file. Using JSON.net this is really straight forward to serialize with the following lines of code:
[JsonConverter(typeof(SimpleClassSerializer))]
public class SimpleClass
{
public string name;
}
SimpleClass obj1 = new SimpleClass { name="exampleObj1" };
string serialised = JsonConvert.SerializeObject(obj1, Formatting.Indented);
Giving the output:
{
"name": "exampleObj1"
}
I will actually be working with multiple SimpleClass (example class) objects and want to serialize them all (as I receive them).
Using the following example code, this is nice and simple if I have an array or list:
SimpleClass[] arr1 = new SimpleClass[3];
SimpleClass obj1 = new SimpleClass { name="exampleObj1" };
SimpleClass obj2 = new SimpleClass { name = "exampleObj2" };
SimpleClass obj3 = new SimpleClass { name = "exampleObj3" };
arr1[0] = obj1;
arr1[1] = obj2;
arr1[2] = obj3;
string serialised = JsonConvert.SerializeObject(arr1, Formatting.Indented);
Giving me the following output:
[
{
"name": "exampleObj1"
},
{
"name": "exampleObj2"
},
{
"name": "exampleObj3"
}
]
This is easily deserialized into an array.
However, I cannot declare an array up front (for example string[] arr = new string[50000]) because I wont know the number of objects I am dealing with and because of memory constraints (the data in each object is huge). I cannot use a list mainly because of memory constraints as well.
If I do the following:
SimpleClass obj1 = new SimpleClass { name="exampleObj1" };
SimpleClass obj2 = new SimpleClass { name = "exampleObj2" };
SimpleClass obj3 = new SimpleClass { name = "exampleObj3" };
string serialised = JsonConvert.SerializeObject(obj1, Formatting.Indented);
serialised += JsonConvert.SerializeObject(obj2, Formatting.Indented);
serialised += JsonConvert.SerializeObject(obj3, Formatting.Indented);
Giving me this:
{
"name": "exampleObj1"
}{
"name": "exampleObj2"
}{
"name": "exampleObj3"
}
The main thing here is that I have an open file and I am streaming the serialized objects into the file as I receive them. Giving me this format shown above. When attempting to deserialize such an object, I get an error as it is basically expecting it to be an array.
Whats the best approach to achieve continuous serialization in this manner?
Ideally if i could do some custom serialization where I start out writing "[" into the file at the beginning and "]" at the end (and the comma's inbetween each object!), then the consuming app could deserialize into an array. How do I do this?
Upvotes: 1
Views: 832
Reputation: 7217
Based on the information given to me by cgotberg (sorry dont seem to be able to mark a comment as the answer! :-s)
Here was the solution:
using (FileStream fs = File.Open(@"c:\temp\a.json", FileMode.Create))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
JsonSerializer serializer = new JsonSerializer();
sw.Write("[");
serializer.Serialize(jw, obj1);
sw.Write(",");
serializer.Serialize(jw, obj2);
sw.Write(",");
serializer.Serialize(jw, obj3);
sw.Write("]");
}
Upvotes: 2