com.fasterxml.jackson.annotation.Nulls Java Examples

The following examples show how to use com.fasterxml.jackson.annotation.Nulls. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NullConversionsForContentTest.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
public void testNullsSkipWithOverrides() throws Exception
{
    final String JSON = aposToQuotes("{'values':[null]}");
    TypeReference<NullContentSkip<List<Long>>> listType = new TypeReference<NullContentSkip<List<Long>>>() { };

    ObjectMapper mapper = afterburnerMapperBuilder()
    // defaults call for fail; but POJO specifies "skip"; latter should win
            .changeDefaultNullHandling(n -> n.withContentNulls(Nulls.FAIL))
            .build();
    NullContentSkip<List<Long>> result = mapper.readValue(JSON, listType);
    assertEquals(0, result.values.size());

    // ditto for per-type defaults
    mapper = afterburnerMapperBuilder()
            .withConfigOverride(List.class,
                    o -> o.setNullHandling(JsonSetter.Value.forContentNulls(Nulls.FAIL)))
            .build();
    result = mapper.readValue(JSON, listType);
    assertEquals(0, result.values.size());
}
 
Example #2
Source File: NullConversionsForContentTest.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
public void testNullsSkipUsingDefaults() throws Exception
{
    final String JSON = aposToQuotes("{'values':[null]}");
    TypeReference<NullContentUndefined<List<Long>>> listType = new TypeReference<NullContentUndefined<List<Long>>>() { };

    // Let's see defaulting in action
    ObjectMapper mapper = afterburnerMapperBuilder()
            .changeDefaultNullHandling(n -> n.withContentNulls(Nulls.SKIP))
            .build();
    NullContentUndefined<List<Long>> result = mapper.readValue(JSON, listType);
    assertEquals(0, result.values.size());

    // or configured for type:
    mapper = afterburnerMapperBuilder()
            .withConfigOverride(List.class,
                    o -> o.setNullHandling(JsonSetter.Value.forContentNulls(Nulls.SKIP)))
            .build();
    result = mapper.readValue(JSON, listType);
    assertEquals(0, result.values.size());
}
 
Example #3
Source File: NullConversionsForContentTest.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
public void testNullsAsEmptyUsingDefaults() throws Exception
{
    final String JSON = aposToQuotes("{'values':[null]}");
    TypeReference<NullContentUndefined<List<Integer>>> listType = new TypeReference<NullContentUndefined<List<Integer>>>() { };

    // Let's see defaulting in action
    ObjectMapper mapper = afterburnerMapperBuilder()
            .changeDefaultNullHandling(n -> n.withContentNulls(Nulls.AS_EMPTY))
            .build();
    NullContentUndefined<List<Integer>> result = mapper.readValue(JSON, listType);
    assertEquals(1, result.values.size());
    assertEquals(Integer.valueOf(0), result.values.get(0));

    // or configured for type:
    mapper = afterburnerMapperBuilder()
            .withConfigOverride(List.class,
                    o -> o.setNullHandling(JsonSetter.Value.forContentNulls(Nulls.AS_EMPTY)))
            .build();
    result = mapper.readValue(JSON, listType);
    assertEquals(1, result.values.size());
    assertEquals(Integer.valueOf(0), result.values.get(0));
}
 
Example #4
Source File: NullConversionsPojoTest.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
public void testNullsToEmptyScalar() throws Exception
{
    NullAsEmpty result = MAPPER.readValue(aposToQuotes("{'nullAsEmpty':'foo', 'nullsOk':null}"),
            NullAsEmpty.class);
    assertEquals("foo", result.nullAsEmpty);
    assertNull(result.nullsOk);

    // and then see that nulls are not ok for non-nullable
    result = MAPPER.readValue(aposToQuotes("{'nullAsEmpty':null}"),
            NullAsEmpty.class);
    assertEquals("", result.nullAsEmpty);

    // also: config overrides by type should work
    String json = aposToQuotes("{'name':null}");
    NullsForString def = MAPPER.readValue(json, NullsForString.class);
    assertNull(def.getName());

    ObjectMapper mapper = afterburnerMapperBuilder()
            .withConfigOverride(String.class,
                    o -> o.setNullHandling(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY)))
            .build();
    NullsForString named = mapper.readValue(json, NullsForString.class);
    assertEquals("", named.getName());
}
 
Example #5
Source File: PrimitiveArrayDeserializers.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
        BeanProperty property) throws JsonMappingException
{
    Boolean unwrapSingle = findFormatFeature(ctxt, property, _valueClass,
            JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    NullValueProvider nuller = null;

    Nulls nullStyle = findContentNullStyle(ctxt, property);
    if (nullStyle == Nulls.SKIP) {
        nuller = NullsConstantProvider.skipper();
    } else if (nullStyle == Nulls.FAIL) {
        if (property == null) {
            nuller = NullsFailProvider.constructForRootValue(ctxt.constructType(_valueClass));
        } else {
            nuller = NullsFailProvider.constructForProperty(property);
        }
    }
    if ((unwrapSingle == _unwrapSingle) && (nuller == _nuller)) {
        return this;
    }
    return withResolved(nuller, unwrapSingle);
}
 
Example #6
Source File: NullConversionsPojoTest.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
public void testFailOnNullWithDefaults() throws Exception
{
    // also: config overrides by type should work
    String json = aposToQuotes("{'name':null}");
    NullsForString def = MAPPER.readValue(json, NullsForString.class);
    assertNull(def.getName());
    
    ObjectMapper mapper = afterburnerMapperBuilder()
            .withConfigOverride(String.class,
                    o -> o.setNullHandling(JsonSetter.Value.forValueNulls(Nulls.FAIL)))
            .build();
    try {
        mapper.readValue(json, NullsForString.class);
        fail("Should not pass");
    } catch (InvalidNullException e) {
        verifyException(e, "property \"name\"");
    }
}
 
Example #7
Source File: SyncopeXmlMapper.java    From syncope with Apache License 2.0 5 votes vote down vote up
public SyncopeXmlMapper() {
    super();

    registerModule(new AfterburnerModule());

    registerModule(new JavaTimeModule());
    configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

    configOverride(List.class).setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
    configOverride(Set.class).setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
    configOverride(Map.class).setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
}
 
Example #8
Source File: PropertyMetadata.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.9
 */
protected PropertyMetadata(Boolean req, String desc, Integer index, String def,
        MergeInfo mergeInfo, Nulls valueNulls, Nulls contentNulls)
{
    _required = req;
    _description = desc;
    _index = index;
    _defaultValue = (def == null || def.isEmpty()) ? null : def;
    _mergeInfo = mergeInfo;
    _valueNulls = valueNulls;
    _contentNulls = contentNulls;
}
 
Example #9
Source File: StdDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method called to find {@link NullValueProvider} for a contents of a structured
 * primary property (Collection, Map, array), using
 * "content nulls" setting. If no provider found (not defined),
 * will return given value deserializer (which is a null value provider itself).
 *
 * @since 2.9
 */
protected NullValueProvider findContentNullProvider(DeserializationContext ctxt,
        BeanProperty prop, JsonDeserializer<?> valueDeser)
    throws JsonMappingException
{
    final Nulls nulls = findContentNullStyle(ctxt, prop);
    if (nulls == Nulls.SKIP) {
        return NullsConstantProvider.skipper();
    }
    NullValueProvider prov = _findNullProvider(ctxt, prop, nulls, valueDeser);
    if (prov != null) {
        return prov;
    }
    return valueDeser;
}
 
Example #10
Source File: StdDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Nulls findContentNullStyle(DeserializationContext ctxt, BeanProperty prop)
    throws JsonMappingException
{
    if (prop != null) {
        return prop.getMetadata().getContentNulls();
    }
    return null;
}
 
Example #11
Source File: MergeWithNullTest.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
public void testBeanMergingWithNullDefault() throws Exception
{
    // By default `null` should simply overwrite value
    ConfigDefault config = MAPPER.readerForUpdating(new ConfigDefault(5, 7))
            .readValue(aposToQuotes("{'loc':null}"));
    assertNotNull(config);
    assertNull(config.loc);

    // but it should be possible to override setting to, say, skip

    // First: via specific type override
    // important! We'll specify for value type to be merged
    ObjectMapper mapper = afterburnerMapperBuilder()
            .withConfigOverride(AB.class,
                    o -> o.setNullHandling(JsonSetter.Value.forValueNulls(Nulls.SKIP)))
            .build();
    config = mapper.readerForUpdating(new ConfigDefault(137, -3))
            .readValue(aposToQuotes("{'loc':null}"));
    assertNotNull(config.loc);
    assertEquals(137, config.loc.a);
    assertEquals(-3, config.loc.b);

    // Second: by global defaults
    mapper = afterburnerMapperBuilder()
            .changeDefaultNullHandling(n -> n.withValueNulls(Nulls.SKIP))
            .build();
    config = mapper.readerForUpdating(new ConfigDefault(12, 34))
            .readValue(aposToQuotes("{'loc':null}"));
    assertNotNull(config.loc);
    assertEquals(12, config.loc.a);
    assertEquals(34, config.loc.b);
}
 
Example #12
Source File: NullConversionsSkipTest.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
public void testSkipNullWithDefaults() throws Exception
{
    String json = aposToQuotes("{'value':null}");
    StringValue result = MAPPER.readValue(json, StringValue.class);
    assertNull(result.value);

    ObjectMapper mapper = afterburnerMapperBuilder()
            .withConfigOverride(String.class,
                     o -> o.setNullHandling(JsonSetter.Value.forValueNulls(Nulls.SKIP)))
            .build();
    result = mapper.readValue(json, StringValue.class);
    assertEquals("default", result.value);
}
 
Example #13
Source File: TlsOptions.java    From enmasse with Apache License 2.0 4 votes vote down vote up
@JsonSetter(nulls = Nulls.AS_EMPTY)
public void setVersions(List<String> versions) {
    this.versions = versions;
}
 
Example #14
Source File: NullConversionsGenericTest.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@JsonSetter(nulls=Nulls.AS_EMPTY)
public void setValue(T v) {
    value = v;
}
 
Example #15
Source File: NullConversionsSkipTest.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@JsonSetter(nulls=Nulls.SKIP)
public void setNoNulls(String v) {
    _noNulls = v;
}
 
Example #16
Source File: EnumTypeDefinition.java    From conjure with Apache License 2.0 4 votes vote down vote up
@JsonSetter(nulls = Nulls.FAIL, contentNulls = Nulls.FAIL)
List<EnumValueDefinition> values();
 
Example #17
Source File: StdDeserializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected final NullValueProvider _findNullProvider(DeserializationContext ctxt,
        BeanProperty prop, Nulls nulls, JsonDeserializer<?> valueDeser)
    throws JsonMappingException
{
    if (nulls == Nulls.FAIL) {
        if (prop == null) {
            return NullsFailProvider.constructForRootValue(ctxt.constructType(valueDeser.handledType()));
        }
        return NullsFailProvider.constructForProperty(prop);
    }
    if (nulls == Nulls.AS_EMPTY) {
        // cannot deal with empty values if there is no value deserializer that
        // can indicate what "empty value" is:
        if (valueDeser == null) {
            return null;
        }

        // Let's first do some sanity checking...
        // NOTE: although we could use `ValueInstantiator.Gettable` in general,
        // let's not since that would prevent being able to use custom impls:
        if (valueDeser instanceof BeanDeserializerBase) {
            ValueInstantiator vi = ((BeanDeserializerBase) valueDeser).getValueInstantiator();
            if (!vi.canCreateUsingDefault()) {
                final JavaType type = prop.getType();
                ctxt.reportBadDefinition(type,
                        String.format("Cannot create empty instance of %s, no default Creator", type));
            }
        }
        // Second: can with pre-fetch value?
        {
            AccessPattern access = valueDeser.getEmptyAccessPattern();
            if (access == AccessPattern.ALWAYS_NULL) {
                return NullsConstantProvider.nuller();
            }
            if (access == AccessPattern.CONSTANT) {
                return NullsConstantProvider.forValue(valueDeser.getEmptyValue(ctxt));
            }
        }
        return new NullsAsEmptyProvider(valueDeser);
    }
    if (nulls == Nulls.SKIP) {
        return NullsConstantProvider.skipper();
    }
    return null;
}
 
Example #18
Source File: PropertyMetadata.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @since 2.9
 */
public PropertyMetadata withNulls(Nulls valueNulls,
        Nulls contentNulls) {
    return new PropertyMetadata(_required, _description, _index, _defaultValue,
            _mergeInfo, valueNulls, contentNulls);
}
 
Example #19
Source File: ObjectTypeDefinition.java    From conjure with Apache License 2.0 4 votes vote down vote up
@JsonSetter(nulls = Nulls.FAIL, contentNulls = Nulls.FAIL)
Map<FieldName, FieldDefinition> fields();
 
Example #20
Source File: UnionTypeDefinition.java    From conjure with Apache License 2.0 4 votes vote down vote up
@JsonSetter(nulls = Nulls.FAIL, contentNulls = Nulls.FAIL)
Map<FieldName, FieldDefinition> union();
 
Example #21
Source File: PropertyMetadata.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * @since 2.9
 */
public Nulls getContentNulls() { return _contentNulls; }
 
Example #22
Source File: PropertyMetadata.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * @since 2.9
 */
public Nulls getValueNulls() { return _valueNulls; }