Search in sources :

Example 1 with JsonbNumberFormat

use of javax.json.bind.annotation.JsonbNumberFormat in project smallrye-graphql by smallrye.

the class JsonStringReader method formattedNumber.

private Number formattedNumber(String input) {
    String locale = null;
    String format = null;
    JsonbNumberFormat jsonbNumberFormat = field.getAnnotation(JsonbNumberFormat.class);
    if (jsonbNumberFormat != null) {
        locale = jsonbNumberFormat.locale();
        format = jsonbNumberFormat.value();
    }
    NumberFormat numberFormat = field.getAnnotation(NumberFormat.class);
    if (numberFormat != null) {
        locale = numberFormat.locale();
        format = numberFormat.value();
    }
    java.text.NumberFormat nf;
    if (format != null && !format.isEmpty()) {
        nf = new DecimalFormat(format);
    } else if (locale != null && !locale.isEmpty()) {
        nf = java.text.NumberFormat.getInstance(Locale.forLanguageTag(locale));
    } else {
        nf = new DecimalFormat();
    }
    try {
        return nf.parse(input);
    } catch (ParseException e) {
        throw new InvalidResponseException("Can't parse number", e);
    }
}
Also used : JsonbNumberFormat(javax.json.bind.annotation.JsonbNumberFormat) DecimalFormat(java.text.DecimalFormat) JsonString(javax.json.JsonString) ParseException(java.text.ParseException) InvalidResponseException(io.smallrye.graphql.client.InvalidResponseException) NumberFormat(org.eclipse.microprofile.graphql.NumberFormat) JsonbNumberFormat(javax.json.bind.annotation.JsonbNumberFormat)

Aggregations

InvalidResponseException (io.smallrye.graphql.client.InvalidResponseException)1 DecimalFormat (java.text.DecimalFormat)1 ParseException (java.text.ParseException)1 JsonString (javax.json.JsonString)1 JsonbNumberFormat (javax.json.bind.annotation.JsonbNumberFormat)1 NumberFormat (org.eclipse.microprofile.graphql.NumberFormat)1