[英]Best way to store user preferences on a website?(Not just string entries)
Lets say I want to store user preferences, but they aren't just string and integer preferences. 可以说我想存储用户首选项,但不仅仅是字符串和整数首选项。 Some of them are say java objects such as Color, JFreeChart chart settings, etc. What is the best way to do that? 其中有些是说Java对象,例如Color,JFreeChart图表设置等。做到这一点的最佳方法是什么? Would YAML be an option and just serialize that data into the application? 是否可以选择YAML并将其序列化到应用程序中? I want to avoid having to write translation code converting strings into java objects. 我想避免不得不编写将字符串转换为Java对象的翻译代码。 Thanks. 谢谢。
An option is to use XStream . 一种选择是使用XStream 。
It's easy to use, needs no external config* and converts Java objects to/from XML or JSON. 它易于使用,不需要外部配置*,并且可以将Java对象与XML或JSON相互转换。
*If you want to make the serialized preferences more portable or readable, you will need a couple of annotations. *如果您想使序列化的首选项更具可移植性或可读性,则需要几个注释。 But if the objective is just to serialize/deserialize Java objects into/from a roughly human-readable format, you don't even need annotations. 但是,如果目标只是将Java对象序列化/反序列化为大致人类可读的格式,那么您甚至不需要注释。
You can use some standard object-to-string mappings like XML or JSON. 您可以使用一些标准的对象到字符串的映射,例如XML或JSON。 That way you just use a 3rd party library to do the conversion (no need to write your own) 这样,您仅需使用第3方库即可进行转换(无需自己编写)
Where do you want to store your preferences? 您想在哪里存储您的偏好? If they will go to a DB, use a ORM. 如果他们要去数据库,请使用ORM。 If serializing them to a file is good enough, you can use XStream as biziclop suggests, and it can serialize to JSON and XML 如果将它们序列化为文件就足够了,则可以按照biziclop的建议使用XStream,它可以序列化为JSON和XML
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.