在 C# 中序列化和存储对象的最简单方法?

发布于 2024-11-24 20:07:38 字数 300 浏览 0 评论 0原文

我正在寻找一个简单的解决方案来序列化和存储包含配置、应用程序状态和数据的对象。它是一个简单的应用程序,数据不多。速度不是问题。我希望它正在进行中。我希望它在文本编辑器中比在 xml 中更容易编辑。

我找不到任何可以在进程中处理它的 .net 文档数据库。 简单地序列化为 xml 我不确定我想要做什么,因为它...... xml。 序列化为 JSON 似乎非常特定于 javascript,我不会在 javascript 中使用此数据。

我认为有一些非常巧妙的方法可以做到这一点,但 atm 我倾向于使用 JSON,尽管它有 javascript 倾向。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福不弃 2024-12-01 20:07:38

仅仅因为“JSON”是 JavaScript 对象表示法的缩写,与它是否适合您的需求或不作为数据格式无关。 JSON 是轻量级的、基于文本的、易于人类阅读/编辑的格式,尽管名称如此,但它是一种与语言无关的格式。

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

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.

﹂绝世的画 2024-12-01 20:07:38

我将给出几个选择:

  1. 二进制序列化:取决于对象的内容,如果您有复杂的依赖树,它可能会在序列化时产生问题。而且它也不是很灵活,因为 Microsoft 提供的标准二进制序列化也存储保存类型信息。这意味着,如果您将类型保存在二进制文件中,并在一个月后决定重新组织代码,假设将同一个类移动到另一个命名空间,那么从之前保存的二进制文件进行反序列化将会失败,因为类型不再相同。有几种解决方法,但我个人会尽力避免这种序列化。

  2. ORM 映射并将其存储到小型数据库中。 SQLite 是此类内容的绝佳选择,因为它很小(单个文件)并且完全支持 ACID 数据库。您需要一个映射器,或者您需要自己实现映射器。

我确信您会在几分钟内从人们那里得到其他选择。

所以选择取决于你。

祝你好运。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文