List of usage examples for com.fasterxml.jackson.databind ObjectMapper constructType
public JavaType constructType(Type t)
java.lang.Class
), but without explicit context. From source file:com.addthis.codec.plugins.PluginRegistry.java
public PluginRegistry(@Nonnull Config config) { this.config = config; Config defaultPluginMapSettings = config.getConfig(PLUGIN_DEFAULTS_PATH); String pluginPath = config.getString(PLUGINS_PATH_PATH); Config pluginConfigs = config.getConfig(pluginPath); Set<String> categories = pluginConfigs.root().keySet(); Map<String, PluginMap> mapsFromConfig = new HashMap<>(categories.size()); BiMap<Class<?>, PluginMap> mapsFromConfigByClass = HashBiMap.create(categories.size()); // Throwaway ObjectMapper to facilitate AnnotatedClass construction below ObjectMapper temp = new ObjectMapper(); for (String category : categories) { Config pluginConfig = pluginConfigs.getConfig(category).withFallback(defaultPluginMapSettings); PluginMap pluginMap = new PluginMap(category, pluginConfig); mapsFromConfig.put(category, pluginMap); Class<?> baseClass = pluginMap.baseClass(); if (baseClass != null) { // if two categories define _class, then ensure the annotated one (if any) is the canonical one if (mapsFromConfigByClass.containsKey(baseClass)) { AnnotatedClass annotatedClass = AnnotatedClass.construct(temp.constructType(baseClass), temp.getDeserializationConfig()); String existingCategory = mapsFromConfigByClass.get(baseClass).category(); if (!annotatedClass.hasAnnotation(Pluggable.class) || !annotatedClass.getAnnotation(Pluggable.class).value().equals(existingCategory)) { mapsFromConfigByClass.put(pluginMap.baseClass(), pluginMap); }// w w w . j ava2 s . c o m } else { mapsFromConfigByClass.put(pluginMap.baseClass(), pluginMap); } } } pluginMapsByCategory = Collections.unmodifiableMap(mapsFromConfig); pluginMapsByClass = Maps.unmodifiableBiMap(mapsFromConfigByClass); }
From source file:org.versly.rest.wsdoc.AnnotationProcessor.java
String jsonSchemaFromTypeMirror(TypeMirror type) { String serializedSchema = null; if (type.getKind().isPrimitive() || type.getKind() == TypeKind.VOID) { return null; }/*from w w w . java 2 s . c o m*/ // we need the dto class to generate schema using jackson json-schema module // note: Types.erasure() provides canonical names whereas Class.forName() wants a "regular" name, // so forName will fail for nested and inner classes as "regular" names use $ between parent and child. Class dtoClass = null; StringBuffer erasure = new StringBuffer(_typeUtils.erasure(type).toString()); for (boolean done = false; !done;) { try { dtoClass = Class.forName(erasure.toString()); done = true; } catch (ClassNotFoundException e) { if (erasure.lastIndexOf(".") != -1) { erasure.setCharAt(erasure.lastIndexOf("."), '$'); } else { done = true; } } } // if we were able to figure out the dto class, use jackson json-schema module to serialize it Exception e = null; if (dtoClass != null) { try { ObjectMapper m = new ObjectMapper(); m.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); m.registerModule(new JodaModule()); SchemaFactoryWrapper visitor = new SchemaFactoryWrapper(); m.acceptJsonFormatVisitor(m.constructType(dtoClass), visitor); serializedSchema = m.writeValueAsString(visitor.finalSchema()); } catch (Exception ex) { e = ex; } } // report warning if we were not able to generate schema for non-primitive type if (serializedSchema == null) { this.processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "cannot generate json-schema for class " + type.toString() + " (erasure " + erasure + "), " + ((e != null) ? ("exception: " + e.getMessage()) : "class not found")); } return serializedSchema; }
From source file:org.mongojack.JacksonDBCollection.java
/** * Wraps a DB collection in a JacksonDBCollection, using the given object * mapper.// w ww .j a v a 2s. com * * JacksonDBCollection requires a specially configured object mapper to * work. It does not automatically configure the object mapper passed into * this method, because the same object mapper might be passed into multiple * calls to this method. Consequently, it is up to the caller to ensure that * the object mapper has been configured for use by JacksonDBCollection. * This can be done by passing the object mapper to * {@link org.mongojack.internal.MongoJackModule#configure(com.fasterxml.jackson.databind.ObjectMapper)} . * * @param dbCollection * The DB collection to wrap * @param type * The type of objects to deserialize to * @param objectMapper * The ObjectMapper to configure. * @return The wrapped collection */ public static <T, K> JacksonDBCollection<T, K> wrap(DBCollection dbCollection, Class<T> type, Class<K> keyType, ObjectMapper objectMapper) { return new JacksonDBCollection<T, K>(dbCollection, objectMapper.constructType(type), objectMapper.constructType(keyType), objectMapper, null, null); }