.properties-file 是否适合存储分层设置对象? [英] Is a .properties-file suitable for storing hierarchical settings-objects?

查看:51
本文介绍了.properties-file 是否适合存储分层设置对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是在工作中完成一项任务,这需要客户端设置的持久性.这些设置由我正在实现的框架的 C# 版本中的 XML 文件表示,它表示具有多达六个级别的层次结构的对象.此外,不同级别中的对象数量有时是任意的,例如 ArrayList.不幸的是,对象序列化不是一种选择,因为需要能够直接编辑文件.这不仅仅是暗示我必须将这些设置的持久性实现为 .properties 文件,但我担心这是否是一个好主意.

I have been tasked with an assignement at work, that needs persistance of client side settings. These settings are represented by a XML-file in the C# version of the framework I am implementing, and it represents objects with an hierarchical structure of up to six levels. Also, the number of objects in the different levels are sometimes arbitrary, like an ArrayList. Object serialization is unfortunately not an option, due to a requirement of being able to edit the file directly. It has been more than implied that I have to implement the persistance of these settings as a .properties-file, but I am having some concerns about if that is a good idea.

据我所知,.properties-file 只能以与 HashMap 相同的方式使用,只有字符串的键值对.复杂对象的表示似乎只能使用长而复杂的键字符串,表示每个元素的整个路径",包括它在可能的多个列表中的单独索引.

As far as I know, a .properties-file can only be used in the same way as a HashMap, with a key-value pairing of only Strings. The representation of complex objects seems only possible with long, complex key strings, representing the whole "path" of every single element, including it's individual index in possibly several lists.

filters.filter3.traces.rule4.add1.value="8"

我无法找到任何体面的方法将分层对象持久保存在 .properties 文件中,但我也无法找到任何明确的证据证明这是不可能的.

I have not been able to find any decent ways to persist hierarchical objects in a .properties-file, but I have not been able to find any definite proof that it is not possible, either.

那么,.properties-file 是否适合存储分层设置对象?还是应该将我的属性实现为 XML 文件或 JSON?

推荐答案

我会坚持使用 XML 文件.没有什么比使用不同格式的 API 框架变体更能激怒用户的了.

I would stick with an XML file. Nothing irritates users more than having different formats with different variants of an API framework.

您可以使用 Apache XMLBeans 等库来简化读取和写入 XML 文件的过程,使用架构文件自动生成适当的 Java 类.

You can use libraries such as Apache XMLBeans to simplify the process of reading and writing to XML files, using schema files to auto-generate appropriate Java classes.

属性文件可用于存储分层数据,但手动编辑这些内容确实很痛苦,而且很容易引入错误.有些人会争辩说,XML 编辑也不是一件轻而易举的事,但至少有一些编辑器可以在与 XSD 链接时使这变得更容易.

Properties files can be used to store hierarchical data, but it's a real pain to edit that stuff by hand and it's easy to introduce errors. Some would argue that XML editing isn't a breeze either, but at least there exists editors that can make this easier when linked with an XSD.

这篇关于.properties-file 是否适合存储分层设置对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆