将业务对象序列化为 JSON
我正在尝试将我的业务对象序列化为 JSON,以供 Javascript 应用程序使用。 问题是我试图保持我的业务对象“纯粹”,因为它们不知道数据访问或持久性。 在我看来,用 toJSON() 函数“稀释”我的对象会违背这个目标。 另一方面,使用外部对象序列化我的业务对象将不起作用,因为我将所有实例变量保持为私有。
我处理这个问题的方式完全错误吗?
I'm trying to serialize my business objects into JSON for consumption by a Javascript application. The problem is is that I'm trying to keep my business objects "pure" in the sense they are not aware of data access or persistence. It seems to me that "diluting" my objects with a toJSON() function would go against this goal. On the other hand, using an external object to serialize my business objects would not work since I keep all my instance variables private.
Am I approaching this totally the wrong way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果实例变量是私有的,则它们不应出现在发送到 JavaScript 应用程序的序列化中。 根据定义,如果您将它们序列化并将它们发送到单独的应用程序,则它们是公共的。 因此,外部对象应该有某种方式来访问它们,可能是通过某种 getter 方法。
If the instance variables are private, they should not appear in a serialization that is being sent to a JavaScript application. By definition, if you're serializing them and sending them to a separate application, they are public. So, an external object should have some way of accessing them, probably through some sort of getter methods.
序列化 JSON 数据的目的是什么? 纯粹是为了举报吗? 如果是这样,那么 Brian 是对的,这些变量应该有 getter 方法。
如果序列化的目的是将数据传输到可以对其进行操作的 JavaScript 应用程序,然后返回到原始应用程序,那么也许您最好创建一个用于序列化目的的相关类,同时仍然保持强大的封装性。
例如,在 Java 中,您可以定义一个内部类。 内部类实例可以直接访问封闭类实例的所有字段,而不需要 getter 方法。 或者,您可以使用正确的访问修饰符对包(或命名空间)进行分组,以允许序列化程序进行访问,但不允许任何其他类进行访问。
或者你可以使用反射。 或者劫持 toString 方法。 (或者将其全部发挥出来并创建一个 toJson 方法。)
What purpose does searializing the data in JSON serve? Is it purely for reporting? If so, then Brian is correct, those variables should have getter methods.
If the purpose of serialization is to transport the data to a JavaScript app where it can be manipulated and then returned to the originating application, then perhaps you would be best served by creating a related class that serves the purpose of serialization while still maintaining strong encapsulation.
For example, in Java you could define an inner class. An inner class instance has direct access to all fields of the enclosing class instance without the need of getter methods. Or you could group with a package (or namespace) using the correct access modifiers to permit access by the serializer, but not by any other class.
Or you could use reflection. Or hijack the toString method. (Or shine it all and create a toJson method.)
您是否正在考虑从非 javascript 代码(如服务器端 Java)生成 JSON? 答案取决于:JSON 的处理在 Javascript 和 Java 上有很大不同。 javascript 端已经有一个答案,这似乎是正确的。
如果这是在 Java 上,有一些库可以提供帮助; 例如 (Jackson) 可以使用常规 getX/setX 方法内省来反序列化任何 bean; 加上附加(和可选)注释。
Are you thinking of generating JSON from non-javascript code (like server-side Java)? The answer kind of depends on that: handling of JSON is quite different on Javascript and, say, Java. There's already an answer wrt javascript-side, which seems correct.
If this is on Java, there are libraries that can help; for example (Jackson) can deserialize any bean, using regular getX/setX method introspection; plus additional (and optional) annotations.