Java Code Examples for com.fasterxml.jackson.databind.JsonSerializer#serializeWithType()
The following examples show how to use
com.fasterxml.jackson.databind.JsonSerializer#serializeWithType() .
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 |
@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: CollectionSerializer.java From lams with GNU General Public License v2.0 | 6 votes |
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 3
Source File: TypelessCollectionSerializer.java From spring-boot-data-geode with Apache License 2.0 | 5 votes |
@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 4
Source File: CollectionSerializer.java From lams with GNU General Public License v2.0 | 4 votes |
@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 5
Source File: UnwrappingBeanPropertyWriter.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception { final Object value = get(bean); if (value == null) { // Hmmh. I assume we MUST pretty much suppress nulls, since we // can't really unwrap them... return; } JsonSerializer<Object> ser = _serializer; if (ser == null) { Class<?> cls = value.getClass(); PropertySerializerMap map = _dynamicSerializers; ser = map.serializerFor(cls); if (ser == null) { ser = _findAndAddDynamic(map, cls, prov); } } if (_suppressableValue != null) { if (MARKER_FOR_EMPTY == _suppressableValue) { if (ser.isEmpty(prov, value)) { return; } } else if (_suppressableValue.equals(value)) { return; } } // For non-nulls, first: simple check for direct cycles if (value == bean) { if (_handleSelfReference(bean, gen, prov, ser)) { return; } } // note: must verify we are using unwrapping serializer; if not, will write field name if (!ser.isUnwrappingSerializer()) { gen.writeFieldName(_name); } if (_typeSerializer == null) { ser.serialize(value, gen, prov); } else { ser.serializeWithType(value, gen, prov, _typeSerializer); } }
Example 6
Source File: ObjectMethodPropertyWriter.java From jackson-modules-base with Apache License 2.0 | 4 votes |
@Override public final void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception { if (broken) { fallbackWriter.serializeAsField(bean, gen, prov); return; } Object value; try { value = _propertyAccessor.objectGetter(bean, _propertyIndex); } catch (Throwable t) { _handleProblem(bean, gen, prov, t, false); return; } // Null (etc) handling; copied from super-class impl if (value == null) { if (_nullSerializer != null) { gen.writeFieldName(_fastName); _nullSerializer.serialize(null, gen, prov); } else if (!_suppressNulls) { gen.writeFieldName(_fastName); prov.defaultSerializeNullValue(gen); } return; } JsonSerializer<Object> ser = _serializer; if (ser == null) { Class<?> cls = value.getClass(); PropertySerializerMap map = _dynamicSerializers; ser = map.serializerFor(cls); if (ser == null) { ser = _findAndAddDynamic(map, cls, prov); } } if (_suppressableValue != null) { if (MARKER_FOR_EMPTY == _suppressableValue) { if (ser.isEmpty(prov, value)) { return; } } else if (_suppressableValue.equals(value)) { return; } } if (value == bean) { // three choices: exception; handled by call; or pass-through if (_handleSelfReference(bean, gen, prov, ser)) { return; } } gen.writeFieldName(_fastName); if (_typeSerializer == null) { ser.serialize(value, gen, prov); } else { ser.serializeWithType(value, gen, prov, _typeSerializer); } }
Example 7
Source File: ObjectMethodPropertyWriter.java From jackson-modules-base with Apache License 2.0 | 4 votes |
@Override public final void serializeAsElement(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception { if (broken) { fallbackWriter.serializeAsElement(bean, gen, prov); return; } Object value; try { value = _propertyAccessor.objectGetter(bean, _propertyIndex); } catch (Throwable t) { _handleProblem(bean, gen, prov, t, true); return; } // Null (etc) handling; copied from super-class impl if (value == null) { if (_nullSerializer != null) { _nullSerializer.serialize(null, gen, prov); } else if (_suppressNulls) { serializeAsPlaceholder(bean, gen, prov); } else { prov.defaultSerializeNullValue(gen); } return; } JsonSerializer<Object> ser = _serializer; if (ser == null) { Class<?> cls = value.getClass(); PropertySerializerMap map = _dynamicSerializers; ser = map.serializerFor(cls); if (ser == null) { ser = _findAndAddDynamic(map, cls, prov); } } if (_suppressableValue != null) { if (MARKER_FOR_EMPTY == _suppressableValue) { if (ser.isEmpty(prov, value)) { serializeAsPlaceholder(bean, gen, prov); return; } } else if (_suppressableValue.equals(value)) { serializeAsPlaceholder(bean, gen, prov); return; } } if (value == bean) { // three choices: exception; handled by call; or pass-through if (_handleSelfReference(bean, gen, prov, ser)) { return; } } if (_typeSerializer == null) { ser.serialize(value, gen, prov); } else { ser.serializeWithType(value, gen, prov, _typeSerializer); } }
Example 8
Source File: ObjectFieldPropertyWriter.java From jackson-modules-base with Apache License 2.0 | 4 votes |
@Override public final void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception { if (broken) { fallbackWriter.serializeAsField(bean, gen, prov); return; } Object value; try { value = _propertyAccessor.objectField(bean, _propertyIndex); } catch (Throwable t) { _handleProblem(bean, gen, prov, t, false); return; } // Null (etc) handling; copied from super-class impl if (value == null) { if (_nullSerializer != null) { gen.writeFieldName(_fastName); _nullSerializer.serialize(null, gen, prov); } else if (!_suppressNulls) { gen.writeFieldName(_fastName); prov.defaultSerializeNullValue(gen); } return; } JsonSerializer<Object> ser = _serializer; if (ser == null) { Class<?> cls = value.getClass(); PropertySerializerMap map = _dynamicSerializers; ser = map.serializerFor(cls); if (ser == null) { ser = _findAndAddDynamic(map, cls, prov); } } if (_suppressableValue != null) { if (MARKER_FOR_EMPTY == _suppressableValue) { if (ser.isEmpty(prov, value)) { return; } } else if (_suppressableValue.equals(value)) { return; } } if (value == bean) { // three choices: exception; handled by call; or pass-through if (_handleSelfReference(bean, gen, prov, ser)) { return; } } gen.writeFieldName(_fastName); if (_typeSerializer == null) { ser.serialize(value, gen, prov); } else { ser.serializeWithType(value, gen, prov, _typeSerializer); } }
Example 9
Source File: ObjectFieldPropertyWriter.java From jackson-modules-base with Apache License 2.0 | 4 votes |
@Override public final void serializeAsElement(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception { if (broken) { fallbackWriter.serializeAsElement(bean, gen, prov); return; } Object value; try { value = _propertyAccessor.objectField(bean, _propertyIndex); } catch (Throwable t) { _handleProblem(bean, gen, prov, t, true); return; } if (value == null) { if (_nullSerializer != null) { _nullSerializer.serialize(null, gen, prov); } else if (_suppressNulls) { serializeAsPlaceholder(bean, gen, prov); } else { prov.defaultSerializeNullValue(gen); } return; } JsonSerializer<Object> ser = _serializer; if (ser == null) { Class<?> cls = value.getClass(); PropertySerializerMap map = _dynamicSerializers; ser = map.serializerFor(cls); if (ser == null) { ser = _findAndAddDynamic(map, cls, prov); } } if (_suppressableValue != null) { if (MARKER_FOR_EMPTY == _suppressableValue) { if (ser.isEmpty(prov, value)) { serializeAsPlaceholder(bean, gen, prov); return; } } else if (_suppressableValue.equals(value)) { serializeAsPlaceholder(bean, gen, prov); return; } } if (value == bean) { // three choices: exception; handled by call; or pass-through if (_handleSelfReference(bean, gen, prov, ser)) { return; } } if (_typeSerializer == null) { ser.serialize(value, gen, prov); } else { ser.serializeWithType(value, gen, prov, _typeSerializer); } }
Example 10
Source File: DynamicFilters.java From proctor with Apache License 2.0 | 4 votes |
@Override public void serializeWithType(final JsonGenerator gen, final SerializerProvider serializers, final TypeSerializer typeSer) throws IOException { final JsonSerializer<Object> serializer = serializers.findValueSerializer(DynamicFilter[].class); serializer.serializeWithType(filters.toArray(), gen, serializers, typeSer); }