Conversation
cchantep
force-pushed
the
feature/1121_number
branch
5 times, most recently
from
September 13, 2026 20:32
6bb28c0 to
db3166a
Compare
cchantep
commented
Sep 13, 2026
|
|
||
| debug(showCode(generated)) | ||
|
|
||
| c.Expr[Reads[T]](c.typeCheck(generated)) |
| override def serialize(value: JsValue, json: JsonGenerator, provider: SerializerProvider): Unit = { | ||
| value match { | ||
| case JsNumber(v) => { | ||
| case n: JsNumber.JsLazy => |
| "num0.5" in assertSizes("""0.5""", 80, 80) | ||
| "numLongMax" in assertSizes(Long.MaxValue.toString, 144, 144) | ||
| "numDoubleMax" in assertSizes(Double.MaxValue.toString, 144, 144) | ||
| "num0" in assertSizes("""0""", 32, 80) |
Member
Author
There was a problem hiding this comment.
Update according to size changes due to specific implementation classes
|
|
||
| parsed.mustEqual(json) | ||
|
|
||
| json.mustEqual(parsed) |
| val exceedsScaleLimit = BigDecimal(2, parserSettings.bigDecimalParseConfig.scaleLimit + 1) | ||
| Json | ||
| .parse(bigNumbersJson(bigDec = exceedsScaleLimit.toString)) | ||
| .as[BigNumbers] |
Member
Author
There was a problem hiding this comment.
Exception thrown as soon as .parse
| object JsonSpec { | ||
| val exceedsDigitsLimit: BigDecimal = BigDecimal("9" * 1000000) | ||
| lazy val exceedsDigitsLimit: BigDecimal = { | ||
| // BigDecimal("9" * 1000000) |
Member
Author
There was a problem hiding this comment.
Such instantiation was really too slow
|
|
||
| package play.api.libs.json | ||
|
|
||
| private[json] trait JsNumberExtractors { self: JsNumber.type => |
Member
Author
There was a problem hiding this comment.
Extractors used by Reads according JsNumber implementations
| * Represent a Json number value. | ||
| */ | ||
| case class JsNumber(value: BigDecimal) extends JsValue | ||
| sealed trait JsNumber extends JsValue with Serializable { |
Member
Author
There was a problem hiding this comment.
Refactor as sealed family
| /** | ||
| * Serializer for Java BigDecimal types. | ||
| */ | ||
| implicit object JavaBigDecimalWrites extends Writes[java.math.BigDecimal] { |
cchantep
marked this pull request as ready for review
September 13, 2026 20:43
cchantep
force-pushed
the
feature/1121_number
branch
3 times, most recently
from
September 15, 2026 14:43
a5d054e to
58953ab
Compare
Member
Author
|
Note that if update to Jackson 3, the That would possibly make the |
cchantep
force-pushed
the
feature/1121_number
branch
17 times, most recently
from
September 16, 2026 16:40
077f285 to
defaa3c
Compare
cchantep
force-pushed
the
feature/1121_number
branch
2 times, most recently
from
September 16, 2026 17:54
70b83d3 to
07cb4f4
Compare
cchantep
force-pushed
the
feature/1121_number
branch
from
September 16, 2026 18:16
07cb4f4 to
b7f3f66
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Checklist
Purpose
Improve performance about the JSON numbers for the following use cases:
Background Context
The 2 main points of the design are described below.
JsNumber.value) or validated (usingReads), a deserializedJsNumberonly kept the raw representation from the JSON parser, without more processing/conversion.JsNumberas a sealed family with different specifialized implementation class according to use cases and underlying value precision/representation, allow to implement feature efficiently according to each one.All the implementations,
equals/hashCode),JsNumberis visible in the API, to stay compatible.Benchmarks
New benchmarks have been added in
benchmarks/src/main/scala/play/api/libs/json/JsonNumberBench.scalato measure and compare the performance of different kinds of JSON numbers across the following use cases.Parse
Basically
Json.parse(x).asInstanceOf[JsNumber], wherexis a JSON raw number (integer, float, double, big decimal, ...).Eval parsed
Accessing the
value: BigDecimalfield of a parsedJsNumber; basicallyJson.parse(x).asInstanceOf[JsNumber].value.Instantiation
The
JsNumber(..)factories, creatingJsNumberinstances using the specialized implementation classes.Validation
From JSON parsing to
Reads, basicallyJson.parse(..).as[Int/Float/Double/BigDecimal/..].Write
Using
Writesmatching the specialized implementations; basicallyJson.toJson(..).Serialization
Basically
Json.toJson(aJsNumber).Overall
The specialized
JsNumberimplementations provide noticeable performance improvements for several numeric types, particularlyByte,Short,Int,LongandSmallInteger. The other numeric representations remain broadly stable, with some trade-offs depending on the operation and representation.The benchmarks show improvements across most operations, while also highlighting a regression for
Decimalwhen evaluating itsBigDecimal representation.Overall, the changes improve performance for the common integer cases while keeping the impact on the other numeric types limited.
References
#1121