我正在用Java代码处理大量数字,并且由于JavaScript的局限性(即对Integers的32位支持),我需要在应用程序返回的JSON中将这些数字写为Strings。
是否存在允许我执行此操作的全局配置或注释?如果可能,我想避免编写自定义序列化器/适配器。
我正在将RestEasy与新的JSON-B / Yasson支持一起使用。
答案 0 :(得分:1)
我唯一想到的方法就是使用这样的适配器:
public void newspaper() {
System.out.println("Question 4 \n");
int youth;
double avg = 0;
int sum = 0;
int numYouth = 5;
List<Integer> number = new ArrayList<>();
// The loop for calculating the average
int ctr = 0;
for (int i = 0; i < 5; i++) {
System.out.println("Youth " + ++ctr + " How many was delivered?");
youth = in.nextInt();
number.add(youth);
sum = sum + youth;
avg = sum / numYouth;
}
System.out.println("Average is: " + avg + "\n");
ctr = 0;
// The loop for checking below of above average
for (int j : number) {
if (j > avg) {
System.out.println("Youth " + ++ctr + " is above average");
} else {
System.out.println("Youth " + ++ctr + " below average");
}
}
}
然后用以下注释您的财产:
import javax.json.bind.adapter.JsonbAdapter;
public class AdapterIntegerToString implements JsonbAdapter<Integer, String> {
@Override
public String adaptToJson(Integer obj) throws Exception {
return String.valueOf(obj);
}
@Override
public Integer adaptFromJson(String obj) throws Exception {
return Integer.parseInt(obj);
}
}
其他任何@JsonbTypeAdapter(AdapterIntegerToString.class)
private Integer age;
没有注释的内容将被默认处理。