/** * Search {@link JsonbNumberFormat} on property, if not found looks at annotations declared on property type class. * * @param property Property to search on. * @return Map of {@link JsonbNumberFormatter} instances categorized by their scopes (class, property, getter or setter). If there is no number * formatter specified for given property, an empty map would be returned */ public Map<AnnotationTarget, JsonbNumberFormatter> getJsonNumberFormatter(Property property) { Map<AnnotationTarget, JsonbNumberFormatter> result = new HashMap<>(); Map<AnnotationTarget, JsonbNumberFormat> annotationFromPropertyCategorized = getAnnotationFromPropertyCategorized(JsonbNumberFormat.class, property); if(annotationFromPropertyCategorized.size() == 0) { final Optional<Class<?>> propertyRawTypeOptional = ReflectionUtils.getOptionalRawType(property.getPropertyType()); if (propertyRawTypeOptional.isPresent()) { Class<?> rawType = propertyRawTypeOptional.get(); if (!Number.class.isAssignableFrom(rawType)) { return new HashMap<>(); } } } else { annotationFromPropertyCategorized.forEach((key, annotation) -> result.put(key, new JsonbNumberFormatter(annotation.value(), annotation.locale()))); } JsonbNumberFormat classLevelNumberFormatter = findAnnotation(property.getDeclaringClassElement().getAnnotations(), JsonbNumberFormat.class); if(classLevelNumberFormatter != null) { result.put(AnnotationTarget.CLASS, new JsonbNumberFormatter(classLevelNumberFormatter.value(), classLevelNumberFormatter.locale())); } return result; }
/** * Search for {@link JsonbNumberFormat} annotation on java class. * * @param clazzElement class to search not null * @return formatter to use */ public JsonbNumberFormatter getJsonbNumberFormat(JsonbAnnotatedElement<Class<?>> clazzElement) { final JsonbNumberFormat formatAnnotation = findAnnotation(clazzElement.getAnnotations(), JsonbNumberFormat.class); if (formatAnnotation == null) { return null; } return new JsonbNumberFormatter(formatAnnotation.value(), formatAnnotation.locale()); }
public JsonbNumberFormatter getConstructorNumberFormatter(JsonbAnnotatedElement<Parameter> param) { JsonbNumberFormat annotation = param.getAnnotation(JsonbNumberFormat.class); if (annotation != null) { return new JsonbNumberFormatter(annotation.value(), annotation.locale()); } return null; }
public JsonbNumberConverter(final JsonbNumberFormat numberFormat) { final String locale = numberFormat.locale(); final String format = numberFormat.value(); final boolean customLocale = !JsonbNumberFormat.DEFAULT_LOCALE.equals(locale); if (format.isEmpty() && customLocale) { delegateFactory = () -> NumberFormat.getInstance(newLocale(locale)); } else if (format.isEmpty()) { delegateFactory = NumberFormat::getInstance; } else if (customLocale) { delegateFactory = () -> new DecimalFormat(format, DecimalFormatSymbols.getInstance(newLocale(locale))); } else { delegateFactory = () -> new DecimalFormat(format); } }
private void validateAnnotations(final Object parameter, final JsonbTypeAdapter adapter, final JsonbDateFormat dateFormat, final JsonbNumberFormat numberFormat, final JohnzonConverter johnzonConverter) { int notNull = adapter != null ? 1 : 0; notNull += dateFormat != null ? 1 : 0; notNull += numberFormat != null ? 1 : 0; notNull += johnzonConverter != null ? 1 : 0; if (notNull > 1) { throw new IllegalArgumentException("Conflicting @JsonbXXX/@JohnzonConverter on " + parameter); } }
private Adapter<?, ?> toConverter(final Type type, final JsonbTypeAdapter adapter, final JsonbDateFormat dateFormat, final JsonbNumberFormat numberFormat) throws InstantiationException, IllegalAccessException { final Adapter converter; if (adapter != null) { final Class<? extends JsonbAdapter> value = adapter.value(); final ParameterizedType pt = findPt(value, JsonbAdapter.class); if (pt == null) { throw new IllegalArgumentException(value + " doesn't implement JsonbAdapter"); } final JohnzonAdapterFactory.Instance<? extends JsonbAdapter> instance = newInstance(value); toRelease.add(instance); final Type[] actualTypeArguments = pt.getActualTypeArguments(); converter = new JohnzonJsonbAdapter(instance.getValue(), actualTypeArguments[0], actualTypeArguments[1]); } else if (dateFormat != null) { // TODO: support lists, LocalDate? if (Date.class == type) { converter = new ConverterAdapter<>(new JsonbDateConverter(dateFormat)); } else if (LocalDateTime.class == type) { converter = new ConverterAdapter<>(new JsonbLocalDateTimeConverter(dateFormat)); } else if (LocalDate.class == type) { converter = new ConverterAdapter<>(new JsonbLocalDateConverter(dateFormat)); } else if (ZonedDateTime.class == type) { converter = new ConverterAdapter<>(new JsonbZonedDateTimeConverter(dateFormat)); } else { throw new IllegalArgumentException(type + " not a supported date type"); } } else if (numberFormat != null) { // TODO: support lists? converter = new ConverterAdapter<>(new JsonbNumberConverter(numberFormat)); } else { converter = new ConverterAdapter<>(new JsonbValueConverter()); } return converter; }
@JsonbNumberFormat("000.00000000") public Double getDoubleGetterFormatted() { return doubleGetterFormatted; }
@JsonbNumberFormat(value = "000,000", locale = "da-da") public void setDoubleSetterFormatted(Double doubleSetterFormatted) { this.doubleSetterFormatted = doubleSetterFormatted; }
@JsonbNumberFormat(value = "000,000", locale = "da-da") public void setDoubleSetterAndPropertyFormatter(Double doubleSetterAndPropertyFormatter) { this.doubleSetterAndPropertyFormatter = doubleSetterAndPropertyFormatter; }
@JsonbCreator public static FactoryNumberParam createInstance( @JsonbProperty("number") @JsonbNumberFormat(value = "000.000", locale = "en-us") BigDecimal number) { return new FactoryNumberParam(number); }