简体   繁体   中英

JSON.NET serialize and deserialize fragments

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?

I would rather encourage using a serializer better suited to this, such as protobuf-net created by our very own Marc Gravell , in lieu of trying to force JSON.NET into submission. Protobuf will allow you to stream data into a file and stream it back out very easily.

You simply declare that you're expecting a list vs requiring the deserializer to recognize that a list has been provided. As a side benefit, your resultant files will be smaller due to protocol buffers aggressive compaction.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM