Just quote.
Gson Gson is a Java library that can be used to convert Java Objects into their JSON >representation. It can also be used to convert a JSON string to an equivalent Java object. >Gson can work with arbitrary Java objects including pre-existing objects that you do not have >source-code of.
There are a few open-source projects that can convert Java objects to JSON. However, most of >them require that you place Java annotations in your classes; something that you can not do if >you do not have access to the source-code. Most also do not fully support the use of Java >Generics. Gson considers both of these as very important design goals.
Person1.java
import java.util.Date;
public class Person1 {
private Integer age;
private String name;
private String noUse;
private Date target;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String noUse() {
return noUse;
}
public void noUse(String noUse) {
this.noUse = noUse;
}
public Date target() {
return target;
}
public void target(Date target) {
this.target = target;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Person1 [age=");
builder.append(age);
builder.append(", name=");
builder.append(name);
builder.append(", noUse=");
builder.append(noUse);
builder.append(", target=");
builder.append(target);
builder.append("]");
return builder.toString();
}
}
Ex1.java
public class Sample {
public static void main(String[] args){
//-----------------------------
//jsonReader experiment
//-----------------------------
StringBuilder jsonReader = new StringBuilder();
jsonReader.append("[");
jsonReader.append(" {\"age\":1,\"name\":\"Gson Taro\",\"target\":\"2020-02-10\"},");
jsonReader.append(" {\"age\":2,\"name\":\"Gson Taro\",\"target\":\"2020-02-30\"},");
jsonReader.append(" {\"age\":3,\"name\":\"Gson Taro\",\"target\":\"2020-02-12\"}");
jsonReader.append("]");
try {
Gson gsonReader = new Gson();
Type listType = new TypeToken<List<Person1>>(){}.getType();
List<Person1> p1 = gsonReader.fromJson(jsonReader.toString(), listType);
System.out.println("------------------------------------------->");
System.out.println("Person1#toString: " + p1);
System.out.println("------------------------------------------->");
} catch (Exception e) {
System.out.println(e.getCause());
System.out.println("------------------------------------------->");
if(e.getCause() instanceof NumberFormatException) {
System.out.println("Numerical error:" + e.getCause().getMessage());
}else if (e.getCause() instanceof ParseException){
System.out.println("Date input error:" + e.getCause().getMessage());
}
System.out.println("------------------------------------------->");
}
}
It's a success without difficulty. The part that passes the type you want to convert to Gson is the liver.
------------------------------------------->
Person1#toString: [Person1 [age=1, name=Gson Taro, noUse=null, target=Mon Feb 10 00:00:00 JST 2020], Person1 [age=2, name=Gson Taro, noUse=null, target=Sun Mar 01 00:00:00 JST 2020], Person1 [age=3, name=Gson Taro, noUse=null, target=Wed Feb 12 00:00:00 JST 2020]]
------------------------------------------->
test.java
StringBuilder jsonReader = new StringBuilder();
jsonReader.append("[");
jsonReader.append(" {\"age\":null,\"name\":\"Gson Taro\",\"target\":\"2020-02-10\"},");
jsonReader.append(" {\"age\":2,\"name\":\"Gson Taro\",\"target\":\"2020-02-30\"},");
jsonReader.append(" {\"age\":3,\"name\":\"Gson Taro\",\"target\":\"2020-02-12\"}");
jsonReader.append("]");
By the way, if the recipient is an int type, it will be "0".
------------------------------------------->
Person1#toString: [Person1 [age=null, name=Gson Taro, noUse=null, target=Mon Feb 10 00:00:00 JST 2020], Person1 [age=2, name=Gson Taro, noUse=null, target=Sun Mar 01 00:00:00 JST 2020], Person1 [age=3, name=Gson Taro, noUse=null, target=Wed Feb 12 00:00:00 JST 2020]]
------------------------------------------->
test.java
StringBuilder jsonReader = new StringBuilder();
jsonReader.append("[");
jsonReader.append(" {\"age\":test,\"name\":\"Gson Taro\",\"target\":\"2020-02-10\"},");
jsonReader.append(" {\"age\":2,\"name\":\"Gson Taro\",\"target\":\"2020-02-30\"},");
jsonReader.append(" {\"age\":3,\"name\":\"Gson Taro\",\"target\":\"2020-02-12\"}");
jsonReader.append("]");
I get a NumberFormatException.
test.java
jsonReader.append("[");
jsonReader.append(" {\"age\":1,\"name\":\"Gson Taro\",\"target\":\"2020-02-10\"},");
jsonReader.append(" {\"age\":2,\"name\":\"Gson Taro\",\"target\":\"20200230\"},");
jsonReader.append(" {\"age\":3,\"name\":\"Gson Taro\",\"target\":\"2020/02/12\"}");
jsonReader.append("]");
Gson parses from the top line, left item on json, It raises an exception when the conversion fails. According to this experiment, in the case of "yyyy-MM-dd" and "yyyyMMdd" format, it is converted to a date, An exception will be thrown for "yyyy / MM / dd".
------------------------------------------->
Date input error: Failed to parse date["2020/02/12"]: Invalid number: _0
------------------------------------------->
test.java
StringBuilder jsonReader = new StringBuilder();
jsonReader.append("[");
jsonReader.append(" {\"age\":1,\"name\":\"Gson Taro\",\"target\":\"2020-02-10\"},");
jsonReader.append(" {\"age\":2,\"name\":\"Gson Taro\",\"target\":\"20200230\"},");
jsonReader.append(" {\"age\":3,\"name\":\"Gson Taro\",\"target\":\"20200212\"}");
jsonReader.append("]");
------------------------------------------->
Person1#toString: [Person1 [age=1, name=Gson Taro, noUse=null, target=Mon Feb 10 00:00:00 JST 2020], Person1 [age=2, name=Gson Taro, noUse=null, target=Sun Mar 01 00:00:00 JST 2020], Person1 [age=3, name=Gson Taro, noUse=null, target=Wed Feb 12 00:00:00 JST 2020]]
------------------------------------------->
Here, interestingly, 2020/2/30 is converted to 2020/3/1.
It is this part that is converted to Date. It looks like # L78). I think I can customize it here. .. ..
So, when I looked it up, in this part It seems that you can specify the date format. I will try using it.
test.java
jsonReader.append("[");
jsonReader.append(" {\"age\":1,\"name\":\"Gson Taro\",\"target\":\"2020-02-10\"},");
jsonReader.append(" {\"age\":2,\"name\":\"Gson Taro\",\"target\":\"20200230\"},");
jsonReader.append(" {\"age\":3,\"name\":\"Gson Taro\",\"target\":\"2020/02/12\"}");
jsonReader.append("]");
Gson gsonReader = new GsonBuilder().setDateFormat("yyyy/MM/dd").create();
Type listType = new TypeToken<List<Person1>>(){}.getType();
List<Person1> p1 = gsonReader.fromJson(jsonReader.toString(), listType);
System.out.println("------------------------------------------->");
System.out.println("Person1#toString: " + p1);
System.out.println("------------------------------------------->");
The result is a successful perspective!
------------------------------------------->
Person1#toString: [Person1 [age=1, name=Gson Taro, noUse=null, target=Mon Feb 10 00:00:00 JST 2020], Person1 [age=2, name=Gson Taro, noUse=null, target=Sun Mar 01 00:00:00 JST 2020], Person1 [age=3, name=Gson Taro, noUse=null, target=Wed Feb 12 00:00:00 JST 2020]]
------------------------------------------->
Sounds good. Very easy to use! !!
Recommended Posts