在c#中序列化和存储对象的最简单方法是什么?

时间:2021-12-14 14:04:28

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.

我正在寻找一个简单的解决方案来序列化和存储包含配置、应用程序状态和数据的对象。它是一个简单的应用程序,不是很多数据。速度是没有问题。我想让它在进程中。我希望它在texteditor中比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.

我找不到任何能在进程中处理它的。net文档数据库。简单地序列化xml我不确定我想做什么,因为……xml。序列化JSON似乎是javascript特有的,我不会在javascript中使用这些数据。

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

我认为有很好的方法可以做到这一点,但是atm我倾向于使用JSON,尽管它是javascript的。

2 个解决方案

#1


5  

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.

仅仅因为“JSON”是JavaScript对象表示法的首字母缩写,如果它符合您的需要或不符合数据格式,就没有相关性。JSON是轻量级的、基于文本的、易读/可编辑的,而且它是一种语言不可知的格式,尽管它的名称不同。

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

我肯定倾向于使用它,因为它听起来非常适合你的情况。

#2


1  

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.

    二进制序列化:取决于对象的内容,如果您有复杂的依赖树,那么它会在序列化时产生问题。它也不是很灵活,因为Microsoft store提供的标准二进制序列化也保存类型信息。这意味着,如果您将一个类型保存到二进制文件中,并且在一个月后决定重新组织您的代码,并让我们说将同一个类移动到另一个名称空间,那么在先前保存的二进制文件的desirialization上,它将会失败,因为类型不再相同。在这方面有一些工作,但我个人尽量避免这种序列化。

  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.

    ORM映射并将其存储到小型数据库中。SQLite是这种东西的极好选择,因为它很小(单个文件)和全酸支持数据库。您需要一个映射器,或者您需要自己实现映射器。

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.

祝你好运。

#1


5  

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.

仅仅因为“JSON”是JavaScript对象表示法的首字母缩写,如果它符合您的需要或不符合数据格式,就没有相关性。JSON是轻量级的、基于文本的、易读/可编辑的,而且它是一种语言不可知的格式,尽管它的名称不同。

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

我肯定倾向于使用它,因为它听起来非常适合你的情况。

#2


1  

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.

    二进制序列化:取决于对象的内容,如果您有复杂的依赖树,那么它会在序列化时产生问题。它也不是很灵活,因为Microsoft store提供的标准二进制序列化也保存类型信息。这意味着,如果您将一个类型保存到二进制文件中,并且在一个月后决定重新组织您的代码,并让我们说将同一个类移动到另一个名称空间,那么在先前保存的二进制文件的desirialization上,它将会失败,因为类型不再相同。在这方面有一些工作,但我个人尽量避免这种序列化。

  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.

    ORM映射并将其存储到小型数据库中。SQLite是这种东西的极好选择,因为它很小(单个文件)和全酸支持数据库。您需要一个映射器,或者您需要自己实现映射器。

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.

祝你好运。