Jackson Data Binding Source Code

Jackson is "the Java JSON library" or "the best JSON parser for Java". Or simply as "JSON for Java".

  • Jackson Data Binding module allows you to converts JSON to and from POJO (Plain Old Java Object) using property accessor or using annotations.
  • Jackson Databind Source Code files are provided in the source packge (jackson-databind-2.12.4-sources.jar). You can download it at Jackson Maven Website.

    You can also browse Jackson Databind Source Code below:

    ✍: FYIcenter.com

    com/fasterxml/jackson/databind/ser/impl/UnknownSerializer.java

    package com.fasterxml.jackson.databind.ser.impl;
    
    import java.io.IOException;
    import java.lang.reflect.Type;
    
    import com.fasterxml.jackson.core.*;
    import com.fasterxml.jackson.core.type.WritableTypeId;
    import com.fasterxml.jackson.databind.*;
    import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
    import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
    import com.fasterxml.jackson.databind.ser.std.StdSerializer;
    
    @SuppressWarnings("serial")
    public class UnknownSerializer
        extends StdSerializer<Object>
    {
        public UnknownSerializer() {
            super(Object.class);
        }
    
        /**
         * @since 2.6
         */
        public UnknownSerializer(Class<?> cls) {
            super(cls, false);
        }
        
        @Override
        public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException
        {
            // 27-Nov-2009, tatu: As per [JACKSON-201] may or may not fail...
            if (provider.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)) {
                failForEmpty(provider, value);
            }
            // But if it's fine, we'll just output empty JSON Object:
            gen.writeStartObject(value, 0);
            gen.writeEndObject();
        }
    
        @Override
        public final void serializeWithType(Object value, JsonGenerator gen, SerializerProvider provider,
                TypeSerializer typeSer) throws IOException
        {
            if (provider.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)) {
                failForEmpty(provider, value);
            }
            WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen,
                    typeSer.typeId(value, JsonToken.START_OBJECT));
            typeSer.writeTypeSuffix(gen, typeIdDef);
        }
    
        @Override
        public boolean isEmpty(SerializerProvider provider, Object value) {
            return true;
        }
    
        @Override
        public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException {
            return null;
        }
        
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
            throws JsonMappingException
        { 
            visitor.expectAnyFormat(typeHint);
        }
    
        protected void failForEmpty(SerializerProvider prov, Object value)
                throws JsonMappingException {
            prov.reportBadDefinition(handledType(), String.format(
                    "No serializer found for class %s and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)",
                    value.getClass().getName()));
        }
    }
    

    com/fasterxml/jackson/databind/ser/impl/UnknownSerializer.java

     

    ⇒ Jackson Annotations Source Code

    ⇐ Download and Install Jackson Binary Package

    ⇑ Downloading and Reviewing jackson-*.jar

    ⇑⇑ Jackson - Java JSON library

    2022-03-29, 31965👍, 0💬