public static void writeWithType(final String key, final Object object, final JsonGenerator jsonGenerator,
                                     final SerializerProvider serializerProvider,
                                     final TypeSerializer typeSerializer) throws IOException {
        final JsonSerializer<Object> serializer = serializerProvider.findValueSerializer(object.getClass(), null);
        if (typeSerializer != null) {
            // serialize with types embedded
            if (key != null && !key.isEmpty()) jsonGenerator.writeFieldName(key);
            serializer.serializeWithType(object, jsonGenerator, serializerProvider, typeSerializer);
        } else {
            // types are not embedded, but use the serializer when possible or else custom serializers will get
            // bypassed and you end up with the default jackson serializer when you don't want it.
            if (key != null && !key.isEmpty()) jsonGenerator.writeFieldName(key);
            serializer.serialize(object, jsonGenerator, serializerProvider);
        }
    }
Utility methods for GraphSON serialization. Functions in here might be used by external serializers.

@author Stephen Mallette (http://stephen.genoprime.com)
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONUtil.java