-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Milestone
Description
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 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.
Metadata
Metadata
Assignees
Labels
No labels