Skip to content

add asShort(), asFloat() conversion methods with tests #5138

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

Merged
merged 9 commits into from
May 8, 2025

Conversation

hannyu
Copy link

@hannyu hannyu commented May 3, 2025

feat: complete type conversion support

  • Add conversion methods: asShort(), asFloat()...etc. for all non-container nodes.
  • Include test coverage for all new methods.
  • Add range check in POJONode.
  • Some minor fixes.

@cowtowncoder cowtowncoder added the 3.0 Issue planned for initial 3.0 release label May 7, 2025
@@ -111,11 +111,6 @@ protected String _asString() {
return _value ? "true" : "false";
}

@Override
Copy link
Member

Choose a reason for hiding this comment

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

Good catch!

@@ -55,6 +60,9 @@ public final boolean canConvertToExactIntegral() {
@Override
public final short shortValue() {
if (!_inShortRange()) {
if (isNaN()) {
Copy link
Member

Choose a reason for hiding this comment

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

Also: good catch!

Copy link
Member

@cowtowncoder cowtowncoder left a comment

Choose a reason for hiding this comment

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

Very good! Approved as-is.

@cowtowncoder cowtowncoder added this to the 3.0.0-rc4 milestone May 8, 2025
@cowtowncoder cowtowncoder merged commit 11ecff1 into FasterXML:3.x May 8, 2025
6 checks passed
@cowtowncoder
Copy link
Member

Excellent, thank you @hannyu!

If you want, it'd be possible to, as follow-up:

  1. Improve error handling of POJONode wrt number coercion fails wrt Range checks (right now I think will throw exception for "non-numeric" values even if it's range check fail)
  2. Add "optional" variants -- shortValueOpt(), floatValueOpt(), asShortOpt(), asFloatOpt()

but this is very good addition already.

@cowtowncoder cowtowncoder changed the title add asShort(), asFloat() conversion methods with tests add asShort(), asFloat() conversion methods with tests May 8, 2025
cowtowncoder added a commit that referenced this pull request May 8, 2025
@hannyu
Copy link
Author

hannyu commented May 9, 2025

@cowtowncoder Very glad to contribute!

Here’s what I think:

  1. Got it. I’ll give it a shot.
  2. I had already noticed the missing of shortValueOpt() and floatValueOpt(). Also, it’s strange that the JDK has OptionalInt and OptionalDouble, but no OptionalShort or OptionalFloat. So, should we just go with Optional<Short>?

@cowtowncoder
Copy link
Member

Right, for Optional- and Atomic- there's just Int/Long/Double by JDK. So yes, Optional<Float> and Optional<Short> are needed unfortunately.

Thank you very much again for working on these!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.0 Issue planned for initial 3.0 release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants