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/std/StdJdkSerializers.java

    package com.fasterxml.jackson.databind.ser.std;
    
    import java.io.*;
    import java.lang.reflect.Type;
    import java.util.*;
    import java.util.concurrent.atomic.*;
    
    import com.fasterxml.jackson.core.*;
    import com.fasterxml.jackson.databind.*;
    import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
    import com.fasterxml.jackson.databind.ser.BasicSerializerFactory;
    
    /**
     * Class that providers access to serializers user for non-structured JDK types that
     * are serializer as scalars; some using basic {@link ToStringSerializer},
     * others explicit serializers.
     */
    @SuppressWarnings("serial")
    public class StdJdkSerializers
    {
        /**
         * Method called by {@link BasicSerializerFactory} to access
         * all serializers this class provides.
         */
        public static Collection<Map.Entry<Class<?>, Object>> all()
        {
            HashMap<Class<?>,Object> sers = new HashMap<Class<?>,Object>();
    
            // First things that 'toString()' can handle
            sers.put(java.net.URL.class, new ToStringSerializer(java.net.URL.class));
            sers.put(java.net.URI.class, new ToStringSerializer(java.net.URI.class));
    
            sers.put(Currency.class, new ToStringSerializer(Currency.class));
            sers.put(UUID.class, new UUIDSerializer());
            sers.put(java.util.regex.Pattern.class, new ToStringSerializer(java.util.regex.Pattern.class));
            sers.put(Locale.class, new ToStringSerializer(Locale.class));
    
            // then atomic types (note: AtomicReference defined elsewhere)
            sers.put(AtomicBoolean.class, AtomicBooleanSerializer.class);
            sers.put(AtomicInteger.class, AtomicIntegerSerializer.class);
            sers.put(AtomicLong.class, AtomicLongSerializer.class);
    
            // then other types that need specialized serializers
            sers.put(File.class, FileSerializer.class);
            sers.put(Class.class, ClassSerializer.class);
    
            // And then some stranger types... not 100% they are needed but:
            sers.put(Void.class, NullSerializer.instance);
            sers.put(Void.TYPE, NullSerializer.instance);
    
            return sers.entrySet();
        }
    
        /*
        /**********************************************************
        /* Serializers for atomic types
        /**********************************************************
         */
    
        public static class AtomicBooleanSerializer
            extends StdScalarSerializer<AtomicBoolean>
        {
            public AtomicBooleanSerializer() { super(AtomicBoolean.class, false); }
        
            @Override
            public void serialize(AtomicBoolean value, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonGenerationException {
                gen.writeBoolean(value.get());
            }
        
            @Override
            public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
                return createSchemaNode("boolean", true);
            }
            
            @Override
            public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException {
                visitor.expectBooleanFormat(typeHint);
            }
        }
        
        public static class AtomicIntegerSerializer
            extends StdScalarSerializer<AtomicInteger>
        {
            public AtomicIntegerSerializer() { super(AtomicInteger.class, false); }
        
            @Override
            public void serialize(AtomicInteger value, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonGenerationException {
                gen.writeNumber(value.get());
            }
        
            @Override
            public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
                return createSchemaNode("integer", true);
            }
            
            @Override
            public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException
            {
                visitIntFormat(visitor, typeHint, JsonParser.NumberType.INT);
            }
        }
    
        public static class AtomicLongSerializer
            extends StdScalarSerializer<AtomicLong>
        {
            public AtomicLongSerializer() { super(AtomicLong.class, false); }
        
            @Override
            public void serialize(AtomicLong value, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonGenerationException {
                gen.writeNumber(value.get());
            }
        
            @Override
            public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
                return createSchemaNode("integer", true);
            }
            
            @Override
            public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
                throws JsonMappingException
            {
                visitIntFormat(visitor, typeHint, JsonParser.NumberType.LONG);
            }
        }
    }
    

    com/fasterxml/jackson/databind/ser/std/StdJdkSerializers.java

     

    ⇒ Jackson Annotations Source Code

    ⇐ Download and Install Jackson Binary Package

    ⇑ Downloading and Reviewing jackson-*.jar

    ⇑⇑ Jackson - Java JSON library

    2022-03-29, 32006👍, 0💬