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-core] Add JsonExtract helper to ease extracting values and number types from raw Map<String,Object> #325

Merged
merged 2 commits into from
Jan 8, 2025

Conversation

rbygrave
Copy link
Contributor

@rbygrave rbygrave commented Jan 8, 2025

Helps mostly when extracting values out of nested documents, and dealing with the various number types and type conversion

Example:

  String json = "{\"name\":\"Rob\",\"score\":4.5,\"whenActive\":\"2025-10-20\",\"address\":{\"street\":\"Pall Mall\"}}";
  Map<String, Object> mapFromJson = simpleMapper.fromJsonObject(json);

  JsonExtract extract = simpleMapper.extract(mapFromJson);

  String name = extract.extract("name");
  double score = extract.extract("score", -1D);
  String street = extract.extract("address.street");

  LocalDate activeDate = extract.extractOrEmpty("whenActive")
    .map(LocalDate::parse)
    .orElseThrow();

…er types from raw Map<String,Object>

Helps mostly when extracting values out of nested documents, and dealing with the various number types and type conversion
@rbygrave rbygrave requested a review from SentryMan January 8, 2025 08:46
@rbygrave rbygrave self-assigned this Jan 8, 2025
@rbygrave rbygrave merged commit 287d442 into main Jan 8, 2025
9 checks passed
@rbygrave rbygrave deleted the feature/core-JsonExtract branch January 8, 2025 11:13
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.

1 participant