Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.std;
  
  
  
  
Generic handler for types that implement com.fasterxml.jackson.databind.JsonSerializable.

Note: given that this is used for anything that implements interface, can not be checked for direct class equivalence.

 
 public class SerializableSerializer
     extends StdSerializer<JsonSerializable>
 {
     public final static SerializableSerializer instance = new SerializableSerializer();
 
     // Ugh. Should NOT need this...
     private final static AtomicReference<ObjectMapper_mapperReference = new AtomicReference<ObjectMapper>();
     
     protected SerializableSerializer() { super(JsonSerializable.class); }
 
     @Override
     public void serialize(JsonSerializable valueJsonGenerator jgenSerializerProvider provider)
         throws IOExceptionJsonGenerationException
     {
         value.serialize(jgenprovider);
     }
 
     @Override
     public final void serializeWithType(JsonSerializable valueJsonGenerator jgenSerializerProvider provider,
             TypeSerializer typeSer)
         throws IOExceptionJsonGenerationException
     {
         value.serializeWithType(jgenprovidertypeSer);
     }
     
     @Override
     @SuppressWarnings("deprecation")
     public JsonNode getSchema(SerializerProvider providerType typeHint)
         throws JsonMappingException
     {
         ObjectNode objectNode = createObjectNode();
         String schemaType = "any";
         String objectProperties = null;
         String itemDefinition = null;
         if (typeHint != null) {
             Class<?> rawClass = TypeFactory.rawClass(typeHint);
             if (rawClass.isAnnotationPresent(JsonSerializableSchema.class)) {
                 JsonSerializableSchema schemaInfo = rawClass.getAnnotation(JsonSerializableSchema.class);
                 schemaType = schemaInfo.schemaType();
                 if (!..equals(schemaInfo.schemaObjectPropertiesDefinition())) {
                     objectProperties = schemaInfo.schemaObjectPropertiesDefinition();
                 }
                 if (!..equals(schemaInfo.schemaItemDefinition())) {
                     itemDefinition = schemaInfo.schemaItemDefinition();
                 }
             }
         }
         /* 19-Mar-2012, tatu: geez, this is butt-ugly abonimation of code...
          *    really, really should not require back ref to an ObjectMapper.
          */
         objectNode.put("type"schemaType);
         if (objectProperties != null) {
             try {
                 objectNode.put("properties"_getObjectMapper().readTree(objectProperties));
             } catch (IOException e) {
                 throw new JsonMappingException("Failed to parse @JsonSerializableSchema.schemaObjectPropertiesDefinition value");
             }
         }
         if (itemDefinition != null) {
             try {
                 objectNode.put("items"_getObjectMapper().readTree(itemDefinition));
             } catch (IOException e) {
                 throw new JsonMappingException("Failed to parse @JsonSerializableSchema.schemaItemDefinition value");
             }
         }
         // always optional, no need to specify:
         //objectNode.put("required", false);
         return objectNode;
     }
     
    private final static synchronized ObjectMapper _getObjectMapper()
    {
        ObjectMapper mapper = .get();
        if (mapper == null) {
            mapper = new ObjectMapper();
            .set(mapper);
        }
        return mapper;
    }
    @Override
    public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
        throws JsonMappingException
    {
        visitor.expectAnyFormat(typeHint);
    }
New to GrepCode? Check out our FAQ X