I'm trying to make a method, which uses the Json data with jackson library to have a list of objects. If I run my code, I get the error:
java.lang.reflect.InaccessibleObjectException: Unable to make field private final int java.time.LocalDate.year accessible: module java.base does not "opens java.time" to unnamed module @5c90e579
Why is it giving me an error about LocalDate even though I have JodaModul in my Code?
public static List<Tweet> getTweetsFile() throws Exception{
ObjectMapper mapper = new ObjectMapper().
registerModule(new JodaModule()).
configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
File path = new File ("C:/Users/PC/eclipse-workspace/hw4/tw.json");
List<Tweet> myObjects3 = Arrays.asList(mapper.readValue(path, Tweet.class));
return myObjects3;
}
How it looks in my File:
[{"date":"2001-12-28","tweetNumber":1,"country":"England","comments":11,"message":"I like to watch anime and reading books","retweets":3,"username":"Isabelle","likes":55},{"date":"2003-05-11","tweetNumber":2,"country":"France","comments":25,"message":"I'm Viatnamese, but I live in France","retweets":30,"username":"Vin","likes":110}..
It's not in the right order like my Object has in their constructor, could that be the reaseon?
You can fix this issue on your own.
add --add-opens java.base/java.time=ALL-UNNAMED
as start-argument and it'll work.
Here is the corresponding JEP
I used this way and fixed this Problem. Firstly you have to create LocalDateTime Deserializer with the given Date Format in your Json Data.
class LocalDateTimeDeserializer implements JsonDeserializer < LocalDateTime > {
@Override
public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return LocalDateTime.parse(json.getAsString(), DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss").withLocale(Locale.ENGLISH));
}
}
Then create Object of Type GsonBuilder and adapt it with your LocalDateDeserializer.
gsonBuilder.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer());
Gson gson = gsonBuilder.setPrettyPrinting().create();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With