/** * Trace an error with current writer state and returns a corresponding exception. * * @param value current value * @param message error message * @param writer current writer * * @return a {@link JsonSerializationException} with the given message */ public JsonSerializationException traceError( Object value, String message, JsonWriter writer ) { JsonSerializationException exception = traceError( value, message ); traceWriterInfo( value, writer ); return exception; }
/** * Trace an error with current writer state and returns a corresponding exception. * * @param value current value * @param cause cause of the error * @param writer current writer * * @return a {@link JsonSerializationException} if we wrap the exceptions, the cause otherwise */ public RuntimeException traceError( Object value, RuntimeException cause, JsonWriter writer ) { RuntimeException exception = traceError( value, cause ); traceWriterInfo( value, writer ); return exception; }
@Override public void serializeInternally( JsonWriter writer, T value, JsonSerializationContext ctx, JsonSerializerParameters params, IdentitySerializationInfo<T> defaultIdentityInfo, TypeSerializationInfo<T> defaultTypeInfo ) { final TypeSerializationInfo typeInfo = null == params.getTypeInfo() ? defaultTypeInfo : params.getTypeInfo(); if ( null != typeInfo ) { String typeInformation = typeInfo.getTypeInfo( value.getClass() ); if ( null == typeInformation ) { throw ctx.traceError( value, "Cannot find type info for class " + value.getClass(), writer ); } switch ( typeInfo.getInclude() ) { case WRAPPER_OBJECT: // type info is included in a wrapper object that contains only one property. The name of this property is the type // info and the value the object writer.beginObject(); writer.name( typeInformation ); getSerializer().serialize( writer, value, ctx, params ); writer.endObject(); break; default: // included as wrapper array even if property is set writer.beginArray(); writer.value( typeInformation ); getSerializer().serialize( writer, value, ctx, params ); writer.endArray(); } } else { getSerializer().serialize( writer, value, ctx, params ); } } }
/** {@inheritDoc} */ @Override public String write( T value, JsonSerializationContext ctx ) throws JsonSerializationException { JsonWriter writer = ctx.newJsonWriter(); try { if ( ctx.isWrapRootValue() ) { writer.beginObject(); writer.name( rootName ); getSerializer().serialize( writer, value, ctx ); writer.endObject(); } else { getSerializer().serialize( writer, value, ctx ); } return writer.getOutput(); } catch ( JsonSerializationException e ) { // already logged, we just throw it throw e; } catch ( RuntimeException e ) { throw ctx.traceError( value, e, writer ); } }