You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm on version 2.10.1 (latest) and below are the sample code, output and issues with it.
Code:
public class Test
{
public static class Car
{
public String model;
public Map<String, Integer> properties;
}
public static void main( String[] args )
{
Map<String, Integer> carProperties = new HashMap<>();
carProperties.put("Speed", 100);
carProperties.put("Weight", null);
Car car = new Car();
car.model = "F60";
car.properties = carProperties;
ObjectMapper mapper1 = new ObjectMapper();
mapper1.setSerializationInclusion(JsonInclude.Include.NON_NULL);
System.out.println("Test1: Include.NON_NULL on ObjectMapper");
System.out.println("CarProperties -> " + mapper1.writeValueAsString(carProperties));
System.out.println("Car -> " + mapper1.writeValueAsString(car));
System.out.println();
ObjectMapper mapper2 = new ObjectMapper();
mapper2.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper2.configOverride(Map.class).setInclude(JsonInclude.Value.construct(JsonInclude.Include.USE_DEFAULTS, JsonInclude.Include.USE_DEFAULTS));
System.out.println("Test2: Include.NON_NULL on ObjectMapper and Include.USE_DEFAULTS override on Map class ");
System.out.println("CarProperties -> " + mapper2.writeValueAsString(carProperties));
System.out.println("Car -> " + mapper2.writeValueAsString(car));
System.out.println();
ObjectMapper mapper3 = new ObjectMapper();
mapper3.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper3.configOverride(Map.class).setInclude(JsonInclude.Value.construct(JsonInclude.Include.ALWAYS, JsonInclude.Include.ALWAYS));
System.out.println("Test3: Include.NON_NULL on ObjectMapper and Include.ALWAYS override on Map class ");
System.out.println("CarProperties -> " + mapper3.writeValueAsString(carProperties));
System.out.println("Car -> " + mapper3.writeValueAsString(car));
}
}
Output:
Test1: Include.NON_NULL on ObjectMapper
CarProperties -> {"Speed":100}
Car -> {"model":"F60","properties":{"Speed":100}}
Test2: Include.NON_NULL on ObjectMapper and Include.USE_DEFAULTS override on Map class
CarProperties -> {"Speed":100,"Weight":null}
Car -> {"model":"F60","properties":{"Speed":100}}
Test3: Include.NON_NULL on ObjectMapper and Include.ALWAYS override on Map class
CarProperties -> {"Speed":100,"Weight":null}
Car -> {"model":"F60","properties":{"Speed":100}}
Issues:
In Test2, Weight property is included when properties map is serialized directly. Config override for Map class is set to USE_DEFAULTS which ultimately should resolve to NON_NULL as set on the mapper object, so weight should not be included and the output should be the same as Test1.
In Test3, Weight property is missing when Car is serialized. Include for Map class has been overridden with ALWAYS yet Weight property with null value is ignored when serializing the class. However, it is included when the map is serialized directly.
The text was updated successfully, but these errors were encountered:
I'm on version
2.10.1
(latest) and below are the sample code, output and issues with it.Code:
Output:
Issues:
In
Test2
,Weight
property is included when properties map is serialized directly. Config override forMap
class is set toUSE_DEFAULTS
which ultimately should resolve toNON_NULL
as set on the mapper object, so weight should not be included and the output should be the same asTest1
.In
Test3
,Weight
property is missing whenCar
is serialized.Include
forMap
class has been overridden withALWAYS
yetWeight
property with null value is ignored when serializing the class. However, it is included when the map is serialized directly.The text was updated successfully, but these errors were encountered: