I have the following class, and I do output the objects of the class in Json string format:
public final class MyClass {
private String field1 = null;
private String field2 = null;
@Override
public final String toString() {
return toJsonString();
}
public final String toJsonString() {
return (new Gson()).toJson(this);
}
:
:
public static void main(String[] args) {
MyClass a = new MyClass();
a.field1 = "Hello";
System.out.println(a);
}
}
The above code, the main program output is below:
{"field1":"Hello"}
Is it possible to make it output:
{"field1":"Hello", "field2":null}
Thanks!