I have queues of objects that I continuously flush to files for offline processing. Each object type gets its own file, and I want to use JSON.NET to serialize the objects.
so let’s say I have a Queue<LogInfo>, and every time I flush it, it will go to the same file. I serialize each object one at a time and append them to a string builder, separated by commas. So my file will look like.
{ "Id":1,"Name":"a"},
{ "Id":2,"Name":"b"},
Now when I want to process these, I read in the file, but precede all the text by a “[” and at the end I append a “]”, so that JSON.NET can deserialize the fragments as a list of LogInfo objects.
I realize this probably isn’t the intended use of the JSON serializer, but is there a better way? Is there I way I can read in the fragments (serialized objects) one-by-one without having to hack a [ and ] in there?, or a way to tell the serializer I’m giving it a comma-separated series of objects?
You’re doing it wrong. Just put them in a list and serialize that using Json.NET. One object per line isn’t a valid JSON document and if it is as you say, you just want a list anyway. So why are you not making it a list?