简体   繁体   English

Java“设置对象”,序列化/反序列化

[英]Java “settings object”, serialization/deserialization

( Code is for Android Actually, I need code to be portable between Android and Java SE.) 代码适用于Android实际上,我需要在Android和Java SE之间移植代码。)

I want to have a "settings" class with various game settings, like 我希望有一个“设置”类,其中包含各种游戏设置

public int map_size;
public String server_name;

etc. 等等

The data needs to be accessed fairly frequently (so members, not a key-value map), and from time to time de/serialized in some standard way (mainly to send it through network). 数据需要相当频繁地访问(因此成员,而不是键值映射),并且以某种标准方式(主要通过网络发送)不时地进行序列化。

I want to be able to 我希望能够

  1. Serialize and deserialize the object into XML or JSON, without having to explicitly write the code for every member (but still having some degree of control over the format). 将对象序列化和反序列化为XML或JSON,而不必为每个成员显式编写代码(但仍然对格式有一定程度的控制)。

  2. Define some (constant) meta-data about every member (default value, GUI name, XML identifier, ...), in a way that allows for easy modification in the source code (I want to be able to add a new meta-property, define a default value for it, and not have to specify it everywhere else). 以允许在源代码中轻松修改的方式定义关于每个成员的一些(常量)元数据(默认值,GUI名称,XML标识符......)(我希望能够添加新的元素 - property,为其定义默认值,而不必在其他地方指定它。

1 is achievable by using reflection. 通过使用反射可以实现图1的实施例。 I thought Java annotations for class members would be perfect for 2: 我认为类成员的Java注释对于2来说是完美的:

@Setting(id = "server_name", name = "Server title", default = "Server0")
public String server_name;

But it looks like (user-defined) annotations don't work in Android yet - code using them crashes the compiler... 但看起来(用户定义的)注释在Android中不起作用 - 使用它们的代码会使编译器崩溃...

What would be the easiest way to store the meta-data about the settings (or another way to approach all this)? 存储有关设置的元数据 (或其他方法来解决所有这些问题) 最简单的方法是什么?

  • Store information about settings in some external XML file? 在一些外部XML文件中存储有关设置的信息?

  • Store it in a Java data structure, with content defined in the code? 将其存储在Java数据结构中,并在代码中定义内容? Defining the data in this way somehow seems very unwieldy, especially compared to keyword arguments of annotations. 以这种方式定义数据在某种程度上看起来非常笨拙,特别是与注释的关键字参数相比。

  • ?

FYI, it looks like Jackson was fixed to work with Android in the Jackson 0.9.7 release. 仅供参考,它看起来像杰克逊被固定在杰克逊0.9.7版本与Android合作。

Though I agree with Daniel Lew that using the built-in Android preferences is the best solution for an Android client. 虽然我同意Daniel Lew的说法,使用内置的Android偏好设置是Android客户端的最佳解决方案。 For a JavaSE client the Properties class is a good way to store preferences. 对于JavaSE客户端,Properties类是存储首选项的好方法。 There's also a JavaSE preferences package, but it may do more then you need. 还有一个JavaSE首选项包,但它可能会做更多你需要的。

Is there any particular reason you're not using built-in Android preferences ? 您是否有任何特殊原因没有使用内置的Android偏好设置 As long as all the game settings are primitives (or Strings) then that's probably the easiest way to store preferences. 只要所有游戏设置都是原始(或字符串),那么这可能是存储首选项的最简单方法。 Also, it synchronizes well with PreferenceActivity if you end up making a settings page (there are some Preference examples in the ApiDemos). 此外,如果您最终创建设置页面,它与PreferenceActivity同步良好(ApiDemos中有一些首选项示例)。

While I'm not sure how well they work on Android, XStream or Jackson provide highly customizable XML or JSON (de)serialization of Java objects. 虽然我不确定它们在Android上的效果如何,但XStreamJackson提供了高度可定制的Java对象的Java或JSON(反)序列化。 Note that XStream supports both XML and JSON output, Jackson is just JSON. 请注意,XStream支持XML和JSON输出,Jackson只是JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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