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

Fix numeric conversion to Long when serializing numbers as JsonElement tree #2814

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Marcono1234
Copy link
Collaborator

@Marcono1234 Marcono1234 commented Feb 22, 2025

Purpose

Fixes #2680

Description

As mentioned in #2680, an unintended side-effect of 3e3266c was that serializing to a JsonElement tree is now converting all integral numbers to Long instances.

This pull request here tries to solve this by using the JsonWriter#value(Number) overload again, boxing the converted number. If the type is already matching (e.g. serializing a Byte as Byte) then this also has the advantage that it avoids unwrapping and having JsonTreeWriter wrap the value again.
But on the other hand when using JsonWriter and not JsonTreeWriter there might be a bit overhead from JsonWriter#value(Number) compared to JsonWriter#value(long).

An alternative to this PR could be to instead revert 3e3266c partially or completely. After all the underlying issue #2156 had been reported by me (and was not blocking me in any way), so maybe other users would not have cared about the missing numeric conversion.

Checklist

  • New code follows the Google Java Style Guide
    This is automatically checked by mvn verify, but can also be checked on its own using mvn spotless:check.
    Style violations can be fixed using mvn spotless:apply; this can be done in a separate commit to verify that it did not cause undesired changes.
  • If necessary, new public API validates arguments, for example rejects null
  • New public API has Javadoc
    • Javadoc uses @since $next-version$
      ($next-version$ is a special placeholder which is automatically replaced during release)
  • If necessary, new unit tests have been added
    • Assertions in unit tests use Truth, see existing tests
    • No JUnit 3 features are used (such as extending class TestCase)
    • If this pull request fixes a bug, a new test was added for a situation which failed previously and is now fixed
  • mvn clean verify javadoc:jar passes without errors

@Marcono1234 Marcono1234 reopened this Feb 22, 2025
@Marcono1234
Copy link
Collaborator Author

Closed and reopend this PR to fix an intermittent test failure, see #2815.

Comment on lines +377 to 381
} else if (value instanceof Long) {
out.value(value);
} else {
// Only perform unwrapping if numeric conversion is necessary
out.value(value.longValue());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For correctness this value instanceof Long check is not needed; the previous code which always called out.value(value.longValue()) was fine as well. However, for JsonTreeWriter it would have unwrapped and then wrapped the value again. But on the other hand for JsonWriter value(long) might be more efficient. I am not really sure which approach is better for performance, or if it even matters at all.

(same applies to Double below)

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.

TypeAdapterFactory INTEGER_FACTORY will change INTEGER type to LONG type
1 participant