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

Merge walker class into writer #447

Merged
merged 1 commit into from
Dec 24, 2024
Merged

Merge walker class into writer #447

merged 1 commit into from
Dec 24, 2024

Conversation

jodastephen
Copy link
Member

@jodastephen jodastephen commented Dec 20, 2024

  • No need for a separate class anymore

Summary by CodeRabbit

  • New Features

    • Enhanced JSON serialization for Joda-Beans, improving handling of various data types, including arrays and collections.
    • Introduced a new interface for JSON handlers to streamline the serialization process.
  • Bug Fixes

    • Removed deprecated class that was previously responsible for JSON serialization.
  • Refactor

    • Updated method signatures and refactored logic for writing beans to improve efficiency and clarity.

* No need for a separate class anymore
Copy link

coderabbitai bot commented Dec 20, 2024

📝 Walkthrough

Walkthrough

The pull request involves significant changes to the JSON serialization mechanism for Joda-Beans. The JodaBeanSimpleJsonWalker class has been completely removed, and the JodaBeanSimpleJsonWriter class has been extensively refactored. The modifications introduce a more flexible and efficient approach to serializing objects, with enhanced support for various data types, including arrays, collections, and optional values. The changes include adding new methods for handling different object types and introducing a JsonHandler interface to manage serialization processes.

Changes

File Change Summary
src/main/java/org/joda/beans/ser/json/JodaBeanSimpleJsonWalker.java Entire class deleted
src/main/java/org/joda/beans/ser/json/JodaBeanSimpleJsonWriter.java - Constructor updated to accept JodaBeanSer settings instead of JodaBeanSimpleJsonWalker
- Added multiple new methods: writeObject, writeBean, writeArray, writePrimitiveArray, writeJodaConvert
- Introduced JsonHandler interface and several handler implementations
- Added static ClassValue for efficient handler lookup

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@jodastephen

This comment was marked as outdated.

Copy link

coderabbitai bot commented Dec 20, 2024

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
src/main/java/org/joda/beans/ser/json/JodaBeanSimpleJsonWriter.java (4)

19-25: Encourage the use of var and handle potential reflection concerns.

Good to see that these imports are arranged logically. In Java 21, favour using 'var' where it can simplify code without losing clarity. Additionally, reflectively handling arrays always has a potential risk of difficulty in maintenance. Here, everything seems correct, but ensure that accidental type confusion or class-loading constraints do not arise, especially if reflection logic becomes more complex in the future.


66-82: Leverage pattern matching for class checks (if desired).

Java 21 supports pattern matching for instanceof, which safely handles null. For instance:
if (value instanceof Bean beanValue) { … }
However, since your approach relies on ClassValue and an explicit hierarchy of condition checks, this is acceptable and works efficiently for your scenario. No changes are strictly required.


162-165: Final new line approach for JSON output.

Appending the newline after serialisation is often nice for readability. Ensure that, in nested usage or streaming scenarios, this behaviour does not introduce extraneous blank lines. Currently, it appears safe, but consider making it configurable in the future.


426-442: Multiset serialisation approach.

Writing a multiset as nested arrays for [element, count] is clear. Just be mindful of the length of output for massive sets. This should be documented for consumers who rely on performance.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 459819b and 3aa4b27.

📒 Files selected for processing (2)
  • src/main/java/org/joda/beans/ser/json/JodaBeanSimpleJsonWalker.java (0 hunks)
  • src/main/java/org/joda/beans/ser/json/JodaBeanSimpleJsonWriter.java (4 hunks)
💤 Files with no reviewable changes (1)
  • src/main/java/org/joda/beans/ser/json/JodaBeanSimpleJsonWalker.java
🧰 Additional context used
📓 Path-based instructions (1)
src/main/java/org/joda/beans/ser/json/JodaBeanSimpleJsonWriter.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/main/java/**/*.java: - This project is mature and must provide a stable backwards-compatible public Java API.

  • In the 'Walkthrough' section, you must always provide a list of up to 25 changes to the public Java API that will affect end users.
    If there are no changes, you must explicitly state that there are no changes to the public Java API in this PR.
    The public Java API is defined as public and protected methods on public classes, plus the file module-info.java.
    Provide the list by deeply analysing code flow, which incudes analysing code flow through private methods and calls to Guava and Java 21.
    Changes to be reported on include:
    • New or removed methods in the public Java API
    • Changes to method return types or parameter types in the public Java API
    • Changes to method behaviour in the public Java API that might affect consumers
  • This project uses System.out.println instead of logging
  • This project tends to prefer for loops to streams for performance reasons, however either form is acceptable.
    Do not make suggestions to change between streams and for loops or vice versa.
🔇 Additional comments (10)
src/main/java/org/joda/beans/ser/json/JodaBeanSimpleJsonWriter.java (10)

63-65: Observe O(1) approach correctness.

Using ClassValue is a neat approach for O(1) lookups. This is indeed a recommended Java approach to caching class-based data rather than relying on comparatively slower reflection-based solutions or maps. Ensure correct synchronisation with the rest of your pipeline if concurrency is introduced.


117-118: Validate immutability usage and var.

Fields 'settings' is properly declared final. Given that it remains unchanged and is not shared across threads, this ensures safe publication. Also check if the 'output' field might ever require re-entrancy. Currently, it is replaced each time you call 'write', so concurrency is out of scope. This is consistent.


170-176: Potential short-circuiting for null checks.

Your approach to handle null with output.writeNull() is correct. The subsequent block obtains a handler from LOOKUP. Since the code is straightforward, there is no immediate improvement required for clarity or performance.


179-188: Joda-Convert usage can throw runtime exceptions.

Ensure that the approach to handle convertible beans is safe. The fallback to writing properties individually is sensible. The try-catch block for handling conversion errors is present elsewhere. Maintain consistent error handling for all potential conversions.


262-279: Interface naming and scope.

'JsonHandler' is declared private, so it's intentionally not part of your public API. Fine for encapsulation. The default method for handleProperty() also helps keep property handling consistent. This design effectively centralises logic.


300-311: Map and Collection detection approach.

Using a chain of if-statements here is pragmatic, especially with ClassValue caching. Suggestions to adopt pattern matching wouldn't necessarily improve clarity. This code block is well-structured.


354-380: Map writing logic and null key guard.

This logic for writing map keys is correct by forcing keys to be non-null. The error message clarifies the failure. The usage of a dedicated converter for keys is also well thought out, ensuring type safety.


482-491: Hierarchy-based solution for Grid support.

Introducing a separate 'CollectJsonHandlers' build on top of 'GuavaJsonHandlers' is consistent with the sealed class approach. No complexities are evident for future expansions.


522-552: Implementation for OptionalJsonHandler is correct.

Handling optional values with an orElse(null) approach is fine, ignoring them if empty. This aligns with typical JSON omission patterns. Check that consumers do not require explicit "null" writes for empty Optionals.


555-585: Guava’s Optional handling is analogous.

Same logic for 'com.google.common.base.Optional' as standard Optional. It’s helpful for bridging older code still using Guava utilities.

@jodastephen jodastephen merged commit a9a22d6 into main Dec 24, 2024
5 checks passed
@jodastephen jodastephen deleted the simplify-json-classes branch December 24, 2024 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant