Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.jaxrs.json.annotation;
  
  
  
Container class for figuring out annotation-based configuration for JAX-RS end points.
 
 public class EndpointConfig
 {
     // // General configuration
     
     protected Class<?> _activeView;
 
     protected String _rootName;
 
     // // Deserialization-only config
     
     protected DeserializationFeature[] _deserEnable;
     protected DeserializationFeature[] _deserDisable;
 
     protected ObjectReader _reader;
     
     // // Serialization-only config
 
     protected JSONP.Def _jsonp;
     
     protected SerializationFeature[] _serEnable;
     protected SerializationFeature[] _serDisable;
 
     protected ObjectWriter _writer;
     
     /*
     /**********************************************************
     /* Construction
     /**********************************************************
      */
 
     protected EndpointConfig() { }
 
     public static EndpointConfig forReading(ObjectMapper mapperAnnotation[] annotations)
     {
         return new EndpointConfig()
             .add(annotationsfalse)
             .initReader(mapper);
     }
 
     public static EndpointConfig forWriting(ObjectMapper mapperAnnotation[] annotations,
             String defaultJsonpMethod)
     {
         EndpointConfig config =  new EndpointConfig();
         if (defaultJsonpMethod != null) {
             config._jsonp = new JSONP.Def(defaultJsonpMethod);
         }
         return config
             .add(annotationstrue)
             .initWriter(mapper)
         ;
     }
     
     protected EndpointConfig add(Annotation[] annotationsboolean forWriting)
     {
     	if (annotations != null) {
           for (Annotation annotation : annotations) {
             Class<?> type = annotation.annotationType();
             if (type == JSONP.class) {
                 if (forWriting) {
                      = new JSONP.Def((JSONPannotation);
                 }
             } else if (type == JsonView.class) {
                 // Can only use one view; but if multiple defined, use first (no exception)
                 Class<?>[] views = ((JsonViewannotation).value();
                  = (views.length > 0) ? views[0] : null;
             } else if (type == JsonRootName.class) {
                  = ((JsonRootNameannotation).value();
             } else if (type == JacksonFeatures.class) {
                 JacksonFeatures feats = (JacksonFeaturesannotation;
                 if (forWriting) {
                      = nullIfEmpty(feats.serializationEnable());
                      = nullIfEmpty(feats.serializationDisable());
                 } else {
                      = nullIfEmpty(feats.deserializationEnable());
                      = nullIfEmpty(feats.deserializationDisable());
                 }
             } else if (type == JacksonAnnotationsInside.class) {
                 // skip; processed below (in parent), so encountering here is of no use
             } else {
                 // For all unrecognized types, check meta-annotation(s) to see if they are bundles
                 JacksonAnnotationsInside inside = type.getAnnotation(JacksonAnnotationsInside.class);
                 if (inside != null) {
                     add(type.getAnnotations(), forWriting);
                }
            }
          }
    	}
        return this;
    }
    protected EndpointConfig initReader(ObjectMapper mapper)
    {
        // first common config
        if ( != null) {
             = mapper.readerWithView();
        } else {
             = mapper.reader();
        }
        if ( != null) {
             = .withRootName();
        }
        // Then deser features
        if ( != null) {
             = .withFeatures();
        }
        if ( != null) {
             = .withoutFeatures();
        }
        /* Important: we are NOT to close the underlying stream after
         * mapping, so we need to instruct parser:
         */
        
        return this;
    }
    
    protected EndpointConfig initWriter(ObjectMapper mapper)
    {
        // first common config
        if ( != null) {
             = mapper.writerWithView();
        } else {
             = mapper.writer();
        }
        if ( != null) {
             = .withRootName();
        }
        // Then features
        if ( != null) {
             = .withFeatures();
        }
        if ( != null) {
             = .withoutFeatures();
        }
        // then others
        // Finally: couple of features we always set
        /* Important: we are NOT to close the underlying stream after
         * mapping, so we need to instruct parser:
         */
        
        return this;
    }
    /*
    /**********************************************************
    /* Accessors
    /**********************************************************
     */
    public ObjectReader getReader() {
        if ( == null) { // sanity check, should never happen
            throw new IllegalStateException();
        }
        return ;
    }
    public ObjectWriter getWriter() {
        if ( == null) { // sanity check, should never happen
            throw new IllegalStateException();
        }
        return ;
    }

    
Method that will add JSONP wrapper object, if and as configured by collected annotations.
    public Object applyJSONP(Object value)
    {
        if ( != null) {
            // full prefix+suffix?
            if (. != null || . != null) {
                return new JSONWrappedObject(..value);
            }
            if (. != null) {
                return new JSONPObject(.value);
            }
        }
        return value;
    }
    
    /*
    /**********************************************************
    /* Helper methods
    /**********************************************************
     */
    private static <T> T[] nullIfEmpty(T[] arg) {
        if (arg == null || arg.length == 0) {
            return null;
        }
        return arg;
    }
New to GrepCode? Check out our FAQ X