use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty in project druid by druid-io.
the class JsonConfigurator method configurate.
public <T> T configurate(Properties props, String propertyPrefix, Class<T> clazz) throws ProvisionException {
verifyClazzIsConfigurable(jsonMapper, clazz);
// Make it end with a period so we only include properties with sub-object thingies.
final String propertyBase = propertyPrefix.endsWith(".") ? propertyPrefix : propertyPrefix + ".";
Map<String, Object> jsonMap = Maps.newHashMap();
for (String prop : props.stringPropertyNames()) {
if (prop.startsWith(propertyBase)) {
final String propValue = props.getProperty(prop);
Object value;
try {
// If it's a String Jackson wants it to be quoted, so check if it's not an object or array and quote.
String modifiedPropValue = propValue;
if (!(modifiedPropValue.startsWith("[") || modifiedPropValue.startsWith("{"))) {
modifiedPropValue = jsonMapper.writeValueAsString(propValue);
}
value = jsonMapper.readValue(modifiedPropValue, Object.class);
} catch (IOException e) {
log.info(e, "Unable to parse [%s]=[%s] as a json object, using as is.", prop, propValue);
value = propValue;
}
jsonMap.put(prop.substring(propertyBase.length()), value);
}
}
final T config;
try {
config = jsonMapper.convertValue(jsonMap, clazz);
} catch (IllegalArgumentException e) {
throw new ProvisionException(String.format("Problem parsing object at prefix[%s]: %s.", propertyPrefix, e.getMessage()), e);
}
final Set<ConstraintViolation<T>> violations = validator.validate(config);
if (!violations.isEmpty()) {
List<String> messages = Lists.newArrayList();
for (ConstraintViolation<T> violation : violations) {
String path = "";
try {
Class<?> beanClazz = violation.getRootBeanClass();
final Iterator<Path.Node> iter = violation.getPropertyPath().iterator();
while (iter.hasNext()) {
Path.Node next = iter.next();
if (next.getKind() == ElementKind.PROPERTY) {
final String fieldName = next.getName();
final Field theField = beanClazz.getDeclaredField(fieldName);
if (theField.getAnnotation(JacksonInject.class) != null) {
path = String.format(" -- Injected field[%s] not bound!?", fieldName);
break;
}
JsonProperty annotation = theField.getAnnotation(JsonProperty.class);
final boolean noAnnotationValue = annotation == null || Strings.isNullOrEmpty(annotation.value());
final String pathPart = noAnnotationValue ? fieldName : annotation.value();
if (path.isEmpty()) {
path += pathPart;
} else {
path += "." + pathPart;
}
}
}
} catch (NoSuchFieldException e) {
throw Throwables.propagate(e);
}
messages.add(String.format("%s - %s", path, violation.getMessage()));
}
throw new ProvisionException(Iterables.transform(messages, new Function<String, Message>() {
@Override
public Message apply(String input) {
return new Message(String.format("%s%s", propertyBase, input));
}
}));
}
log.info("Loaded class[%s] from props[%s] as [%s]", clazz, propertyBase, config);
return config;
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty in project pinot by linkedin.
the class SwaggerResource method schemaForType.
private JSONObject schemaForType(Class<?> type) {
try {
JSONObject schema = new JSONObject();
schema.put("type", "object");
schema.put("title", type.getSimpleName());
Example example = type.getAnnotation(Example.class);
if (example != null) {
schema.put("example", new JSONObject(example.value()));
}
for (Constructor<?> constructor : type.getConstructors()) {
if (constructor.isAnnotationPresent(JsonCreator.class)) {
JSONObject properties = new JSONObject();
JSONArray required = new JSONArray();
Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
Class<?>[] parameterTypes = constructor.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
Class<?> parameterType = parameterTypes[i];
Annotation[] annotations = parameterAnnotations[i];
if (annotations.length != 0) {
for (Annotation annotation : annotations) {
if (annotation instanceof JsonProperty) {
JsonProperty jsonPropertyAnnotation = (JsonProperty) annotation;
JSONObject parameter = new JSONObject();
properties.put(jsonPropertyAnnotation.value(), parameter);
if (parameterType.equals(String.class)) {
parameter.put("type", "string");
} else if (parameterType.equals(Boolean.class) || parameterType.equals(Boolean.TYPE)) {
parameter.put("type", "boolean");
} else if (parameterType.equals(Integer.class) || parameterType.equals(Integer.TYPE)) {
parameter.put("type", "integer");
} else if (parameterType.equals(Long.class) || parameterType.equals(Long.TYPE)) {
// Long maps to integer type in http://swagger.io/specification/#dataTypeFormat
parameter.put("type", "integer");
} else if (parameterType.equals(Float.class) || parameterType.equals(Float.TYPE)) {
parameter.put("type", "boolean");
} else if (parameterType.equals(Double.class) || parameterType.equals(Double.TYPE)) {
parameter.put("type", "double");
} else if (parameterType.equals(Byte.class) || parameterType.equals(Byte.TYPE)) {
// Byte maps to string type in http://swagger.io/specification/#dataTypeFormat
parameter.put("type", "string");
} else {
parameter.put("type", "string");
}
if (jsonPropertyAnnotation.required()) {
required.put(jsonPropertyAnnotation.value());
}
}
}
}
}
if (required.length() != 0) {
schema.put("required", required);
}
schema.put("properties", properties);
break;
}
}
return schema;
} catch (Exception e) {
return new JSONObject();
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty in project dhis2-core by dhis2.
the class Jackson2PropertyIntrospectorService method scanClass.
@Override
protected Map<String, Property> scanClass(Class<?> clazz) {
Map<String, Property> propertyMap = Maps.newHashMap();
Map<String, Property> hibernatePropertyMap = getPropertiesFromHibernate(clazz);
List<String> classFieldNames = ReflectionUtils.getAllFieldNames(clazz);
// TODO this is quite nasty, should find a better way of exposing properties at class-level
if (AnnotationUtils.isAnnotationPresent(clazz, JacksonXmlRootElement.class)) {
Property property = new Property();
JacksonXmlRootElement jacksonXmlRootElement = AnnotationUtils.getAnnotation(clazz, JacksonXmlRootElement.class);
if (!StringUtils.isEmpty(jacksonXmlRootElement.localName())) {
property.setName(jacksonXmlRootElement.localName());
}
if (!StringUtils.isEmpty(jacksonXmlRootElement.namespace())) {
property.setNamespace(jacksonXmlRootElement.namespace());
}
propertyMap.put("__self__", property);
}
List<Property> properties = collectProperties(clazz);
for (Property property : properties) {
Method getterMethod = property.getGetterMethod();
JsonProperty jsonProperty = AnnotationUtils.getAnnotation(getterMethod, JsonProperty.class);
String fieldName = getFieldName(getterMethod);
property.setName(!StringUtils.isEmpty(jsonProperty.value()) ? jsonProperty.value() : fieldName);
if (property.getGetterMethod() != null) {
property.setReadable(true);
}
if (property.getSetterMethod() != null) {
property.setWritable(true);
}
if (classFieldNames.contains(fieldName)) {
property.setFieldName(fieldName);
}
if (hibernatePropertyMap.containsKey(fieldName)) {
Property hibernateProperty = hibernatePropertyMap.get(fieldName);
property.setPersisted(true);
property.setWritable(true);
property.setUnique(hibernateProperty.isUnique());
property.setRequired(hibernateProperty.isRequired());
property.setLength(hibernateProperty.getLength());
property.setMax(hibernateProperty.getMax());
property.setMin(hibernateProperty.getMin());
property.setCollection(hibernateProperty.isCollection());
property.setCascade(hibernateProperty.getCascade());
property.setOwner(hibernateProperty.isOwner());
property.setManyToMany(hibernateProperty.isManyToMany());
property.setOneToOne(hibernateProperty.isOneToOne());
property.setManyToOne(hibernateProperty.isManyToOne());
property.setOwningRole(hibernateProperty.getOwningRole());
property.setInverseRole(hibernateProperty.getInverseRole());
property.setGetterMethod(hibernateProperty.getGetterMethod());
property.setSetterMethod(hibernateProperty.getSetterMethod());
}
if (AnnotationUtils.isAnnotationPresent(property.getGetterMethod(), Description.class)) {
Description description = AnnotationUtils.getAnnotation(property.getGetterMethod(), Description.class);
property.setDescription(description.value());
}
if (AnnotationUtils.isAnnotationPresent(property.getGetterMethod(), JacksonXmlProperty.class)) {
JacksonXmlProperty jacksonXmlProperty = AnnotationUtils.getAnnotation(getterMethod, JacksonXmlProperty.class);
if (StringUtils.isEmpty(jacksonXmlProperty.localName())) {
property.setName(property.getName());
} else {
property.setName(jacksonXmlProperty.localName());
}
if (!StringUtils.isEmpty(jacksonXmlProperty.namespace())) {
property.setNamespace(jacksonXmlProperty.namespace());
}
property.setAttribute(jacksonXmlProperty.isAttribute());
}
Class<?> returnType = property.getGetterMethod().getReturnType();
property.setKlass(Primitives.wrap(returnType));
if (Collection.class.isAssignableFrom(returnType)) {
property.setCollection(true);
property.setCollectionName(property.getName());
property.setOrdered(List.class.isAssignableFrom(returnType));
Type type = property.getGetterMethod().getGenericReturnType();
if (ParameterizedType.class.isInstance(type)) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Class<?> klass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
property.setItemKlass(Primitives.wrap(klass));
if (collectProperties(klass).isEmpty()) {
property.setSimple(true);
}
property.setIdentifiableObject(IdentifiableObject.class.isAssignableFrom(klass));
property.setNameableObject(NameableObject.class.isAssignableFrom(klass));
property.setEmbeddedObject(EmbeddedObject.class.isAssignableFrom(klass));
}
} else {
if (collectProperties(returnType).isEmpty()) {
property.setSimple(true);
}
}
if (property.isCollection()) {
if (AnnotationUtils.isAnnotationPresent(property.getGetterMethod(), JacksonXmlElementWrapper.class)) {
JacksonXmlElementWrapper jacksonXmlElementWrapper = AnnotationUtils.getAnnotation(getterMethod, JacksonXmlElementWrapper.class);
property.setCollectionWrapping(jacksonXmlElementWrapper.useWrapping());
// TODO what if element-wrapper have different namespace?
if (!StringUtils.isEmpty(jacksonXmlElementWrapper.localName())) {
property.setCollectionName(jacksonXmlElementWrapper.localName());
}
}
propertyMap.put(property.getCollectionName(), property);
} else {
propertyMap.put(property.getName(), property);
}
if (Enum.class.isAssignableFrom(property.getKlass())) {
Object[] enumConstants = property.getKlass().getEnumConstants();
List<String> enumValues = new ArrayList<>();
for (Object value : enumConstants) {
enumValues.add(value.toString());
}
property.setConstants(enumValues);
}
SchemaUtils.updatePropertyTypes(property);
}
return propertyMap;
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty in project hono by eclipse.
the class TenantObject method setAdapterConfigurations.
/**
* Sets the configuration information for this tenant's
* configured adapters.
*
* @param configurations A list of configuration properties, one set of properties
* for each configured adapter. The list's content will be
* copied into a new list in order to prevent modification
* of the list after this method has been invoked.
* @throws NullPointerException if the list is {@code null}.
* @return This tenant for command chaining.
*/
@JsonProperty(TenantConstants.FIELD_ADAPTERS)
public TenantObject setAdapterConfigurations(final List<Map<String, Object>> configurations) {
if (configurations == null) {
this.adapterConfigurations = null;
} else {
configurations.stream().forEach(map -> {
final JsonObject config = new JsonObject(map);
addAdapterConfiguration(config);
});
}
return this;
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty in project fdroidclient by f-droid.
the class App method setLocalized.
/**
* Parses the {@code localized} block in the incoming index metadata,
* choosing the best match in terms of locale/language while filling as
* many fields as possible. It first sets up a locale list based on user
* preference and the locales available for this app, then picks the texts
* based on that list. One thing that makes this tricky is that any given
* locale block in the index might not have all the fields. So when filling
* out each value, it needs to go through the whole preference list each time,
* rather than just taking the whole block for a specific locale. This is to
* ensure that there is something to show, as often as possible.
* <p>
* It is still possible that the fields will be loaded directly by Jackson
* without any locale info. This comes from the old-style, inline app metadata
* fields that do not have locale info. They should not be used if the
* {@code localized} block is included in the index. Also, null strings in
* the {@code localized} block should not overwrite Name/Summary/Description
* strings with empty/null if they were set directly by Jackson.
* <p>
* Choosing the locale to use follows two sets of rules, one for Android versions
* older than {@code android-24} and the other for {@code android-24} or newer.
* The system-wide language preference list was added in {@code android-24}.
* <ul>
* <li>{@code >= android-24}<ol>
* <li>the country variant {@code de-AT} from the user locale list
* <li>only the language {@code de} from the above locale
* <li>{@code en-US} since its the most common English for software
* <li>the first available {@code en} locale
* </ol></li>
* <li>{@code < android-24}<ol>
* <li>the country variant from the user locale: {@code de-AT}
* <li>only the language from the above locale: {@code de}
* <li>all available locales with the same language: {@code de-BE}
* <li>{@code en-US} since its the most common English for software
* <li>all available {@code en} locales
* </ol></li>
* </ul>
* On {@code >= android-24}, it is by design that this does not fallback to other
* country-specific locales, e.g. {@code fr-CH} does not fall back on {@code fr-FR}.
* If someone wants to fallback to {@code fr-FR}, they can add it to the system
* language list. There are many cases where it is inappropriate to fallback to a
* different country-specific locale, for example {@code de-DE --> de-CH} or
* {@code zh-CN --> zh-TW}.
* <p>
* On {@code < android-24}, the user can only set a single
* locale with a country as an option, so here it makes sense to try to fallback
* on other country-specific locales, rather than English.
*/
@JsonProperty("localized")
private void setLocalized(Map<String, Map<String, Object>> localized) {
// NOPMD
Locale defaultLocale = Locale.getDefault();
String languageTag = defaultLocale.getLanguage();
String countryTag = defaultLocale.getCountry();
String localeTag;
if (TextUtils.isEmpty(countryTag)) {
localeTag = languageTag;
} else {
localeTag = languageTag + "-" + countryTag;
}
Set<String> availableLocales = localized.keySet();
Set<String> localesToUse = new LinkedHashSet<>();
if (availableLocales.contains(localeTag)) {
localesToUse.add(localeTag);
}
if (availableLocales.contains(languageTag)) {
localesToUse.add(languageTag);
}
if (Build.VERSION.SDK_INT >= 24) {
LocaleList localeList = Resources.getSystem().getConfiguration().getLocales();
String[] sortedLocaleList = localeList.toLanguageTags().split(",");
Arrays.sort(sortedLocaleList, new java.util.Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
});
for (String toUse : sortedLocaleList) {
localesToUse.add(toUse);
for (String l : availableLocales) {
if (l.equals(toUse.split("-")[0])) {
localesToUse.add(l);
break;
}
}
}
} else {
for (String l : availableLocales) {
if (l.startsWith(languageTag)) {
localesToUse.add(l);
}
}
}
if (availableLocales.contains("en-US")) {
localesToUse.add("en-US");
}
for (String l : availableLocales) {
if (l.startsWith("en")) {
localesToUse.add(l);
break;
}
}
whatsNew = getLocalizedEntry(localized, localesToUse, "whatsNew");
String value = getLocalizedEntry(localized, localesToUse, "video");
if (!TextUtils.isEmpty(value)) {
video = value.split("\n", 1)[0];
}
value = getLocalizedEntry(localized, localesToUse, "name");
if (!TextUtils.isEmpty(value)) {
name = value;
}
value = getLocalizedEntry(localized, localesToUse, "summary");
if (!TextUtils.isEmpty(value)) {
summary = value;
}
value = getLocalizedEntry(localized, localesToUse, "description");
if (!TextUtils.isEmpty(value)) {
description = formatDescription(value);
}
featureGraphic = getLocalizedGraphicsEntry(localized, localesToUse, "featureGraphic");
promoGraphic = getLocalizedGraphicsEntry(localized, localesToUse, "promoGraphic");
tvBanner = getLocalizedGraphicsEntry(localized, localesToUse, "tvBanner");
wearScreenshots = getLocalizedListEntry(localized, localesToUse, "wearScreenshots");
phoneScreenshots = getLocalizedListEntry(localized, localesToUse, "phoneScreenshots");
sevenInchScreenshots = getLocalizedListEntry(localized, localesToUse, "sevenInchScreenshots");
tenInchScreenshots = getLocalizedListEntry(localized, localesToUse, "tenInchScreenshots");
tvScreenshots = getLocalizedListEntry(localized, localesToUse, "tvScreenshots");
}
Aggregations