List of usage examples for com.fasterxml.jackson.databind.introspect AnnotatedField getName
public String getName()
From source file:ca.oson.json.Oson.java
public ObjectMapper getJackson() { if (jackson == null) { jackson = new ObjectMapper(); prefixProcessing();/* w w w. j a v a 2s . c om*/ // do not fail for funny reason jackson.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); jackson.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); switch (getDefaultType()) { case ALWAYS: jackson.setSerializationInclusion(Include.ALWAYS); break; case NON_NULL: jackson.setSerializationInclusion(Include.NON_NULL); break; case NON_EMPTY: jackson.setSerializationInclusion(Include.NON_EMPTY); break; case DEFAULT: jackson.setSerializationInclusion(Include.USE_DEFAULTS); break; default: jackson.setSerializationInclusion(Include.NON_NULL); break; } if (getPrettyPrinting() && getIndentation() > 0) { jackson.enable(SerializationFeature.INDENT_OUTPUT); jackson.configure(SerializationFeature.INDENT_OUTPUT, true); } if (getOrderByKeyAndProperties()) { jackson.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); } Set<FieldMapper> mappers = getFieldMappers(); if (mappers != null) { //defines how names of JSON properties ("external names") are derived from names of POJO methods and fields ("internal names"), // in cases where they are not auto-detected and no explicit annotations exist for naming // config - Configuration in used: either SerializationConfig or DeserializationConfig, // depending on whether method is called during serialization or deserialization jackson.setPropertyNamingStrategy(new PropertyNamingStrategy() { @Override public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) { String name = defaultName; if (name == null) { name = field.getName(); } name = name.trim(); if (name.isEmpty()) { return super.nameForField(config, field, defaultName); } String lname = name.toLowerCase(); if (config instanceof SerializationConfig || !(config instanceof DeserializationConfig)) { for (FieldMapper fieldMapper : mappers) { String java = fieldMapper.java; if (java != null && lname.equals(java.toLowerCase())) { Class<?> type = fieldMapper.getType(); String fullName = field.getFullName(); // ca.oson.json.Artist#age; int index = fullName.indexOf('#'); if (index > -1) { fullName = fullName.substring(0, index); } if (type != null) { try { // mapper.getType().getTypeName() is the same as mapper.getType().getName() if (type.getTypeName().equals(fullName)) { return fieldMapper.json; } } catch (SecurityException e) { // e.printStackTrace(); } } else { // just by name return fieldMapper.json; } } } } if (config instanceof DeserializationConfig || !(config instanceof SerializationConfig)) { for (FieldMapper fieldMapper : mappers) { String json = fieldMapper.json; if (json != null && lname.equals(json.toLowerCase())) { Class<?> type = fieldMapper.getType(); String fullName = field.getFullName(); // ca.oson.json.Artist#age; int index = fullName.indexOf('#'); if (index > -1) { fullName = fullName.substring(0, index); } if (type != null) { try { // mapper.getType().getTypeName() is the same as mapper.getType().getName() if (type.getTypeName().equals(fullName)) { return fieldMapper.java; } } catch (SecurityException e) { // e.printStackTrace(); } } else { // just by name return fieldMapper.java; } } } } return super.nameForField(config, field, defaultName); } @Override public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) { String name = defaultName; if (name == null) { name = method.getName().substring(3); } name = name.trim(); if (name.isEmpty()) { return super.nameForGetterMethod(config, method, defaultName); } String lname = name.toLowerCase(); if (config instanceof SerializationConfig || !(config instanceof DeserializationConfig)) { for (FieldMapper fieldMapper : mappers) { String java = fieldMapper.java; if (java != null && lname.equals(java.toLowerCase())) { Class<?> type = fieldMapper.getType(); String fullName = method.getFullName(); // java.util.Date#getTime(0 params) int index = fullName.indexOf('#'); if (index > -1) { fullName = fullName.substring(0, index); } if (type != null) { try { // mapper.getType().getTypeName() is the same as mapper.getType().getName() if (type.getTypeName().equals(fullName)) { return fieldMapper.json; } } catch (SecurityException e) { // e.printStackTrace(); } } else { // just by name return fieldMapper.json; } } } } if (config instanceof DeserializationConfig || !(config instanceof SerializationConfig)) { for (FieldMapper fieldMapper : mappers) { String json = fieldMapper.json; if (json != null && lname.equals(json.toLowerCase())) { Class<?> type = fieldMapper.getType(); String fullName = method.getFullName(); // java.util.Date#getTime(0 params) int index = fullName.indexOf('#'); if (index > -1) { fullName = fullName.substring(0, index); } if (type != null) { try { // mapper.getType().getTypeName() is the same as mapper.getType().getName() if (type.getTypeName().equals(fullName)) { return fieldMapper.java; } } catch (SecurityException e) { // e.printStackTrace(); } } else { // just by name return fieldMapper.java; } } } } return super.nameForGetterMethod(config, method, defaultName); } /* * (non-Javadoc) * @see com.fasterxml.jackson.databind.PropertyNamingStrategy#nameForSetterMethod(com.fasterxml.jackson.databind.cfg.MapperConfig, com.fasterxml.jackson.databind.introspect.AnnotatedMethod, java.lang.String) * * Method called to find external name (name used in JSON) for given logical POJO property, as defined by given setter method; typically called when building a deserializer (but not necessarily only then). */ @Override public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) { String name = defaultName; if (name == null) { name = method.getName().substring(3); } name = name.trim(); if (name.isEmpty()) { return super.nameForSetterMethod(config, method, defaultName); } String lname = name.toLowerCase(); if (config instanceof SerializationConfig || !(config instanceof DeserializationConfig)) { for (FieldMapper fieldMapper : mappers) { String java = fieldMapper.java; if (java != null && lname.equals(java.toLowerCase())) { Class<?> type = fieldMapper.getType(); String fullName = method.getFullName(); // java.util.Date#getTime(0 params) int index = fullName.indexOf('#'); if (index > -1) { fullName = fullName.substring(0, index); } if (type != null) { try { // mapper.getType().getTypeName() is the same as mapper.getType().getName() if (type.getTypeName().equals(fullName)) { return fieldMapper.json; } } catch (SecurityException e) { // e.printStackTrace(); } } else { // just by name return fieldMapper.json; } } } } if (config instanceof DeserializationConfig || !(config instanceof SerializationConfig)) { for (FieldMapper fieldMapper : mappers) { String json = fieldMapper.json; if (json != null && lname.equals(json.toLowerCase())) { Class<?> type = fieldMapper.getType(); String fullName = method.getFullName(); // java.util.Date#getTime(0 params) int index = fullName.indexOf('#'); if (index > -1) { fullName = fullName.substring(0, index); } if (type != null) { try { // mapper.getType().getTypeName() is the same as mapper.getType().getName() if (type.getTypeName().equals(fullName)) { return fieldMapper.java; } } catch (SecurityException e) { // e.printStackTrace(); } } else { // just by name return fieldMapper.java; } } } } return super.nameForSetterMethod(config, method, defaultName); } /* * (non-Javadoc) * @see com.fasterxml.jackson.databind.PropertyNamingStrategy#nameForConstructorParameter(com.fasterxml.jackson.databind.cfg.MapperConfig, com.fasterxml.jackson.databind.introspect.AnnotatedParameter, java.lang.String) * * find external name (name used in JSON) for given logical POJO property, as defined by given setter method; typically called when building a deserializer (but not necessarily only then). */ @Override public String nameForConstructorParameter(MapperConfig<?> config, AnnotatedParameter ctorParam, String defaultName) { String name = defaultName; if (name == null) { name = ctorParam.getName(); } name = name.trim(); if (name.isEmpty()) { return super.nameForConstructorParameter(config, ctorParam, defaultName); } String lname = name.toLowerCase(); if (config instanceof SerializationConfig || !(config instanceof DeserializationConfig)) { for (FieldMapper fieldMapper : mappers) { String java = fieldMapper.java; if (java != null && lname.equals(java.toLowerCase())) { Class<?> type = fieldMapper.getType(); String fullName = ctorParam.getName(); // java.util.Date# int index = fullName.indexOf('#'); if (index > -1) { fullName = fullName.substring(0, index); } if (type != null) { try { // mapper.getType().getTypeName() is the same as mapper.getType().getName() if (type.getTypeName().equals(fullName)) { return fieldMapper.json; } } catch (SecurityException e) { // e.printStackTrace(); } } else { // just by name String json = fieldMapper.json; if (json == null) { // do nothing? } else { return json; } } } } } if (config instanceof DeserializationConfig || !(config instanceof SerializationConfig)) { for (FieldMapper fieldMapper : mappers) { String json = fieldMapper.json; if (json != null && lname.equals(json.toLowerCase())) { Class<?> type = fieldMapper.getType(); String fullName = ctorParam.getName(); // java.util.Date# int index = fullName.indexOf('#'); if (index > -1) { fullName = fullName.substring(0, index); } if (type != null) { try { // mapper.getType().getTypeName() is the same as mapper.getType().getName() if (type.getTypeName().equals(fullName)) { return fieldMapper.java; } } catch (SecurityException e) { // e.printStackTrace(); } } else { // just by name return fieldMapper.java; } } } } return super.nameForConstructorParameter(config, ctorParam, defaultName); } }); } jackson.setDateFormat(getDateFormat()); } return jackson; }