Skip to content

Issues with long values near max/min #26

@wvdvegt

Description

@wvdvegt

Hi

I changed the code of ParseElement a bit so it has some checks on long values near their min/max (see https://wvdvegt.wordpress.com/2018/04/26/double-precision-issues/ for details on the issue):

        private static JSONNode ParseElement(string token, bool quoted)
        {
            ...
            double val;
            if (double.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, out val))
                return val;
            else
                return token;
        }

into

        private static JSONNode ParseElement(string token, bool quoted)
        {
            ...

            //! Patched start.
            Boolean isLong = (longAsString && long.TryParse(token, out long lval));

            if (double.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, out double rval))
            {
                if (isLong && !rval.ToString("F0").Equals(token))
                    return token;
                else
                    return rval;
            }
            else
                return token;
            //! Patched end.
        }

Fix can probably optimized a bit by mainly using the rval.ToString("F0").Equals(token) check

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions