Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.jaxrs.json.cfg;
  
  import java.util.*;
  
  
Helper class used to encapsulate details of configuring an com.fasterxml.jackson.databind.ObjectMapper instance to be used for data binding, as well as accessing it.
 
 public class MapperConfigurator
 {
    
Mapper provider was constructed with if any, or that was constructed due to a call to explicitly configure mapper. If defined (explicitly or implicitly) it will be used, instead of using provider-based lookup.
 
     protected ObjectMapper _mapper;

    
If no mapper was specified when constructed, and no configuration calls are made, a default mapper is constructed. The difference between default mapper and regular one is that default mapper is only used if no mapper is found via provider lookup.
 
     protected ObjectMapper _defaultMapper;

    
Annotations set to use by default; overridden by explicit call to setAnnotationsToUse(com.fasterxml.jackson.jaxrs.json.Annotations[])
 
     protected Annotations[] _defaultAnnotationsToUse;
    
    
To support optional dependency to Jackson JAXB annotations module (needed iff JAXB annotations are used for configuration)
 
     protected Class<? extends AnnotationIntrospector_jaxbIntrospectorClass;
     
     /*
     /**********************************************************
     /* Construction
     /**********************************************************
      */
     
     public MapperConfigurator(ObjectMapper mapperAnnotations[] defAnnotations)
     {
          = mapper;
          = defAnnotations;
     }

    
Method that locates, configures and returns com.fasterxml.jackson.databind.ObjectMapper to use
 
     public synchronized ObjectMapper getConfiguredMapper() {
         /* important: should NOT call mapper(); needs to return null
          * if no instance has been passed or constructed
          */
         return ;
     }
 
     public synchronized ObjectMapper getDefaultMapper() {
         if ( == null) {
              = new ObjectMapper();
         }
         return ;
     }
 
     /*
      ***********************************************************
      * Configuration methods
      ***********************************************************
       */
 
     public synchronized void setMapper(ObjectMapper m) {
          = m;
     }
 
     public synchronized void setAnnotationsToUse(Annotations[] annotationsToUse) {
         _setAnnotations(mapper(), annotationsToUse);
     }
 
     public synchronized void configure(DeserializationFeature fboolean state) {
         mapper().configure(fstate);
     }
 
     public synchronized void configure(SerializationFeature fboolean state) {
         mapper().configure(fstate);
     }
 
     public synchronized void configure(JsonParser.Feature fboolean state) {
        mapper().configure(fstate);
    }
    public synchronized void configure(JsonGenerator.Feature fboolean state) {
        mapper().configure(fstate);
    }
    /*
     ***********************************************************
     * Internal methods
     ***********************************************************
      */

    
Helper method that will ensure that there is a configurable non-default mapper (constructing an instance if one didn't yet exit), and return that mapper.
    protected ObjectMapper mapper()
    {
        if ( == null) {
             = new ObjectMapper();
        }
        return ;
    }
    protected void _setAnnotations(ObjectMapper mapperAnnotations[] annotationsToUse)
    {
        AnnotationIntrospector intr;
        if (annotationsToUse == null || annotationsToUse.length == 0) {
            intr = AnnotationIntrospector.nopInstance();
        } else {
            intr = _resolveIntrospectors(annotationsToUse);
        }
        mapper.setAnnotationIntrospector(intr);
    }
    protected AnnotationIntrospector _resolveIntrospectors(Annotations[] annotationsToUse)
    {
        // Let's ensure there are no dups there first, filter out nulls
        for (Annotations a : annotationsToUse) {
            if (a != null) {
                intr.add(_resolveIntrospector(a));
            }
        }
        int count = intr.size();
        if (count == 0) {
            return AnnotationIntrospector.nopInstance();
        }
        AnnotationIntrospector curr = intr.get(0);
        for (int i = 1, len = intr.size(); i < len; ++i) {
            curr = AnnotationIntrospector.pair(currintr.get(i));
        }
        return curr;
    }
    {
        switch (ann) {
        case :
            return new JacksonAnnotationIntrospector();
        case :
            /* For this, need to use indirection just so that error occurs
             * when we get here, and not when this class is being loaded
             */
            try {
                if ( == null) {
                     = JaxbAnnotationIntrospector.class;
                }
                return .newInstance();
            } catch (Exception e) {
                throw new IllegalStateException("Failed to instantiate JaxbAnnotationIntrospector: "+e.getMessage(), e);
            }
        default:
            throw new IllegalStateException(); 
        }
    }
New to GrepCode? Check out our FAQ X