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

JSONObject.toString does not use an Enum's toString() but its name() #838

Open
arthurBricq opened this issue Dec 11, 2023 · 7 comments
Open

Comments

@arthurBricq
Copy link

arthurBricq commented Dec 11, 2023

When calling the toString method of a JSONObject, if a field of the object is an enum, the name method is called (when constructing the string) instead of the toString method. This is problematic as name() is not overidable (as it is decalred final) whereas toString is.

I have found where this is in the sources.

Furthermore, it is specified in the java doc of the name function :

Most programmers should use the toString() method in preference to this one, as the toString method may return a more user-friendly name

The fix should be really easy, I can do it if it helps you. Is this an issue not tracked ? I have not found corresponding issues.

Here's a quick way to reproduce this default.

public class TestEnumToString {
    enum MyEnum {
        V_1, V_2;

        @Override
        public String toString() {
            switch (this) {
                case V_1:
                    return "1.0";
                case V_2:
                    return "2.0";
            };
            return "";
        }
    }

    public static void main(String[] args) throws JSONException {
        JSONObject json = new JSONObject();
        json.put("v1", MyEnum.V_1);
        json.put("v2", MyEnum.V_2);
        System.out.println(json.toString());
    }
}

Thanks in advance,
Arthur

@ThestralWarrior
Copy link

Hi can I work on this?

@stleary
Copy link
Owner

stleary commented Dec 14, 2023

@ThestralWarrior Sure, there are no restrictions on who gets to work on tickets. I don't know of anyone else working on this, so feel free to get started. Please keep in mind that changes to existing behavior are disallowed, unless there is a compelling reason for the change.

ThestralWarrior added a commit to ThestralWarrior/JSON-java that referenced this issue Dec 14, 2023
…ridden toString() method for enum values instead of name().
@johnjaylward
Copy link
Contributor

This is not a bug and was the intended way for enums to be serialized and deserialised by this library. This would be a major breaking change for anyone who is using the enum feature

@ThestralWarrior
Copy link

I understand. It's my first time trying to make an open source contribution. I could use some guidance!

@stleary
Copy link
Owner

stleary commented Dec 15, 2023

@ThestralWarrior No worries, this was an unusual issue since the obvious solution was not right for this project. There will be other opportunities to contribute.

@robross0606
Copy link

robross0606 commented Nov 11, 2024

This is not a bug and was the intended way for enums to be serialized and deserialised by this library. This would be a major breaking change for anyone who is using the enum feature

What is the workaround for this problem? JSONObject is treating Enum differently than any other class. If a user wants to serialize and deserialize an enum value using something other than name, there should be a way to do that. If it isn't by overriding toString() then what is it?

I see https://github.com/stleary/JSON-java/wiki/Tech:-How-to-solve-specific-problems-in-your-JSON-Java-code which talks about implementing JSONString. This appears to work one direction by implementing the toJSONString() function. What about the reverse? Is there an interface for fromJSONString()?

@stleary
Copy link
Owner

stleary commented Nov 12, 2024

@robross0606 This is how the library was designed; there is no workaround. It may be possible for a future release to provide an option for alternative handling using a JSONParserConfiguration object and toString(), , but the support for that is not in place yet.

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

No branches or pull requests

5 participants