web123456

Java: null problem solving

Problem description:

.UnsupportedOperationException: null

Problem analysis:

1. UseGsonSerialize, but usefastjsonDeserialization is performed, because fastjson does not support Gson serializing the json data after LocalDate and LocalDateTime types, an error is reported.

  1. List<TestModel> testModelList = new ArrayList<>();
  2. TestModel testModel = new TestModel();
  3. (());
  4. (());
  5. (testModel);
  6. Gson gson = new Gson();
  7. String jsonStr = (testModelList);
  8. testModelList = (jsonStr, );
[{"localDate":{"year":2022,"month":5,"day":18},"localDateTime":{"date":{"year":2022,"month":5,"day":18},"time":{"hour":10,"minute":27,"second":34,"nano":979527900}}}]

Solution:

1. Deserialization using Gson.

  1. List<TestModel> testModelList = new ArrayList<>();
  2. TestModel testModel = new TestModel();
  3. (());
  4. (());
  5. (testModel);
  6. Gson gson = new Gson();
  7. String jsonStr = (testModelList);
  8. testModelList = (jsonStr, new TypeToken<List<TestModel>>() {
  9. }.getType());

2. The serialization and deserialization methods remain unchanged. LocalDate and LocalDateTime types are converted toStringtype.

  1. List<TestModel> testModelList = new ArrayList<>();
  2. TestModel testModel = new TestModel();
  3. (().format(("yyyy-MM-dd")));
  4. (().format(("yyyy-MM-dd HH:mm:ss")));
  5. (testModel);
  6. Gson gson = new Gson();
  7. String jsonStr = (testModelList);
  8. testModelList = (jsonStr, );