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

Accurately classifies all int values by size, rather than conservatively classifying all 4-byte ints as Java longs. #627

Merged
merged 1 commit into from
Nov 3, 2023

Conversation

tgregg
Copy link
Contributor

@tgregg tgregg commented Oct 27, 2023

Description of changes:

Before this change, the new reader implementation classified all Ion int values that consumed 4 binary Ion UInt bytes as Java long. This was conservative, ensuring that IonReader.getIntegerSize() would always return a Java type at least as large as necessary to hold the value (but sometimes larger). The goal was to simplify the implementation of getIntegerSize.

However, some applications depend on int values being classified accurately. Typically this is the case when an object mapping library like Jackson is used on top of ion-java for round-tripping values from Java classes. When a class is defined with a capital-I Integer field and that field is round-tripped as a Long, raw casts to Integer will fail. Additionally, unit tests that simply assertEquals the expected Integer and the actual Long will fail due to the type difference, even if the values are numerically equivalent.

In order to avoid imposing pain on existing users, this PR proposes to accurately classify all int values, such that getIntegerSize always returns the smallest Java type necessary to hold the value without data loss. It turns out that doing this is not significantly more complicated, and has negligible performance impact. It should also be noted that IonReader.getIntegerSize() is not a commonly-used method; most applications will implicitly know the maximum size of int value that may exist in the data and simply call through to the corresponding IonReader value method directly.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@@ -143,10 +143,10 @@ private void testGetIntegerSizeIntBoundary(int boundaryValue, long pastBoundary)
{
in.next();
// It's fine if IntegerSize recommends a larger-than-necessary type, just not a smaller one.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// It's fine if IntegerSize recommends a larger-than-necessary type, just not a smaller one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

…ely classifying all 4-byte ints as Java longs.
@tgregg tgregg force-pushed the accurate-integer-size branch from 00a49fc to 8d744c6 Compare October 30, 2023 19:31
@tgregg tgregg merged commit 390c13e into empty-bais Nov 3, 2023
@tgregg tgregg deleted the accurate-integer-size branch November 3, 2023 00:33
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.

3 participants