Java Code Examples for com.fasterxml.jackson.core.JsonGenerator#setCurrentValue()

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#setCurrentValue() . 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: BeanAsArraySerializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void serializeWithType(Object bean, JsonGenerator gen,
        SerializerProvider provider, TypeSerializer typeSer)
    throws IOException
{
    /* 10-Dec-2014, tatu: Not sure if this can be made to work reliably;
     *   but for sure delegating to default implementation will not work. So:
     */
    if (_objectIdWriter != null) {
        _serializeWithObjectId(bean, gen, provider, typeSer);
        return;
    }
    gen.setCurrentValue(bean);
    WritableTypeId typeIdDef = _typeIdDef(typeSer, bean, JsonToken.START_ARRAY);
    typeSer.writeTypePrefix(gen, typeIdDef);
    serializeAsArray(bean, gen, provider);
    typeSer.writeTypeSuffix(gen, typeIdDef);
}
 
Example 2
Source File: BeanAsArraySerializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Main serialization method that will delegate actual output to
 * configured
 * {@link BeanPropertyWriter} instances.
 */
@Override
public final void serialize(Object bean, JsonGenerator gen, SerializerProvider provider)
    throws IOException
{
    if (provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
            && hasSingleElement(provider)) {
        serializeAsArray(bean, gen, provider);
        return;
    }
    /* note: it is assumed here that limitations (type id, object id,
     * any getter, filtering) have already been checked; so code here
     * is trivial.
     */
    gen.writeStartArray();
    // [databind#631]: Assign current value, to be accessible by custom serializers
    gen.setCurrentValue(bean);
    serializeAsArray(bean, gen, provider);
    gen.writeEndArray();
}
 
Example 3
Source File: UnwrappingBeanSerializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void serializeWithType(Object bean, JsonGenerator gen, SerializerProvider provider,
		TypeSerializer typeSer) throws IOException
{
    if (provider.isEnabled(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS)) {
        provider.reportBadDefinition(handledType(),
                "Unwrapped property requires use of type information: cannot serialize without disabling `SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS`");
    }
    gen.setCurrentValue(bean); // [databind#631]
    if (_objectIdWriter != null) {
        _serializeWithObjectId(bean, gen, provider, typeSer);
        return;
    }
    if (_propertyFilterId != null) {
        serializeFieldsFiltered(bean, gen, provider);
    } else {
        serializeFields(bean, gen, provider);
    }
}
 
Example 4
Source File: BeanSerializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Main serialization method that will delegate actual output to
 * configured
 * {@link BeanPropertyWriter} instances.
 */
@Override
public final void serialize(Object bean, JsonGenerator gen, SerializerProvider provider)
    throws IOException
{
    if (_objectIdWriter != null) {
        gen.setCurrentValue(bean); // [databind#631]
        _serializeWithObjectId(bean, gen, provider, true);
        return;
    }
    gen.writeStartObject(bean);
    if (_propertyFilterId != null) {
        serializeFieldsFiltered(bean, gen, provider);
    } else {
        serializeFields(bean, gen, provider);
    }
    gen.writeEndObject();
}
 
Example 5
Source File: CharIterableSerializer.java    From jackson-datatypes-collections with Apache License 2.0 6 votes vote down vote up
@Override
public void serializeWithType(
        CharIterable value,
        JsonGenerator g,
        SerializerProvider ctxt,
        TypeSerializer typeSer
) throws IOException {
    g.setCurrentValue(value);
    WritableTypeId typeIdDef;
    if (ctxt.isEnabled(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS)) {
        typeIdDef = typeSer.writeTypePrefix(g, ctxt, typeSer.typeId(value, JsonToken.START_ARRAY));
        writeContentsAsArray(value, g);
    } else {
        typeIdDef = typeSer.writeTypePrefix(g, ctxt, typeSer.typeId(value, JsonToken.VALUE_STRING));
        char[] chars = value.toArray();
        g.writeString(chars, 0, chars.length);
    }
    typeSer.writeTypeSuffix(g, ctxt, typeIdDef);
}
 
Example 6
Source File: MapEntrySerializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void serializeWithType(Map.Entry<?, ?> value, JsonGenerator g,
        SerializerProvider provider, TypeSerializer typeSer) throws IOException
{
    // [databind#631]: Assign current value, to be accessible by custom serializers
    g.setCurrentValue(value);
    WritableTypeId typeIdDef = typeSer.writeTypePrefix(g,
            typeSer.typeId(value, JsonToken.START_OBJECT));
    serializeDynamic(value, g, provider);
    typeSer.writeTypeSuffix(g, typeIdDef);
}
 
Example 7
Source File: UnwrappingBeanSerializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Main serialization method that will delegate actual output to
 * configured
 * {@link BeanPropertyWriter} instances.
 */
@Override
public final void serialize(Object bean, JsonGenerator gen, SerializerProvider provider) throws IOException
{
    gen.setCurrentValue(bean); // [databind#631]
    if (_objectIdWriter != null) {
        _serializeWithObjectId(bean, gen, provider, false);
        return;
    }
    if (_propertyFilterId != null) {
        serializeFieldsFiltered(bean, gen, provider);
    } else {
        serializeFields(bean, gen, provider);
    }
}
 
Example 8
Source File: CustomSimpleSerializers.java    From caravan with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public JsonSerializer<?> findArraySerializer(SerializationConfig config, ArrayType type, BeanDescription beanDesc, TypeSerializer elementTypeSerializer,
    JsonSerializer<Object> elementValueSerializer) {

  Class<?> contentClass = type.getContentType().getRawClass();
  TypeCustomizationFactory factory = provider.factoryForArrayOf(contentClass);

  if (factory != null) {
    final JsonSerializer ser = factory.createSerializer();
    return new JsonSerializer() {

      @SuppressWarnings("unchecked")
      @Override
      public void serialize(Object o, JsonGenerator gen, SerializerProvider serializerProvider)
          throws IOException {

        gen.writeStartArray();
        gen.setCurrentValue(o);

        int length = Array.getLength(o);
        for (int i = 0; i < length; i++) {
          Object arrayElement = Array.get(o, i);
          ser.serialize(arrayElement, gen, serializerProvider);
        }

        gen.writeEndArray();
      }
    };
  }

  return super.findArraySerializer(config, type, beanDesc, elementTypeSerializer, elementValueSerializer);
}
 
Example 9
Source File: JkesJsonSerializer.java    From jkes with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOException {

    JsonStreamContext context = gen.getOutputContext();
    gen.setCurrentValue(value); // must set manually, or else the result of context.getCurrentValue() will be null。this method will call context.setCurrentValue(value);

    gen.writeStartObject(); // writeStartObject(value) will call setCurrentValue(value) well
    Class<?> entityClass = value.getClass();
    Method[] methods = entityClass.getMethods(); // include method inherited from super classes
    for(Method method : methods) {
        // check whether serialization will be recursive
        if (willRecursive(gen, context, method)) {
            gen.writeEndObject();
            return;
        }

        // method annotated with @Field
        Field fieldAnnotation = method.getAnnotation(Field.class);
        if(fieldAnnotation != null) {
            serializeField(value, gen, entityClass, method, fieldAnnotation);
        }else {
            MultiFields multiFields = method.getAnnotation(MultiFields.class);
            if(multiFields != null) {
                Field mainFieldAnnotation = multiFields.mainField();
                serializeField(value, gen, entityClass, method, mainFieldAnnotation);
            }
        }
    }

    // gen.writeObjectField("extra_field", "whatever_value");
    gen.writeEndObject();

}
 
Example 10
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 11
Source File: RefRefMapSerializer.java    From jackson-datatypes-collections with Apache License 2.0 5 votes vote down vote up
@Override
public void serializeWithType(
        T value, JsonGenerator gen,
        SerializerProvider ctxt, TypeSerializer typeSer
) throws IOException {
    gen.setCurrentValue(value);
    WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen, ctxt,
            typeSer.typeId(value, JsonToken.START_OBJECT));
    if (!isEmpty(ctxt, value)) {
        serializeFields(value, gen, ctxt);
    }
    typeSer.writeTypeSuffix(gen, ctxt, typeIdDef);
}
 
Example 12
Source File: PrimitiveIterableSerializer.java    From jackson-datatypes-collections with Apache License 2.0 5 votes vote down vote up
@Override
public void serializeWithType(C value, JsonGenerator g, SerializerProvider ctxt, TypeSerializer typeSer)
        throws IOException {
    g.setCurrentValue(value);
    WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt, typeSer.typeId(value, JsonToken.START_ARRAY));
    serializeContents(value, g);
    typeSer.writeTypeSuffix(g, ctxt, typeIdDef);
}