Skip to content

Any.get() method returns assumed values when specified path doesn't exist #99

Closed
@keremulutas

Description

@keremulutas

Currently, Any API returns .get(...) calls with default values if the specified JSON path does not exist. Consider:

String input = '{ "id": 35, "name": "Agatha", "surname": "Christie" }';
Any any = JsonIterator.deserialize(input);
System.out.println(any.get("age").toInt()); --> prints "0"

Instead of returning some default values ("" for String, 0 for int, etc.), do you think it's sane to have the .get method like this?

Any.get(Object defaultValue, Object... keys)

So we could do this:

String input = '{ "id": 35, "name": "Agatha", "surname": "Christie" }';
Any any = JsonIterator.deserialize(input);
System.out.println(any.get(26, "age").toInt()); --> Since "age" key doesn't exist, will print 26

Likewise, the first version would throw JsonException, as .object() and .as(...) methods do.

Thanks for this lovely library by the way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions