Giter VIP home page Giter VIP logo

Comments (2)

Marcono1234 avatar Marcono1234 commented on August 25, 2024 1

Yes can confirm this; it looks like this was indeed introduced by 3e3266c (by accident):
There is only JsonWriter.value(long), so the adapters for all other integral primitive types (byte, short, int) actually call value(long).

Sorry for that. Maybe it would make sense to (partially) revert that commit.

It seems AtomicInteger is affected as well, but it's adapter always behaved this way, that a Long instead of the AtomicInteger is stored in the JsonPrimitive. However, this might be a good thing since it is mutable and might cause confusing behavior if it was stored in JsonPrimitive.

from gson.

Marcono1234 avatar Marcono1234 commented on August 25, 2024

Ok, so the potential issues with the current implementation are:

  • Conversion to Long might be unexpected (especially when implicitly using JsonTreeWriter as described in your issue here)
    Might depend on the use case whether that is a problem for user code.
  • Conversion to Long instead of keeping for example the Byte or Integer might take up more memory
    This might be especially noticeable when you have a lot of JsonPrimitives containing them. But maybe that also depends on the JVM object layout for these boxed objects. Maybe a Long is not actually that much larger than an Integer, despite long being larger than int (?), but I haven't investigated that yet.

Reverting 3e3266c (or at least parts of it) would also revert narrowing numeric conversion, e.g. 1.5f -> (byte) 1, see #2156. Though that was originally reported by me, and I am not sure if / how many users were actually negatively affected by that behavior.
That could be solved again by performing the narrowing conversion, but then boxing as Number again and then call JsonWriter#value(Number) instead of JsonWriter#value(long), but performance-wise this will likely not be ideal.

Maybe JsonWriter#value(Number) could also be refactored to first check using a new isTrustedIntegralNumberType, to avoid redundant string.equals("-Infinity") checks in these cases. Though it would have to be verified if that actually makes a noticeable performance difference.

So am I currently not sure what the best approach here would be.
@eamonnmcmanus, what do you think?

from gson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.