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

[json-node] Add extract() methods to assist filtering and mapping #300

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

rbygrave
Copy link
Contributor

    JsonArray phoneNumbers = ...;

    List<String> result =
      phoneNumbers.stream()
        .filter(n -> "home".equals(n.extract("type")))
        .map(n -> n.extract("number"))
        .collect(Collectors.toList());

extract() can take a dot notation path like person.name (so not JSONPath)

    var node = JsonObject.create()
      .add("person", JsonObject.create().add("name", "myName").add("type", "doo").add("active", true))
      .add("address", JsonObject.create().add("size", 42).add("junk", 99L).add("other", JsonObject.create().add("deep", "one")));

    JsonNode name = node.find("name");
    assertThat(name).isNull();

    JsonNode personName = node.find("person.name");
    assertThat(personName).isNotNull();
    assertThat(personName.text()).isEqualTo("myName");

    assertThat(node.extract("person.name")).isEqualTo("myName");
    assertThat(node.extract("person.active")).isEqualTo("true");
    assertThat(node.extract("person.active", false)).isEqualTo(true);
    assertThat(node.extract("person.missing", false)).isEqualTo(false);

    assertThat(node.extract("address.size", -1)).isEqualTo(42);

    assertThat(node.extract("address.other.deep")).isEqualTo("one");

@rbygrave rbygrave self-assigned this Dec 12, 2024
@rbygrave rbygrave added this to the 3.0 milestone Dec 12, 2024
@rbygrave rbygrave merged commit 212f113 into main Dec 12, 2024
9 checks passed
@rbygrave rbygrave deleted the feature/json-node-extract branch December 12, 2024 02:01
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

Successfully merging this pull request may close these issues.

2 participants