Java Code Examples for com.fasterxml.jackson.databind.SerializerProvider#defaultSerializeNull()

The following examples show how to use com.fasterxml.jackson.databind.SerializerProvider#defaultSerializeNull() . 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: AbstractTypedJacksonModule.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(T value, JsonGenerator generator, SerializerProvider provider)
        throws IOException
{
    if (value == null) {
        provider.defaultSerializeNull(generator);
        return;
    }

    try {
        Class<?> type = value.getClass();
        JsonSerializer<T> serializer = serializerCache.get(type, () -> createSerializer(provider, type));
        serializer.serializeWithType(value, generator, provider, typeSerializer);
    }
    catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause != null) {
            throwIfInstanceOf(cause, IOException.class);
        }
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: JoynrArraySerializer.java    From joynr with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(Object[] value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    gen.writeStartArray();
    for (Object elem : value) {
        if (elem == null) {
            provider.defaultSerializeNull(gen);
        } else {
            Class<?> clazz = elem.getClass();
            JsonSerializer<Object> serializer = serializers.get(clazz);
            if (serializer == null) {
                serializer = provider.findTypedValueSerializer(clazz, false, null);
            }
            serializer.serialize(elem, gen, provider);
        }
    }
    gen.writeEndArray();

}
 
Example 3
Source File: JoynrListSerializer.java    From joynr with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(List<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
                                                                                      JsonGenerationException {

    jgen.writeStartArray();
    for (Object elem : value) {
        if (elem == null) {
            provider.defaultSerializeNull(jgen);
        } else {
            Class<?> clazz = elem.getClass();
            JsonSerializer<Object> serializer = serializers.get(clazz);
            if (serializer == null) {
                serializer = provider.findTypedValueSerializer(clazz, false, null);
            }
            serializer.serialize(elem, jgen, provider);
        }
    }
    jgen.writeEndArray();

}
 
Example 4
Source File: CollectionSerializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void serializeContentsUsing(Collection<?> value, JsonGenerator g, SerializerProvider provider,
        JsonSerializer<Object> ser) throws IOException
{
    Iterator<?> it = value.iterator();
    if (it.hasNext()) {
        TypeSerializer typeSer = _valueTypeSerializer;
        int i = 0;
        do {
            Object elem = it.next();
            try {
                if (elem == null) {
                    provider.defaultSerializeNull(g);
                } else {
                    if (typeSer == null) {
                        ser.serialize(elem, g, provider);
                    } else {
                        ser.serializeWithType(elem, g, provider, typeSer);
                    }
                }
                ++i;
            } catch (Exception e) {
                wrapAndThrow(provider, e, value, i);
            }
        } while (it.hasNext());
    }
}
 
Example 5
Source File: HibernateProxySerializer.java    From onedev with MIT License 6 votes vote down vote up
@Override
public void serializeWithType(HibernateProxy value, JsonGenerator jgen,
		SerializerProvider provider, TypeSerializer typeSer)
		throws IOException, JsonProcessingException {
	Object proxiedValue = findProxied(value);
	if (proxiedValue == null) {
		provider.defaultSerializeNull(jgen);
		return;
	}
	/*
	 * This isn't exactly right, since type serializer really refers to
	 * proxy object, not value. And we really don't either know static type
	 * (necessary to know how to apply additional type info) or other
	 * things; so it's not going to work well. But... we'll do out best.
	 */
	findSerializer(provider, proxiedValue).serializeWithType(proxiedValue,
			jgen, provider, typeSer);
}
 
Example 6
Source File: POJONode.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException
{
    if (_value == null) {
        serializers.defaultSerializeNull(gen);
    } else if (_value instanceof JsonSerializable) {
        ((JsonSerializable) _value).serialize(gen, serializers);
    } else {
        gen.writeObject(_value);
    }
}
 
Example 7
Source File: TypelessCollectionSerializer.java    From spring-boot-data-geode with Apache License 2.0 5 votes vote down vote up
@Override
protected void serializeContents(Collection<?> value, JsonGenerator jsonGenerator,
		SerializerProvider serializerProvider) throws IOException {

	jsonGenerator.setCurrentValue(value);

	PropertySerializerMap serializers = this._dynamicSerializers;
	TypeSerializer typeSerializer = this._valueTypeSerializer;

	int index = -1;

	try {
		for (Object element : CollectionUtils.nullSafeCollection(value)) {

			index++;

			if (Objects.isNull(element)) {
				serializerProvider.defaultSerializeNull(jsonGenerator);
			}
			else {

				Class<?> elementType = element.getClass();

				JsonSerializer<Object> serializer = resolveSerializer(serializerProvider, elementType);

				if (typeSerializer != null) {
					serializer.serializeWithType(element, jsonGenerator, serializerProvider, typeSerializer);
				}
				else {
					serializer.serialize(element, jsonGenerator, serializerProvider);
				}
			}
		}
	}
	catch(Exception cause) {
		wrapAndThrow(serializerProvider, cause, value, index);
	}
}
 
Example 8
Source File: IdSerializer.java    From EDDI with Apache License 2.0 5 votes vote down vote up
public void serialize(String s, BsonGenerator bsonGenerator, SerializerProvider serializerProvider) throws
        IOException {
    if (s == null) {
        serializerProvider.defaultSerializeNull(bsonGenerator);
        return;
    }

    if (!isValid(s)) {
        throw new IllegalArgumentException("invalid hexadecimal representation of an ObjectId: [" + s + "]");
    }

    bsonGenerator.writeObjectId(createObjectIdFromString(s));
}
 
Example 9
Source File: InstantSerializerBase.java    From mongo-jackson-codec with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(T o, BsonGenerator bsonGenerator, SerializerProvider serializerProvider) throws
    IOException, JsonProcessingException {
    if (o == null) {
        serializerProvider.defaultSerializeNull(bsonGenerator);
    } else {
        bsonGenerator.writeDateTime(new Date(this.getEpochMillis.applyAsLong(o)));
    }
}
 
Example 10
Source File: IdSerializer.java    From mongo-jackson-codec with Apache License 2.0 5 votes vote down vote up
public void serialize(String s, BsonGenerator bsonGenerator, SerializerProvider serializerProvider) throws
    IOException, JsonProcessingException {
    if (s == null) {
        serializerProvider.defaultSerializeNull(bsonGenerator);
        return;
    }

    if (!isValid(s)) {
        throw new IllegalArgumentException("invalid hexadecimal representation of an ObjectId: [" + s + "]");
    }

    bsonGenerator.writeObjectId(createObjectIdFromString(s));
}
 
Example 11
Source File: WithConverterSerializer.java    From moserp with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(TYPE value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    if(value == null) {
        serializers.defaultSerializeNull(gen);
    } else {
        gen.writeString(converter.convert(value));
    }
}
 
Example 12
Source File: BeanAsArraySerializer.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public void serializeContents(Object value, JsonGenerator jgen, SerializerProvider provider)
    throws IOException
{
    JsonPropertyOrder order = value.getClass().getAnnotation(JsonPropertyOrder.class);
    String[] propOrder = (order == null) ? null : order.value();

    if (propOrder == null) {
        throw new IllegalStateException("Bean must declare JsonPropertyOrder!");
    }

    if (propOrder.length == 0) {
        return;
    }

    int i = 0;
    try {
        do {
            Field field = value.getClass().getDeclaredField(propOrder[i]);
            ReflectionUtils.makeAccessible(field);
            Object elem = field.get(value);
            if (elem == null) {
                provider.defaultSerializeNull(jgen);
            }
            else {
                Class<?> cc = elem.getClass();
                JsonSerializer<Object> serializer = provider.findValueSerializer(cc, null);
                serializer.serialize(elem, jgen, provider);
            }
            ++i;
        }
        while (i < propOrder.length);
    }
    catch (Exception e) {
        // [JACKSON-55] Need to add reference information
        wrapAndThrow(provider, e, value, i);
    }
}
 
Example 13
Source File: StoredAsJsonSerializer.java    From Rosetta with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(T value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
  ObjectMapper mapper = (ObjectMapper) jgen.getCodec();
  JsonNode tree = mapper.valueToTree(value);
  if (tree.isNull()) {
    provider.defaultSerializeNull(jgen);
  } else {
    jgen.writeString(mapper.writeValueAsString(tree));
  }
}
 
Example 14
Source File: HibernateProxySerializer.java    From onedev with MIT License 5 votes vote down vote up
@Override
public void serialize(HibernateProxy value, JsonGenerator jgen,
		SerializerProvider provider) throws IOException,
		JsonProcessingException {
	Object proxiedValue = findProxied(value);
	// TODO: figure out how to suppress nulls, if necessary? (too late for
	// that here)
	if (proxiedValue == null) {
		provider.defaultSerializeNull(jgen);
		return;
	}
	findSerializer(provider, proxiedValue).serialize(proxiedValue, jgen,
			provider);
}
 
Example 15
Source File: BsonInstantSerializer.java    From clouditor with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(Instant value, JsonGenerator gen, SerializerProvider serializers)
    throws IOException {
  if (value == null) {
    serializers.defaultSerializeNull(gen);
  } else if (gen instanceof BsonGenerator) {
    ((BsonGenerator) gen).writeDateTime(new Date(value.toEpochMilli()));
  } else {
    gen.writeNumber(value.toEpochMilli());
  }
}
 
Example 16
Source File: CollectionSerializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void serializeContents(Collection<?> value, JsonGenerator g, SerializerProvider provider) throws IOException
{
    g.setCurrentValue(value);
    if (_elementSerializer != null) {
        serializeContentsUsing(value, g, provider, _elementSerializer);
        return;
    }
    Iterator<?> it = value.iterator();
    if (!it.hasNext()) {
        return;
    }
    PropertySerializerMap serializers = _dynamicSerializers;
    final TypeSerializer typeSer = _valueTypeSerializer;

    int i = 0;
    try {
        do {
            Object elem = it.next();
            if (elem == null) {
                provider.defaultSerializeNull(g);
            } else {
                Class<?> cc = elem.getClass();
                JsonSerializer<Object> serializer = serializers.serializerFor(cc);
                if (serializer == null) {
                    if (_elementType.hasGenericTypes()) {
                        serializer = _findAndAddDynamic(serializers,
                                provider.constructSpecializedType(_elementType, cc), provider);
                    } else {
                        serializer = _findAndAddDynamic(serializers, cc, provider);
                    }
                    serializers = _dynamicSerializers;
                }
                if (typeSer == null) {
                    serializer.serialize(elem, g, provider);
                } else {
                    serializer.serializeWithType(elem, g, provider, typeSer);
                }
            }
            ++i;
        } while (it.hasNext());
    } catch (Exception e) {
        wrapAndThrow(provider, e, value, i);
    }
}
 
Example 17
Source File: NullNode.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final void serialize(JsonGenerator g, SerializerProvider provider)
    throws IOException
{
    provider.defaultSerializeNull(g);
}