Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing integer format with locale #2

Open
SealTecH opened this issue Feb 23, 2019 · 1 comment
Open

Parsing integer format with locale #2

SealTecH opened this issue Feb 23, 2019 · 1 comment

Comments

@SealTecH
Copy link

SealTecH commented Feb 23, 2019

I tried to parse an integer value, that had a comma delimiter, like '1,234' and faced a problem with conversion with locale.

fr.whimtrip.ext.jwhtscrapper.exception.ModelBindingException: Scrap failed with exception Cannot parse field fiveStarsCount.class com.service.parser.pojo.ProductPojo with value 1,626 and locale: us.
	at fr.whimtrip.ext.jwhthtmltopojo.adapter.AbstractHtmlFieldImpl.instanceForNode(AbstractHtmlFieldImpl.java:201) ~[whimtrip-ext-htmltopojo-1.0.2.jar:na]

So, we need to respect locale while conversion number values, because currently an example like this

@Selector(
            value = ".example ",
            locale = 'US',
            returnDefValueOnThrow = false
    )
    public Integer fiveStarsCount;

can't be correctly casted. To use locale during number conversion we need to use method like this
Number number = NumberFormat.getIntegerInstance(Locale.US).parse('123,45');

This is default realiztaion of conversion at castValue in HtmlToPojoUtils


 if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
            if(value.equals(Selector.NO_VALUE))
                return (U) DEFAULT_NOT_FOUND_INTEGER;
            return (U) Integer.valueOf(value);
        }

My question is - can we solve this problem without using custom deserializer? Because currently this problem is in each of hundreds of Integer fields

@Louis-wht
Copy link
Contributor

Hello!

Sorry for the very late reply, I'm not using this account very often now...
I think the issue is because you should use a Float here because Integer.valueOf("1,65") would fail anyway.
I think I should have added the locale to those numbers parsing as well, unfortunately, I didn't so a the moment, there is no quick fix...
Or probably the solution you highlighted would have been more resilient...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants