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

Add object-based versions of optInt & optFloat #714

Closed
davejbur opened this issue Dec 16, 2022 · 2 comments
Closed

Add object-based versions of optInt & optFloat #714

davejbur opened this issue Dec 16, 2022 · 2 comments

Comments

@davejbur
Copy link

When reading a JSON file, I would like to return a null value if a key is not found. At present, optInt and optFloat only return primitives - so you can't set null as the default.

It's a fairly simple change, to add in the following code to JSONObject:

    /**
     * Get the optional double (obj) value associated with an index. NaN is returned
     * if there is no value for the index, or if the value is not a number and
     * cannot be converted to a number.
     *
     * @param key
     *            A key string.
     * @return The value.
     */
    public Float optFloatObj(String key) {
        return this.optFloatObj(key, Float.NaN);
    }

    /**
     * Get the optional double (obj) value associated with an index. The defaultValue
     * is returned if there is no value for the index, or if the value is not a
     * number and cannot be converted to a number.
     *
     * @param key
     *            A key string.
     * @param defaultValue
     *            The default value.
     * @return The value.
     */
    public Float optFloatObj(String key, Float defaultValue) {
        Number val = this.optNumber(key);
        if (val == null) {
            return defaultValue;
        }
        final Float floatValue = val.floatValue();
        // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
        // return defaultValue;
        // }
        return floatValue;
    }

    /**
     * Get an optional integer value associated with a key, or zero if there is no
     * such key or if the value is not a number. If the value is a string, an
     * attempt will be made to evaluate it as a number.
     *
     * @param key
     *            A key string.
     * @return An object which is the value.
     */
    public Integer optIntegerObj(String key) {
        return this.optIntegerObj(key, 0);
    }

    /**
     * Get an optional integer value associated with a key, or the default if there
     * is no such key or if the value is not a number. If the value is a string,
     * an attempt will be made to evaluate it as a number.
     *
     * @param key
     *            A key string.
     * @param defaultValue
     *            The default.
     * @return An object which is the value.
     */
    public Integer optIntegerObj(String key, Integer defaultValue) {
        final Number val = this.optNumber(key, null);
        if (val == null) {
            return defaultValue;
        }
        return val.intValue();
    }

Unfortunately I've forgotten all the little I learnt about forking/creating branches/submitting pull requests etc, and Github doesn't make it particularly easy unless you're already familiar with how to do it - I've made the changes over on https://github.com/davejbur/JSON-java/tree/add-object-based-versions-int-float but the diff seems to think I've deleted and redone huge chunks instead of just adding four methods!

Perhaps someone could either tell me where I've gone wrong with Github, or just add the code above in to the master anyway.

Thanks!
Dave

@davejbur
Copy link
Author

Being dealt with under #744.

@davejbur
Copy link
Author

Closing this as now dealt with by #753

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

1 participant