Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to serialize and store objects in c#?

Im looking for a simple solution to serialize and store objects that contain configuration, application state and data. Its a simple application, its not alot of data. Speed is no issue. I want it to be in-process. I want it to be more easy-to-edit in a texteditor than xml.

I cant find any document database for .net that can handle it in-process. Simply serializing to xml Im not sure I want to do because its... xml. Serializing to JSON seems very javascript specific, and I wont use this data in javascript.

I figure there's very neat ways to do this, but atm im leaning to using JSON despite its javascript inclenation.

like image 378
MatteS Avatar asked Jul 16 '11 18:07

MatteS


People also ask

Which method is used to serialize an object to open?

To serialize an object hierarchy, you simply call the dumps() function. Similarly, to de-serialize a data stream, you call the loads() function.

Does serialization compress the data?

Compact serialization is also binary serialization but NCache provides a lot more flexibility with this serialization framework. Compact serialization does not reduce the size of the entire data, it instead only reduces the size of the data that is travelled over the network.


2 Answers

Just because "JSON" it's an acronym for JavaScript Object Notation, has no relevance on if it fits your needs or not as a data format. JSON is lightweight, text based, easily human readable / editable and it's a language agnostic format despite the name.

I'd definitely lean toward using it, as it sounds pretty ideal for your situation.

like image 172
Brandon Moretz Avatar answered Sep 17 '22 22:09

Brandon Moretz


I will give a couple of choices :

  1. Binary serialization: depends on content of your objects, if you have complicated dependecy tree it can create a problems on serializing. Also it's not very flexible, as standart binary serialization provided by Microsoft stores saving type information too. That means if you save a type in binary file, and after one month decide to reorganize your code and let's say move the same class to another namespace, on desirialization from binary file previously saved it will fail, as the type is not more the same. There are several workarrounds on that, but I personally try to avoid that kind of serialization as much as I can.

  2. ORM mapping and storing it into small database. SQLite is awesome choice for this kind of stuff as it small (single file) and full ACID support database. You need a mapper, or you need implement mapper by yourself.

I'm sure that you will get some other choice from the folks in a couple of minutes.

So choice is up to you.

Good luck.

like image 44
Tigran Avatar answered Sep 16 '22 22:09

Tigran