Mapper:
public static ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.enable(SerializationFeature.INDENT_OUTPUT);
Class to serialize:
@Builder(builderClassName = "GooglePlayGameBuilder", toBuilder = true)
@JsonDeserialize(builder = GooglePlayGame.GooglePlayGameBuilder.class)
public final class GooglePlayGame {
@JsonProperty("Title") private final String title;
@JsonProperty("Genre") private final String genre;
@JsonProperty("Price") private final String price;
@JsonProperty("Last updated") private final String lastUpdated;
@JsonProperty("Current version") private final String currentVersion;
@JsonProperty("Requirements") private final String requiresAndroid;
@JsonProperty("IAP") private final String IAP;
@JsonProperty("Contacts") private final String devEmail;
...
Add object to map and then i want to serialize my map:
public static volatile ConcurrentMap<String, GooglePlayGame> games = new ConcurrentSkipListMap<>(String.CASE_INSENSITIVE_ORDER);
Write to file:
public static void saveLibraryToFile(){
try {
mapper.writeValue(new File(LIBRARY_FILENAME), games);
} catch (IOException e) {
log.error("[Couldn't write to file] ", e.getMessage());
}
}
After this my JSON looks like:
{
"Never Alone: Ki Edition" : {
"Title" : "Never Alone: Ki Edition",
"Genre" : "Adventures",
"Price" : "4,99 €",
"Last updated" : "September 15, 2016",
"Current version" : "1.0.0",
"Requirements" : "2.3+",
"IAP" : "nope",
"Contacts" : "[email protected]"
},
...
If i will annotate my class with lombok @Getter strange field will appear:
{
"Never Alone: Ki Edition" : {
"iap" : "nope"
"Title" : "Never Alone: Ki Edition",
"Genre" : "Adventures",
"Price" : "4,99 €",
"Last updated" : "September 15, 2016",
"Current version" : "1.0.0",
"Requirements" : "2.3+",
"IAP" : "nope",
"Contacts" : "[email protected]"
},
I dont understand this field:
"iap" : "nope"
From where Jackson found it? I checked my local map with logs and all fine, this field doesnt exist, but during serialization it appears.