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

Why length fuction can not use in String? #1026

Open
cyrly97 opened this issue Nov 20, 2024 · 1 comment
Open

Why length fuction can not use in String? #1026

cyrly97 opened this issue Nov 20, 2024 · 1 comment

Comments

@cyrly97
Copy link

cyrly97 commented Nov 20, 2024

Length function can not calculate String's length.
but in AbstractJsonProvider class can calculate String's path.
Why?
image

@abody2k
Copy link

abody2k commented Nov 22, 2024

I just checked it out, the length method has been overridden in some of the classes without providing implementation for String length like GsonJsonProvider.class

 @Override
    public int length(final Object obj) {
        if (isArray(obj)) {
            return toJsonArray(obj).size();
        } else if (isMap(obj)) {
            return toJsonObject(obj).entrySet().size();
        } else {
            if (obj instanceof JsonElement) {
                JsonElement element = toJsonElement(obj);
                if (element.isJsonPrimitive()) {
                    return element.toString().length();
                }
            }
        }
        throw new JsonPathException("length operation can not applied to " + (obj != null ? obj.getClass().getName()
                                                                                         : "null"));
    }

and it totally depends on which file you are using, for instance these classes does implement it supporting the String length.

  • JsonOrgJsonProvider implements it providing the String length.
  • JsonSmartJsonProvider (this one does not override it).
  • JettisonProvider implements the String length

this is the path where you can find the other implementations for this Abstract Class

json-path\src\main\java\com\jayway\jsonpath\spi\json

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