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/util/NameTransformer.java

    package com.fasterxml.jackson.databind.util;
    
    /**
     * Helper class used to encapsulate details of name mangling, transforming
     * of names using different strategies (prefixes, suffixes).
     * Default implementation is "no-operation" (aka identity transformation).
     */
    public abstract class NameTransformer
    {
        /**
         * Singleton "no-operation" transformer which simply returns given
         * name as is. Used commonly as placeholder or marker.
         */
        public final static NameTransformer NOP = new NopTransformer();
        
        protected final static class NopTransformer
            extends NameTransformer
            implements java.io.Serializable
        {
            private static final long serialVersionUID = 1L;
    
            @Override
            public String transform(String name) {
                return name;
            }
            @Override
            public String reverse(String transformed) {
                // identity transformation is always reversible:
                return transformed;
            }
        }
    
        protected NameTransformer() { }
        
        /**
         * Factory method for constructing a simple transformer based on
         * prefix and/or suffix.
         */
        public static NameTransformer simpleTransformer(final String prefix, final String suffix)
        {
            boolean hasPrefix = (prefix != null) && !prefix.isEmpty();
            boolean hasSuffix = (suffix != null) && !suffix.isEmpty();
    
            if (hasPrefix) {
                if (hasSuffix) {
                    return new NameTransformer() {
                        @Override
                        public String transform(String name) { return prefix + name + suffix; }
                        @Override
                        public String reverse(String transformed) {
                            if (transformed.startsWith(prefix)) {
                                String str = transformed.substring(prefix.length());
                                if (str.endsWith(suffix)) {
                                    return str.substring(0, str.length() - suffix.length());
                                }
                            }
                            return null;
                        }
                        @Override
                        public String toString() { return "[PreAndSuffixTransformer('"+prefix+"','"+suffix+"')]"; }
                    };
                }
                return new NameTransformer() {
                    @Override
                    public String transform(String name) { return prefix + name; }
                    @Override
                    public String reverse(String transformed) {
                        if (transformed.startsWith(prefix)) {
                            return transformed.substring(prefix.length());
                        }
                        return null;
                    }
                    @Override
                    public String toString() { return "[PrefixTransformer('"+prefix+"')]"; }
                };
            }
            if (hasSuffix) {
                return new NameTransformer() {
                    @Override
                    public String transform(String name) { return name + suffix; }
                    @Override
                    public String reverse(String transformed) {
                        if (transformed.endsWith(suffix)) {
                            return transformed.substring(0, transformed.length() - suffix.length());
                        }
                        return null;
                    }
                    @Override
                    public String toString() { return "[SuffixTransformer('"+suffix+"')]"; }
                };
            }
            return NOP;
        }
    
        /**
         * Method that constructs transformer that applies given transformers
         * as a sequence; essentially combines separate transform operations
         * into one logical transformation.
         */
        public static NameTransformer chainedTransformer(NameTransformer t1, NameTransformer t2) {
            return new Chained(t1, t2);
        }
        
        /**
         * Method called when (forward) transformation is needed.
         */
        public abstract String transform(String name);
    
        /**
         * Method called when reversal of transformation is needed; should return
         * null if this is not possible, that is, given name cannot have been
         * result of calling {@link #transform} of this object.
         */
        public abstract String reverse(String transformed);
    
        public static class Chained extends NameTransformer
            implements java.io.Serializable
        {
            private static final long serialVersionUID = 1L;
    
            protected final NameTransformer _t1, _t2;
            
            public Chained(NameTransformer t1, NameTransformer t2) {
                _t1 = t1;
                _t2 = t2;
            }
    
            @Override
            public String transform(String name) {
                return _t1.transform(_t2.transform(name));
            }
    
            @Override
            public String reverse(String transformed) {
                transformed = _t1.reverse(transformed);
                if (transformed != null) {
                    transformed = _t2.reverse(transformed);
                }
                return transformed;
            }
    
            @Override
            public String toString() { return "[ChainedTransformer("+_t1+", "+_t2+")]"; }
        }
    }
    

    com/fasterxml/jackson/databind/util/NameTransformer.java

     

    ⇒ Jackson Annotations Source Code

    ⇐ Download and Install Jackson Binary Package

    ⇑ Downloading and Reviewing jackson-*.jar

    ⇑⇑ Jackson - Java JSON library

    2022-03-29, 32263👍, 0💬