@Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.registerModule(new Ocpp12JacksonModule()); mapper.registerModule(new Ocpp15JacksonModule()); mapper.setAnnotationIntrospector( AnnotationIntrospector.pair( new JacksonAnnotationIntrospector(), new JaxbAnnotationIntrospector(mapper.getTypeFactory()) ) ); return mapper; }
public Collection<NamedType> collectAndResolveSubtypes(AnnotatedClass paramAnnotatedClass, MapperConfig<?> paramMapperConfig, AnnotationIntrospector paramAnnotationIntrospector) { HashMap localHashMap = new HashMap(); if (this._registeredSubtypes != null) { Class localClass = paramAnnotatedClass.getRawType(); Iterator localIterator = this._registeredSubtypes.iterator(); while (localIterator.hasNext()) { NamedType localNamedType = (NamedType)localIterator.next(); if (localClass.isAssignableFrom(localNamedType.getType())) _collectAndResolve(AnnotatedClass.constructWithoutSuperTypes(localNamedType.getType(), paramAnnotationIntrospector, paramMapperConfig), localNamedType, paramMapperConfig, paramAnnotationIntrospector, localHashMap); } } _collectAndResolve(paramAnnotatedClass, new NamedType(paramAnnotatedClass.getRawType(), null), paramMapperConfig, paramAnnotationIntrospector, localHashMap); return new ArrayList(localHashMap.values()); }
public JsonSerializer<?> findConvertingContentSerializer(SerializerProvider paramSerializerProvider, BeanProperty paramBeanProperty, JsonSerializer<?> paramJsonSerializer) { AnnotationIntrospector localAnnotationIntrospector = paramSerializerProvider.getAnnotationIntrospector(); if ((localAnnotationIntrospector != null) && (paramBeanProperty != null)) { Object localObject = localAnnotationIntrospector.findSerializationContentConverter(paramBeanProperty.getMember()); if (localObject != null) { Converter localConverter = paramSerializerProvider.converterInstance(paramBeanProperty.getMember(), localObject); JavaType localJavaType = localConverter.getOutputType(paramSerializerProvider.getTypeFactory()); if (paramJsonSerializer == null) paramJsonSerializer = paramSerializerProvider.findValueSerializer(localJavaType, paramBeanProperty); return new StdDelegatingSerializer(localConverter, localJavaType, paramJsonSerializer); } } return paramJsonSerializer; }
public TypeSerializer createTypeSerializer(SerializationConfig paramSerializationConfig, JavaType paramJavaType) { AnnotatedClass localAnnotatedClass = paramSerializationConfig.introspectClassAnnotations(paramJavaType.getRawClass()).getClassInfo(); AnnotationIntrospector localAnnotationIntrospector = paramSerializationConfig.getAnnotationIntrospector(); TypeResolverBuilder localTypeResolverBuilder = localAnnotationIntrospector.findTypeResolver(paramSerializationConfig, localAnnotatedClass, paramJavaType); Collection localCollection; if (localTypeResolverBuilder == null) { localTypeResolverBuilder = paramSerializationConfig.getDefaultTyper(paramJavaType); localCollection = null; } else { localCollection = paramSerializationConfig.getSubtypeResolver().collectAndResolveSubtypes(localAnnotatedClass, paramSerializationConfig, localAnnotationIntrospector); } if (localTypeResolverBuilder == null) return null; return localTypeResolverBuilder.buildTypeSerializer(paramSerializationConfig, paramJavaType, localCollection); }
public Map<String, AnnotatedMember> findBackReferenceProperties() { HashMap localHashMap = null; Iterator localIterator = this._properties.iterator(); while (localIterator.hasNext()) { AnnotatedMember localAnnotatedMember = ((BeanPropertyDefinition)localIterator.next()).getMutator(); if (localAnnotatedMember != null) { AnnotationIntrospector.ReferenceProperty localReferenceProperty = this._annotationIntrospector.findReferenceType(localAnnotatedMember); if ((localReferenceProperty != null) && (localReferenceProperty.isBackReference())) { if (localHashMap == null) localHashMap = new HashMap(); String str = localReferenceProperty.getName(); if (localHashMap.put(str, localAnnotatedMember) != null) throw new IllegalArgumentException("Multiple back-reference properties with name '" + str + "'"); } } } return localHashMap; }
protected POJOPropertiesCollector(MapperConfig<?> paramMapperConfig, boolean paramBoolean, JavaType paramJavaType, AnnotatedClass paramAnnotatedClass, String paramString) { this._config = paramMapperConfig; this._forSerialization = paramBoolean; this._type = paramJavaType; this._classDef = paramAnnotatedClass; String str; if (paramString == null) str = "set"; else str = paramString; this._mutatorPrefix = str; AnnotationIntrospector localAnnotationIntrospector; if (paramMapperConfig.isAnnotationProcessingEnabled()) localAnnotationIntrospector = this._config.getAnnotationIntrospector(); else localAnnotationIntrospector = null; this._annotationIntrospector = localAnnotationIntrospector; if (this._annotationIntrospector == null) { this._visibilityChecker = this._config.getDefaultVisibilityChecker(); return; } this._visibilityChecker = this._annotationIntrospector.findAutoDetectVisibility(paramAnnotatedClass, this._config.getDefaultVisibilityChecker()); }
protected void _addInjectables() { AnnotationIntrospector localAnnotationIntrospector = this._annotationIntrospector; if (localAnnotationIntrospector == null) return; Iterator localIterator1 = this._classDef.fields().iterator(); while (localIterator1.hasNext()) { AnnotatedField localAnnotatedField = (AnnotatedField)localIterator1.next(); _doAddInjectable(localAnnotationIntrospector.findInjectableValueId(localAnnotatedField), localAnnotatedField); } Iterator localIterator2 = this._classDef.memberMethods().iterator(); while (localIterator2.hasNext()) { AnnotatedMethod localAnnotatedMethod = (AnnotatedMethod)localIterator2.next(); if (localAnnotatedMethod.getParameterCount() == 1) _doAddInjectable(localAnnotationIntrospector.findInjectableValueId(localAnnotatedMethod), localAnnotatedMethod); } }
protected void _addMethods() { AnnotationIntrospector localAnnotationIntrospector = this._annotationIntrospector; Iterator localIterator = this._classDef.memberMethods().iterator(); while (localIterator.hasNext()) { AnnotatedMethod localAnnotatedMethod = (AnnotatedMethod)localIterator.next(); int i = localAnnotatedMethod.getParameterCount(); if (i == 0) { _addGetterMethod(localAnnotatedMethod, localAnnotationIntrospector); } else if (i == 1) { _addSetterMethod(localAnnotatedMethod, localAnnotationIntrospector); } else if ((i == 2) && (localAnnotationIntrospector != null) && (localAnnotationIntrospector.hasAnySetterAnnotation(localAnnotatedMethod))) { if (this._anySetters == null) this._anySetters = new LinkedList(); this._anySetters.add(localAnnotatedMethod); } } }
protected POJOPropertiesCollector collectPropertiesWithBuilder(MapperConfig<?> paramMapperConfig, JavaType paramJavaType, ClassIntrospector.MixInResolver paramMixInResolver, boolean paramBoolean) { AnnotationIntrospector localAnnotationIntrospector; if (paramMapperConfig.isAnnotationProcessingEnabled()) localAnnotationIntrospector = paramMapperConfig.getAnnotationIntrospector(); else localAnnotationIntrospector = null; AnnotatedClass localAnnotatedClass = AnnotatedClass.construct(paramJavaType.getRawClass(), localAnnotationIntrospector, paramMixInResolver); JsonPOJOBuilder.Value localValue1; if (localAnnotationIntrospector == null) localValue1 = null; else localValue1 = localAnnotationIntrospector.findPOJOBuilderConfig(localAnnotatedClass); JsonPOJOBuilder.Value localValue2 = localValue1; String str; if (localValue1 == null) str = "with"; else str = localValue2.withPrefix; return constructPropertyCollector(paramMapperConfig, localAnnotatedClass, paramJavaType, paramBoolean, str).collect(); }
public TypeDeserializer findTypeDeserializer(DeserializationConfig paramDeserializationConfig, JavaType paramJavaType) { AnnotatedClass localAnnotatedClass = paramDeserializationConfig.introspectClassAnnotations(paramJavaType.getRawClass()).getClassInfo(); AnnotationIntrospector localAnnotationIntrospector = paramDeserializationConfig.getAnnotationIntrospector(); Object localObject = localAnnotationIntrospector.findTypeResolver(paramDeserializationConfig, localAnnotatedClass, paramJavaType); Collection localCollection; if (localObject == null) { TypeResolverBuilder localTypeResolverBuilder = paramDeserializationConfig.getDefaultTyper(paramJavaType); localObject = localTypeResolverBuilder; localCollection = null; if (localTypeResolverBuilder == null) return null; } else { localCollection = paramDeserializationConfig.getSubtypeResolver().collectAndResolveSubtypes(localAnnotatedClass, paramDeserializationConfig, localAnnotationIntrospector); } if ((((TypeResolverBuilder)localObject).getDefaultImpl() == null) && (paramJavaType.isAbstract())) { JavaType localJavaType = mapAbstractType(paramDeserializationConfig, paramJavaType); if ((localJavaType != null) && (localJavaType.getRawClass() != paramJavaType.getRawClass())) localObject = ((TypeResolverBuilder)localObject).defaultImpl(localJavaType.getRawClass()); } return ((TypeResolverBuilder)localObject).buildTypeDeserializer(paramDeserializationConfig, paramJavaType, localCollection); }
protected JsonDeserializer<?> findConvertingContentDeserializer(DeserializationContext paramDeserializationContext, BeanProperty paramBeanProperty, JsonDeserializer<?> paramJsonDeserializer) { AnnotationIntrospector localAnnotationIntrospector = paramDeserializationContext.getAnnotationIntrospector(); if ((localAnnotationIntrospector != null) && (paramBeanProperty != null)) { Object localObject = localAnnotationIntrospector.findDeserializationContentConverter(paramBeanProperty.getMember()); if (localObject != null) { Converter localConverter = paramDeserializationContext.converterInstance(paramBeanProperty.getMember(), localObject); JavaType localJavaType = localConverter.getInputType(paramDeserializationContext.getTypeFactory()); if (paramJsonDeserializer == null) paramJsonDeserializer = paramDeserializationContext.findContextualValueDeserializer(localJavaType, paramBeanProperty); return new StdDelegatingDeserializer(localConverter, localJavaType, paramJsonDeserializer); } } return paramJsonDeserializer; }
protected SettableBeanProperty constructSettableProperty(DeserializationContext paramDeserializationContext, BeanDescription paramBeanDescription, BeanPropertyDefinition paramBeanPropertyDefinition, Type paramType) { AnnotatedMember localAnnotatedMember = paramBeanPropertyDefinition.getMutator(); if (paramDeserializationContext.canOverrideAccessModifiers()) localAnnotatedMember.fixAccess(); JavaType localJavaType1 = paramBeanDescription.resolveType(paramType); BeanProperty.Std localStd = new BeanProperty.Std(paramBeanPropertyDefinition.getName(), localJavaType1, paramBeanPropertyDefinition.getWrapperName(), paramBeanDescription.getClassAnnotations(), localAnnotatedMember, paramBeanPropertyDefinition.isRequired()); JavaType localJavaType2 = resolveType(paramDeserializationContext, paramBeanDescription, localJavaType1, localAnnotatedMember); if (localJavaType2 != localJavaType1) localStd.withType(localJavaType2); JsonDeserializer localJsonDeserializer = findDeserializerFromAnnotation(paramDeserializationContext, localAnnotatedMember); JavaType localJavaType3 = modifyTypeByAnnotation(paramDeserializationContext, localAnnotatedMember, localJavaType2); TypeDeserializer localTypeDeserializer = (TypeDeserializer)localJavaType3.getTypeHandler(); Object localObject; if ((localAnnotatedMember instanceof AnnotatedMethod)) localObject = new MethodProperty(paramBeanPropertyDefinition, localJavaType3, localTypeDeserializer, paramBeanDescription.getClassAnnotations(), (AnnotatedMethod)localAnnotatedMember); else localObject = new FieldProperty(paramBeanPropertyDefinition, localJavaType3, localTypeDeserializer, paramBeanDescription.getClassAnnotations(), (AnnotatedField)localAnnotatedMember); if (localJsonDeserializer != null) localObject = ((SettableBeanProperty)localObject).withValueDeserializer(localJsonDeserializer); AnnotationIntrospector.ReferenceProperty localReferenceProperty = paramBeanPropertyDefinition.findReferenceType(); if ((localReferenceProperty != null) && (localReferenceProperty.isManagedReference())) ((SettableBeanProperty)localObject).setManagedReferenceName(localReferenceProperty.getName()); return localObject; }
public static EnumValues constructFromName(Class<Enum<?>> paramClass, AnnotationIntrospector paramAnnotationIntrospector) { Enum[] arrayOfEnum = (Enum[])ClassUtil.findEnumType(paramClass).getEnumConstants(); if (arrayOfEnum != null) { HashMap localHashMap = new HashMap(); int i = arrayOfEnum.length; for (int j = 0; j < i; j++) { Enum localEnum = arrayOfEnum[j]; localHashMap.put(localEnum, new SerializedString(paramAnnotationIntrospector.findEnumValue(localEnum))); } return new EnumValues(paramClass, localHashMap); } throw new IllegalArgumentException("Can not determine enum constants for Class " + paramClass.getName()); }
public static EnumValues constructFromToString(Class<Enum<?>> paramClass, AnnotationIntrospector paramAnnotationIntrospector) { Enum[] arrayOfEnum = (Enum[])ClassUtil.findEnumType(paramClass).getEnumConstants(); if (arrayOfEnum != null) { HashMap localHashMap = new HashMap(); int i = arrayOfEnum.length; for (int j = 0; j < i; j++) { Enum localEnum = arrayOfEnum[j]; localHashMap.put(localEnum, new SerializedString(localEnum.toString())); } return new EnumValues(paramClass, localHashMap); } throw new IllegalArgumentException("Can not determine enum constants for Class " + paramClass.getName()); }
@Override public Collection<NamedType> collectAndResolveSubtypes(AnnotatedClass type, MapperConfig<?> config, AnnotationIntrospector ai) { HashMap<NamedType, NamedType> subtypes = new HashMap<NamedType, NamedType>(); // [JACKSON-257] then consider registered subtypes (which have precedence over annotations) if (_registeredSubtypes != null) { Class<?> rawBase = type.getRawType(); for (NamedType subtype : _registeredSubtypes) { // is it a subtype of root type? if (rawBase.isAssignableFrom(subtype.getType())) { // yes AnnotatedClass curr = AnnotatedClass.constructWithoutSuperTypes(subtype.getType(), ai, config); _collectAndResolve(curr, subtype, config, ai, subtypes); } } } // and then check subtypes via annotations from base type (recursively) NamedType rootType = new NamedType(type.getRawType(), null); _collectAndResolve(type, rootType, config, ai, subtypes); return new ArrayList<NamedType>(subtypes.values()); }
/** * Annotation introspector to use for serialization process * is configured separately for serialization and deserialization purposes */ public BotObjectMapper() { final AnnotationIntrospector introspector = new JacksonAnnotationIntrospector(); super.getDeserializationConfig() .with(introspector); super.getSerializationConfig() .with(introspector); }
public static ObjectMapper defaultObjectMapper() { return new XmlMapper(new XmlFactory(new WstxInputFactory(), new WstxPrefixedOutputFactory())) .enable(SerializationFeature.INDENT_OUTPUT) .setSerializationInclusion(JsonInclude.Include.NON_NULL) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true) .configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true) .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) .registerModule(new ParserModule()) .setAnnotationIntrospector(AnnotationIntrospector.pair( new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()), new JacksonAnnotationIntrospector())); }
private static AnnotatedClass buildOldAnnotatedClass(Method method, Class<?> declaringClass, SerializationConfig serializationConfig) throws InvocationTargetException, IllegalAccessException { boolean useAnnotations = serializationConfig.isAnnotationProcessingEnabled(); AnnotationIntrospector aintr = useAnnotations ? serializationConfig.getAnnotationIntrospector() : null; return AnnotatedClass.class.cast(method.invoke(null, declaringClass, aintr, serializationConfig)); }
@Test public void testSerializeJSON() throws IOException { Person person = new Person(); person.setId(12345L); person.setFirstName("Peter"); person.setLastName("Walser"); person.setGender(Gender.MALE); person.setDateOfBirth(LocalDate.of(1975, 12, 20)); StringWriter buffer = new StringWriter(); ObjectMapper mapper = new ObjectMapper(); mapper.setAnnotationIntrospector( AnnotationIntrospector.pair( new JacksonAnnotationIntrospector(), new JaxbAnnotationIntrospector(mapper.getTypeFactory()) ) ); mapper.writerWithDefaultPrettyPrinter().writeValue(buffer, person); String jsonString = buffer.toString(); System.out.println(jsonString); Assert.assertTrue(jsonString.contains("\"id\" : 12345")); Assert.assertTrue(jsonString.contains("\"first_name\" : \"Peter\"")); Assert.assertTrue(jsonString.contains("\"last_name\" : \"Walser\"")); Assert.assertTrue(jsonString.contains("\"gender\" : \"MALE\"")); Assert.assertTrue(jsonString.contains("\"birth_date\" : \"1975-12-20\"")); }
/** * Returns the graph for json. * * @param <T> the generic type * @param json the json * @param graphClass the graph class * @return the graph for json * @throws Exception the exception */ public static <T> T getGraphForJson(String json, Class<T> graphClass) throws Exception { InputStream in = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory()); mapper.setAnnotationIntrospector(introspector); return mapper.readValue(in, graphClass); }
/** * Returns the json for graph. * * @param object the object * @return the json for graph * @throws Exception the exception */ public static String getJsonForGraph(Object object) throws Exception { ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory()); mapper.setAnnotationIntrospector(introspector); return mapper.writeValueAsString(object); }
@Override public BasicBeanDescription forDirectClassAnnotations(MapperConfig<?> cfg, JavaType type, MixInResolver r) { BasicBeanDescription desc = _findStdTypeDesc(type); if (desc == null) { boolean useAnnotations = cfg.isAnnotationProcessingEnabled(); AnnotationIntrospector ai = cfg.getAnnotationIntrospector(); AnnotatedClass ac = annotatedClassConstructor.constructWithoutSuperTypes(type.getRawClass(), (useAnnotations ? ai : null), r); desc = BasicBeanDescription.forOtherUse(cfg, type, ac); } return desc; }
protected POJOPropertiesCollector collectPropertiesWithBuilder(MapperConfig<?> config, JavaType type, MixInResolver r, boolean forSerialization) { boolean useAnnotations = config.isAnnotationProcessingEnabled(); AnnotationIntrospector ai = useAnnotations ? config.getAnnotationIntrospector() : null; AnnotatedClass ac; if (forSerialization) { ac = annotatedClassConstructor.constructForSerialization(type.getRawClass(), ai, r); } else { ac = annotatedClassConstructor.constructForDeserialization(type.getRawClass(), ai, r); } JsonPOJOBuilder.Value builderConfig = (ai == null) ? null : ai.findPOJOBuilderConfig(ac); String mutatorPrefix = (builderConfig == null) ? "with" : builderConfig.withPrefix; return constructPropertyCollector(config, ac, type, forSerialization, mutatorPrefix).collect(); }
/** * Creates an Endpoints standard object mapper that allows unquoted field names and unknown * properties. * * Note on unknown properties: When Apiary FE supports a strict mode where properties * are checked against the schema, BE can just ignore unknown properties. This way, FE does * not need to filter out everything that the BE doesn't understand. Before that's done, * a property name with a typo in it, for example, will just be ignored by the BE. */ public static ObjectMapper createStandardObjectMapper(ApiSerializationConfig config) { ObjectMapper objectMapper = new ObjectMapper() .configure(JsonParser.Feature.ALLOW_COMMENTS, true) .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true) .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .setBase64Variant(Base64Variants.MODIFIED_FOR_URL) .setSerializerFactory( BeanSerializerFactory.instance.withSerializerModifier(new DeepEmptyCheckingModifier())); AnnotationIntrospector pair = AnnotationIntrospector.pair( new ApiAnnotationIntrospector(config), new JacksonAnnotationIntrospector()); objectMapper.setAnnotationIntrospector(pair); return objectMapper; }
public void receiveHeaterMessageFromMeter(String msg) throws Exception { // parse to JSON and send to database // Setting up Jackson Tree Model with ObjectMapper ObjectMapper om = new ObjectMapper(); AnnotationIntrospector introspectorJackson = new JacksonAnnotationIntrospector(); om.setAnnotationIntrospector(introspectorJackson); // Retrieve JSON responses from controller and build Jackson Tree // Model with package de.fzi.iik.habiteq.jackson BcontrolHeaterData bchd = null; // try { bchd = om.readValue(msg, BcontrolHeaterData.class); // } // catch (JsonParseException e) { // getGlobalLogger().logWarning(e.getStackTrace(), e); // } // catch (JsonMappingException e) { // getGlobalLogger().logWarning(e.getStackTrace(), e); // } // catch (IOException e) { // getGlobalLogger().logWarning(e.getStackTrace(), e); // } // getGlobalLogger().logDebug(msg); // getGlobalLogger().logDebug(bcmd); // save to registry getDriverRegistry().setStateOfSender( BcontrolHeaterDriverRawLogDetails.class, convertJsonToRawDetails(bchd)); }
/** * Build a default Jackson ObjectMapper. The default implementation is to * include non-null, ignore uknown properties on deserialization, and use * the date format yyyy-MM-dd'T'HH:mm:ssZ. * @return A Jackson ObjectMapper. */ protected ObjectMapper buildDefaultJacksonObjectMapper() { /* Cant use unless Spring 4.x is being used throughout return new Jackson2ObjectMapperBuilder() .serializationInclusion(JsonInclude.Include.NON_NULL) .failOnUnknownProperties(false) .dateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")) .featuresToEnable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME) .annotationIntrospector(AnnotationIntrospector.pair( new JacksonAnnotationIntrospector(), new JaxbAnnotationIntrospector())) .build(); */ ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")); mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME); mapper.registerModule(new JodaModule()); AnnotationIntrospector primary = new JacksonAnnotationIntrospector(); AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(); mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(primary, secondary)); return mapper; }
public JacksonProvider() { AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()); // if using BOTH JAXB annotations AND Jackson annotations: AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); ObjectMapper mapper = new ObjectMapper().registerModule(new Hibernate4Module()) .setSerializationInclusion(Include.NON_NULL) .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false).enable(SerializationFeature.INDENT_OUTPUT) .setAnnotationIntrospector(new AnnotationIntrospectorPair(introspector, secondary)); // mapper = mapper.setSerializationInclusion(Include) setMapper(mapper); }
public ObjectMapperResolver() { objectMapper = new ObjectMapper(); AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()); AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); objectMapper = objectMapper.setAnnotationIntrospector(new AnnotationIntrospectorPair(introspector, secondary)); objectMapper = objectMapper.enable(SerializationFeature.INDENT_OUTPUT); objectMapper.registerModule(new Hibernate4Module()); }
/** * Returns the graph for json. * * @param <T> the generic type * @param json the json * @param graphClass the graph class * @return the graph for json * @throws Exception the exception */ public static <T> T getGraphForJson(String json, Class<T> graphClass) throws Exception { if (ConfigUtility.isEmpty(json)) { return null; } InputStream in = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory()); mapper.setAnnotationIntrospector(introspector); return mapper.readValue(in, graphClass); }
/** * Returns the graph for json. sample usage: * * <pre> * List<ConceptJpa> list = ConfigUtility.getGraphForJson(str, new TypeReference<List<ConceptJpa>>{}); * </pre> * * @param <T> the * @param json the json * @param typeRef the type ref * @return the graph for json * @throws Exception the exception */ public static <T> T getGraphForJson(String json, TypeReference<T> typeRef) throws Exception { if (ConfigUtility.isEmpty(json)) { return null; } InputStream in = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory()); mapper.setAnnotationIntrospector(introspector); return mapper.readValue(in, typeRef); }
/** * Creates the combined object mapper. * * @return the object mapper */ private static ObjectMapper createDefaultMapper() { ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory()); mapper.setAnnotationIntrospector(introspector); // final AnnotationIntrospector jacksonIntrospector = // new JacksonAnnotationIntrospector(); // return AnnotationIntrospector.pair(jacksonIntrospector, // jaxbIntrospector); return mapper; }
@Override public void setupModule(SetupContext context) { /* First, append annotation introspector (no need to override, esp. * as we just implement couple of methods) */ // Then add serializers we need AnnotationIntrospector ai = annotationIntrospector(); if (ai != null) { context.appendAnnotationIntrospector(ai); } context.addSerializers(new HibernateSerializers(_moduleFeatures)); }
public static ObjectMapper getJSONMapper() { ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector primary = new JaxbAnnotationIntrospector(mapper.getTypeFactory()); AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(primary, secondary)); setupMapper(mapper); return mapper; }
public static XmlMapper getXMLMapper() { XmlMapper mapper = new XmlMapper(); AnnotationIntrospector primary = new XmlJaxbAnnotationIntrospector(mapper.getTypeFactory()); AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(primary, secondary)); setupMapper(mapper); return mapper; }
public ApiObjectMapper() { // Print the JSON with indentation (ie. pretty print) configure(SerializationFeature.INDENT_OUTPUT, true); // Allow JAX-B annotations. setAnnotationIntrospector(AnnotationIntrospector.pair(getSerializationConfig().getAnnotationIntrospector(), new JaxbAnnotationIntrospector())); // Make Jackson respect @XmlElementWrapper. enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME); // Print all dates in ISO8601 format setDateFormat(makeISODateFormat()); }
private static ObjectMapper initialize( ObjectMapper mapper, boolean defaultTyping, boolean nonNullInclusion ) { if( mapper instanceof XmlMapper ) { ( ( XmlMapper ) mapper ).setDefaultUseWrapper( false ); } AnnotationIntrospector introspector = new JacksonAnnotationIntrospector(); mapper.getDeserializationConfig().with( introspector ); mapper.getSerializationConfig().with( introspector ); mapper.registerModule( new AfterburnerModule() ); mapper.registerModule( new Jdk8Module().configureAbsentsAsNulls( true ) ); mapper.registerModule( new JodaModule() ); mapper.registerModule( new ParameterNamesModule( JsonCreator.Mode.DEFAULT ) ); mapper.enable( DeserializationFeature.USE_LONG_FOR_INTS ); mapper.enable( JsonParser.Feature.ALLOW_SINGLE_QUOTES ); mapper.disable( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES ); mapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS ); mapper.disable( SerializationFeature.WRITE_EMPTY_JSON_ARRAYS ); mapper.disable( SerializationFeature.FAIL_ON_EMPTY_BEANS ); mapper.configure( JsonGenerator.Feature.AUTO_CLOSE_TARGET, false ); mapper.setVisibility( PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY ); if( !nonNullInclusion ) mapper.setSerializationInclusion( JsonInclude.Include.NON_NULL ); modules.forEach( mapper::registerModule ); if( defaultTyping ) mapper.enableDefaultTyping( ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY ); return mapper; }