use of com.fasterxml.jackson.databind.type.TypeFactory in project platformlayer by platformlayer.
the class PlatformLayerServletModule method configureServlets.
@Override
protected void configureServlets() {
// switch (ApplicationMode.getMode()) {
// case DEVELOPMENT:
// bind(OpenstackAuthenticationFilterBase.class).to(DevelopmentOpenstackAuthenticationFilter.class).in(Scopes.SINGLETON);
// break;
//
// default:
// throw new IllegalStateException("Unhandled application mode: " + ApplicationMode.getMode());
// }
bind(MetricDataSourceWriter.class);
bind(CORSFilter.class).asEagerSingleton();
filter("/*").through(CORSFilter.class);
bind(ScopeFilter.class).asEagerSingleton();
filter("/*").through(ScopeFilter.class);
extensions.addHttpExtensions(new HttpConfiguration() {
@Override
public FilterKeyBindingBuilder filter(String urlPattern) {
return PlatformLayerServletModule.this.filter(urlPattern);
}
@Override
public ServletKeyBindingBuilder serve(String urlPattern) {
return PlatformLayerServletModule.this.serve(urlPattern);
}
@Override
public <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz) {
return PlatformLayerServletModule.this.bind(clazz);
}
});
bind(ProjectAuthorization.class).toProvider(ScopeProjectAuthorizationProvider.class);
bind(Scope.class).toProvider(ScopeProvider.class);
// if (ApplicationMode.isDevelopment()) {
// bind(AuthenticationTokenValidator.class).to(DevelopmentTokenValidator.class);
// } else {
// bind(AuthenticationTokenValidator.class).to(PlatformLayerTokenValidator.class);
// }
bind(OpsAuthenticationFilter.class).asEagerSingleton();
// /* bind the REST resources */
// bind(ManagedItemCollectionResource.class);
// bind(ManagedItemResource.class);
bind(RootResource.class);
// bind(ServiceAuthorizationCollectionResource.class);
// bind(ServiceAuthorizationResource.class);
// bind(ServiceResource.class);
// bind(ServicesCollectionResource.class);
filter("/v0/*").through(OpsAuthenticationFilter.class);
Map<String, String> params = Maps.newHashMap();
List<String> packages = Lists.newArrayList();
packages.add("org.platformlayer.xaas.web.jaxrs");
packages.add("org.platformlayer.xaas.web.resources");
// packages.add("org.codehaus.jackson.jaxrs");
TypeFactory typeFactory = TypeFactory.defaultInstance();
ObjectMapper objectMapper = JsonHelper.buildObjectMapper(typeFactory, false);
// TypeResolverBuilder<?> typer = new PlatformLayerTypeResolverBuilder();
// TypeIdResolver typeIdResolver = new PlatformLayerTypeIdResolver(null, typeFactory);
// this.requestInjection(typeIdResolver);
// typer = typer.init(JsonTypeInfo.Id.CLASS, typeIdResolver);
// typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
// typer = typer.typeProperty("type");
// objectMapper.setDefaultTyping(typer);
bind(ObjectMapper.class).toInstance(objectMapper);
bind(ObjectMapperProvider.class).in(Scopes.SINGLETON);
bind(PlatformLayerJsonProvider.class).in(Scopes.SINGLETON);
params.put(PackagesResourceConfig.PROPERTY_PACKAGES, Joiner.on(';').join(packages));
serve("/v0/*").with(GuiceContainer.class, params);
// ImmutableMap.of(JSONConfiguration.FEATURE_POJO_MAPPING, "true"));
}
use of com.fasterxml.jackson.databind.type.TypeFactory in project druid by druid-io.
the class AggregationTestHelper method makeStringSerdeQueryRunner.
public QueryRunner<Row> makeStringSerdeQueryRunner(final ObjectMapper mapper, final QueryToolChest toolChest, final Query<Row> query, final QueryRunner<Row> baseRunner) {
return new QueryRunner<Row>() {
@Override
public Sequence<Row> run(Query<Row> query, Map<String, Object> map) {
try {
Sequence<Row> resultSeq = baseRunner.run(query, Maps.<String, Object>newHashMap());
final Yielder yielder = resultSeq.toYielder(null, new YieldingAccumulator() {
@Override
public Object accumulate(Object accumulated, Object in) {
yield();
return in;
}
});
String resultStr = mapper.writer().writeValueAsString(yielder);
TypeFactory typeFactory = mapper.getTypeFactory();
JavaType baseType = typeFactory.constructType(toolChest.getResultTypeReference());
List resultRows = Lists.transform(readQueryResultArrayFromString(resultStr), toolChest.makePreComputeManipulatorFn(query, MetricManipulatorFns.deserializing()));
return Sequences.simple(resultRows);
} catch (Exception ex) {
throw Throwables.propagate(ex);
}
}
};
}
use of com.fasterxml.jackson.databind.type.TypeFactory in project jackson-databind by FasterXML.
the class UntypedObjectDeserializer method resolve.
/*
/**********************************************************
/* Initialization
/**********************************************************
*/
/**
* We need to implement this method to properly find things to delegate
* to: it can not be done earlier since delegated deserializers almost
* certainly require access to this instance (at least "List" and "Map" ones)
*/
@SuppressWarnings("unchecked")
@Override
public void resolve(DeserializationContext ctxt) throws JsonMappingException {
JavaType obType = ctxt.constructType(Object.class);
JavaType stringType = ctxt.constructType(String.class);
TypeFactory tf = ctxt.getTypeFactory();
// So: first find possible custom instances
if (_listType == null) {
_listDeserializer = _clearIfStdImpl(_findCustomDeser(ctxt, tf.constructCollectionType(List.class, obType)));
} else {
// NOTE: if non-default List type, always consider to be non-standard deser
_listDeserializer = _findCustomDeser(ctxt, _listType);
}
if (_mapType == null) {
_mapDeserializer = _clearIfStdImpl(_findCustomDeser(ctxt, tf.constructMapType(Map.class, stringType, obType)));
} else {
// NOTE: if non-default Map type, always consider to be non-standard deser
_mapDeserializer = _findCustomDeser(ctxt, _mapType);
}
_stringDeserializer = _clearIfStdImpl(_findCustomDeser(ctxt, stringType));
_numberDeserializer = _clearIfStdImpl(_findCustomDeser(ctxt, tf.constructType(Number.class)));
// and then do bogus contextualization, in case custom ones need to resolve dependencies of
// their own
JavaType unknown = TypeFactory.unknownType();
_mapDeserializer = (JsonDeserializer<Object>) ctxt.handleSecondaryContextualization(_mapDeserializer, null, unknown);
_listDeserializer = (JsonDeserializer<Object>) ctxt.handleSecondaryContextualization(_listDeserializer, null, unknown);
_stringDeserializer = (JsonDeserializer<Object>) ctxt.handleSecondaryContextualization(_stringDeserializer, null, unknown);
_numberDeserializer = (JsonDeserializer<Object>) ctxt.handleSecondaryContextualization(_numberDeserializer, null, unknown);
}
use of com.fasterxml.jackson.databind.type.TypeFactory in project jackson-databind by FasterXML.
the class JacksonAnnotationIntrospector method refineSerializationType.
/*
/**********************************************************
/* Serialization: type refinements
/**********************************************************
*/
@Override
public JavaType refineSerializationType(final MapperConfig<?> config, final Annotated a, final JavaType baseType) throws JsonMappingException {
JavaType type = baseType;
final TypeFactory tf = config.getTypeFactory();
final JsonSerialize jsonSer = _findAnnotation(a, JsonSerialize.class);
// Ok: start by refining the main type itself; common to all types
final Class<?> serClass = (jsonSer == null) ? null : _classIfExplicit(jsonSer.as());
if (serClass != null) {
if (type.hasRawClass(serClass)) {
// 30-Nov-2015, tatu: As per [databind#1023], need to allow forcing of
// static typing this way
type = type.withStaticTyping();
} else {
Class<?> currRaw = type.getRawClass();
try {
// may be needed here too in future?
if (serClass.isAssignableFrom(currRaw)) {
// common case
type = tf.constructGeneralizedType(type, serClass);
} else if (currRaw.isAssignableFrom(serClass)) {
// specialization, ok as well
type = tf.constructSpecializedType(type, serClass);
} else {
throw new JsonMappingException(null, String.format("Can not refine serialization type %s into %s; types not related", type, serClass.getName()));
}
} catch (IllegalArgumentException iae) {
throw new JsonMappingException(null, String.format("Failed to widen type %s with annotation (value %s), from '%s': %s", type, serClass.getName(), a.getName(), iae.getMessage()), iae);
}
}
}
// First, key type (for Maps, Map-like types):
if (type.isMapLikeType()) {
JavaType keyType = type.getKeyType();
final Class<?> keyClass = (jsonSer == null) ? null : _classIfExplicit(jsonSer.keyAs());
if (keyClass != null) {
if (keyType.hasRawClass(keyClass)) {
keyType = keyType.withStaticTyping();
} else {
Class<?> currRaw = keyType.getRawClass();
try {
// is needed.
if (keyClass.isAssignableFrom(currRaw)) {
// common case
keyType = tf.constructGeneralizedType(keyType, keyClass);
} else if (currRaw.isAssignableFrom(keyClass)) {
// specialization, ok as well
keyType = tf.constructSpecializedType(keyType, keyClass);
} else {
throw new JsonMappingException(null, String.format("Can not refine serialization key type %s into %s; types not related", keyType, keyClass.getName()));
}
} catch (IllegalArgumentException iae) {
throw new JsonMappingException(null, String.format("Failed to widen key type of %s with concrete-type annotation (value %s), from '%s': %s", type, keyClass.getName(), a.getName(), iae.getMessage()), iae);
}
}
type = ((MapLikeType) type).withKeyType(keyType);
}
}
JavaType contentType = type.getContentType();
if (contentType != null) {
// collection[like], map[like], array, reference
// And then value types for all containers:
final Class<?> contentClass = (jsonSer == null) ? null : _classIfExplicit(jsonSer.contentAs());
if (contentClass != null) {
if (contentType.hasRawClass(contentClass)) {
contentType = contentType.withStaticTyping();
} else {
// 03-Apr-2016, tatu: As per [databind#1178], may need to actually
// specialize (narrow) type sometimes, even if more commonly opposite
// is needed.
Class<?> currRaw = contentType.getRawClass();
try {
if (contentClass.isAssignableFrom(currRaw)) {
// common case
contentType = tf.constructGeneralizedType(contentType, contentClass);
} else if (currRaw.isAssignableFrom(contentClass)) {
// specialization, ok as well
contentType = tf.constructSpecializedType(contentType, contentClass);
} else {
throw new JsonMappingException(null, String.format("Can not refine serialization content type %s into %s; types not related", contentType, contentClass.getName()));
}
} catch (IllegalArgumentException iae) {
// shouldn't really happen
throw new JsonMappingException(null, String.format("Internal error: failed to refine value type of %s with concrete-type annotation (value %s), from '%s': %s", type, contentClass.getName(), a.getName(), iae.getMessage()), iae);
}
}
type = type.withContentType(contentType);
}
}
return type;
}
use of com.fasterxml.jackson.databind.type.TypeFactory in project jackson-databind by FasterXML.
the class JacksonAnnotationIntrospector method refineDeserializationType.
/*
/**********************************************************
/* Deserialization: type modifications
/**********************************************************
*/
@Override
public JavaType refineDeserializationType(final MapperConfig<?> config, final Annotated a, final JavaType baseType) throws JsonMappingException {
JavaType type = baseType;
final TypeFactory tf = config.getTypeFactory();
final JsonDeserialize jsonDeser = _findAnnotation(a, JsonDeserialize.class);
// Ok: start by refining the main type itself; common to all types
final Class<?> valueClass = (jsonDeser == null) ? null : _classIfExplicit(jsonDeser.as());
if ((valueClass != null) && !type.hasRawClass(valueClass)) {
try {
type = tf.constructSpecializedType(type, valueClass);
} catch (IllegalArgumentException iae) {
throw new JsonMappingException(null, String.format("Failed to narrow type %s with annotation (value %s), from '%s': %s", type, valueClass.getName(), a.getName(), iae.getMessage()), iae);
}
}
// First, key type (for Maps, Map-like types):
if (type.isMapLikeType()) {
JavaType keyType = type.getKeyType();
final Class<?> keyClass = (jsonDeser == null) ? null : _classIfExplicit(jsonDeser.keyAs());
if (keyClass != null) {
try {
keyType = tf.constructSpecializedType(keyType, keyClass);
type = ((MapLikeType) type).withKeyType(keyType);
} catch (IllegalArgumentException iae) {
throw new JsonMappingException(null, String.format("Failed to narrow key type of %s with concrete-type annotation (value %s), from '%s': %s", type, keyClass.getName(), a.getName(), iae.getMessage()), iae);
}
}
}
JavaType contentType = type.getContentType();
if (contentType != null) {
// collection[like], map[like], array, reference
// And then value types for all containers:
final Class<?> contentClass = (jsonDeser == null) ? null : _classIfExplicit(jsonDeser.contentAs());
if (contentClass != null) {
try {
contentType = tf.constructSpecializedType(contentType, contentClass);
type = type.withContentType(contentType);
} catch (IllegalArgumentException iae) {
throw new JsonMappingException(null, String.format("Failed to narrow value type of %s with concrete-type annotation (value %s), from '%s': %s", type, contentClass.getName(), a.getName(), iae.getMessage()), iae);
}
}
}
return type;
}
Aggregations