Java 可以将 JSON 输出用作 toString() 的默认值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16527932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OK to use JSON output as default for toString()?
提问by tortal
@Override
public String toString() {
return new Gson().toJson(this);
}
Am I breaking some good practice, "Joshua"-patternthing, general design pattern or other convention by simply doing this as default behavior for my model objects?
通过简单地将其作为我的模型对象的默认行为,我是否打破了一些良好的实践,“约书亚”模式的东西,通用设计模式或其他约定?
toString()
will anyhow only be used in debugging in the paradigm (Android) that we are currently using. That's also the reason why I like seeing the object in JSON since much ORM/json persistence will be happening through http->php/python->mysql and to the local SQLite.
toString()
无论如何只能用于我们当前使用的范式(Android)中的调试。这也是我喜欢在 JSON 中看到对象的原因,因为很多 ORM/json 持久性将通过 http->php/python->mysql 和本地 SQLite 发生。
采纳答案by Amit Kaneria
Yes. It's OK to use GSON/Hymanson/Reflections library to implement toString() method.
是的。可以使用 GSON/Hymanson/Reflections 库来实现 toString() 方法。
There are few ways to implement toString method.
有几种方法可以实现 toString 方法。
Reflections (Apache library)
@Override public String toString(){ return org.apache.commons.lang3.builder.ReflectionToStringBuilder.toString(this); }
JSON based implementation (GSON, Hymanson libraries)
// GSON library for JSON @Override public String toString(){ return new com.google.gson.Gson().toJson(this); } // Hymanson libabry for JSON/YAML @Override public String toString() { try { return new com.fasterxml.Hymanson.databind.ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this); } catch (com.fasterxml.Hymanson.core.JsonProcessingException e) { e.printStackTrace(); } return null; }
ToStringBuilder (available with apache-commons library)
@Override public String toString() { return new org.apache.commons.lang3.builder.ToStringBuilder(this). append("field1", field1). append("field2", field2). toString(); }
Hard-core toString() implementation
@Override public String toString() { return new StringBuilder() .append("field1:"+field1) .append("field2:"+field2) .toString(); }
Lombok annotations : Generates toString() at compile time
import lombok.ToString; @ToString public class ToStringExample {}
反射(Apache 库)
@Override public String toString(){ return org.apache.commons.lang3.builder.ReflectionToStringBuilder.toString(this); }
基于 JSON 的实现(GSON、Hymanson 库)
// GSON library for JSON @Override public String toString(){ return new com.google.gson.Gson().toJson(this); } // Hymanson libabry for JSON/YAML @Override public String toString() { try { return new com.fasterxml.Hymanson.databind.ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this); } catch (com.fasterxml.Hymanson.core.JsonProcessingException e) { e.printStackTrace(); } return null; }
ToStringBuilder(可用于 apache-commons 库)
@Override public String toString() { return new org.apache.commons.lang3.builder.ToStringBuilder(this). append("field1", field1). append("field2", field2). toString(); }
核心 toString() 实现
@Override public String toString() { return new StringBuilder() .append("field1:"+field1) .append("field2:"+field2) .toString(); }
Lombok 注释:在编译时生成 toString()
import lombok.ToString; @ToString public class ToStringExample {}
回答by Egor Neliuba
There's no harm in doing it this way. I would suggest you to create a static variable for your Gson
instance and enable pretty printing:
这样做没有坏处。我建议您为您的Gson
实例创建一个静态变量并启用漂亮的打印:
static Gson gson = new GsonBuilder().setPrettyPrinting().create();
This way the output from toString
method will be formatted.
这样,方法的输出toString
将被格式化。
回答by tbraun
It's bad for performance because Gson uses introspection to figure out which fields to print.
这对性能不利,因为 Gson 使用自省来确定要打印的字段。
Apart from that, I think it's ok. That's not the standard Java toString implementation but I don't think changing it would be an anti-pattern.
除此之外,我觉得还可以。这不是标准的 Java toString 实现,但我认为改变它不会是一种反模式。
回答by JWT
NOTE: If you use that GSon pretty printing in your toString() method it is going to look like garbage in your debugger because it will be full of newlines.
注意:如果你在你的 toString() 方法中使用那个 GSon 漂亮的打印它在你的调试器中看起来像垃圾,因为它会充满换行符。
(Sorry didn't have enough rep to comment above)
(抱歉没有足够的代表在上面发表评论)