question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add serialization feature: strict numbers

See original GitHub issue

This is a proposal for a new serialization feature, SerializationFeature.STRICT_NUMBERS.

This feature would be disabled by default. With this feature enabled, the following actions would throw JsonProcessingException:

  1. Attempting to serialize +Infinity, -Infinity, or NaN
  2. Attempting to serialize a long that cannot be represented as a numerically-equivalent double

+Infinity, -Infinity, NaN

Non-finite numbers cannot be represented in JSON. Jackson’s current behavior is to serialize them as strings. This “works” if Jackson is on both ends (serializing and deserializing) but not necessarily when a different actor is deserializing. In particular, if client-side JavaScript is deserializing, the current behavior masks a class of bugs that might be difficult to detect – putting strings where the client expects numbers.

Checking whether a number is finite is extremely low-cost (Double.isFinite(x)) so this part of the feature should not have a noticeable performance burden.

Personally, I’m not intentionally serializing non-finite values anywhere and I’d prefer it if Jackson would throw an exception if I did so by mistake.

Longs that don’t fit

Edit: I mistakenly called this a limitation of numbers in JSON, when it’s actually about numbers in JavaScript.

Not all long values can be represented as 64-bit floating point numbers (double), which is the only number type in JSONJavaScript. For example, casting these numbers to double in Java will result in loss of information:

  • 2^63 - 1
  • 2^53 + 1

All long values whose absolute value is <= 2^53 can be represented exactly as double, so there is a fast path for validation of most common long values. This will likely include all values that come from epoch millisecond timestamps or SQL auto-incrementing IDs. Randomly generated long values, however, would likely require the slow path (if there is a slow path) and be invalid.

All short and int values are smaller than 2^53, so this feature should add no performance burden for them.

Personally, I am accidentally serializing some long values that cannot be represented accurately as numbers in JSONJavaScript. Woops! I didn’t notice until I implemented the strict numbers feature for myself.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:1
  • Comments:22 (15 by maintainers)

github_iconTop GitHub Comments

3reactions
mirabiloscommented, Aug 11, 2016

NaN and ±Infinity should just be serialised as literal “null” instead. I’ve been searching for about half an hour for how to get Jackson to do that, i.e. behave as per the ECMAscript/JSON standard, already, with no success so far…

0reactions
cowtowncodercommented, Oct 3, 2022

Ah ok @sbernard31 that makes sense. I agree. I think I misread your comment there, thank you for clarification.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SerializationFeature (jackson-databind 2.8.5 API) - javadoc.io
Feature that determines whethere Java Enum values are serialized as numbers (true), or textual values (false). WRITE_ENUMS_USING_TO_STRING.
Read more >
Enum SerializationFeature
Feature that determines whethere Java Enum values are serialized as numbers (true), or textual values (false). WRITE_ENUMS_USING_TO_STRING.
Read more >
Formats, Serializers, and Deserializers
The Protobuf serializer recursively registers all referenced schemas separately. With Protobuf and JSON Schema support, the Confluent Platform adds the ability ...
Read more >
How to enable strict type parsing for jackson? - Stack Overflow
FWIW, if you come across this much later, like I did, ALLOW_COERCION_OF_SCALARS will help somewhat by reducing the number of conversions ...
Read more >
Serialization with Jackson - Documentation - Akka
Note. Add the -parameters Java compiler option for usage by the ParameterNamesModule. It reduces the need for some annotations. · Warning. Don't use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found