Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

An external API sends 64-bit unsigned identifiers and big integer amounts in JSON, and a JavaScript front end also displays them. Some ids appear wrong in the UI, and Java parsing of a few ids throws. How would you handle parsing, conversion and serialisation end to end?

Unsigned 64-bit ids above Long.MAX_VALUE do not fit in a signed long, so Long.parseLong throws on them; we parse with Long.parseUnsignedLong and keep the bits in a long with unsigned helpers such as Long.toUnsignedString and Long.compareUnsigned, or use BigInteger. In the browser, JavaScript numbers are IEEE doubles that hold integers exactly only up to 2^53 - 1, so larger ids are silently rounded. The end-to-end fix is to send such ids and large amounts as JSON strings, state that in the API contract, and test the boundary values 2^53, 2^63 and 2^64 - 1.

Why this question is asked

Identifiers are the values we least expect to go wrong, because we never do arithmetic on them. But a 64-bit id passes through several number systems on its way to the screen: the partner's system, the JSON text, Java's signed long, and JavaScript's double. Each has a different range, and an id that is valid in the first can throw in the second or change its last digits in the last. Interviewers ask this scenario to see whether we can reason about a value across a whole system rather than one method. In this answer, we will recap the three ranges involved, look at why Java throws, handle unsigned values correctly in Java, explain why the UI shows wrong ids, and then set up serialisation and tests so it stays fixed.

1A Quick Recap of the Three Ranges

A signed 64-bit long in Java uses one bit for the sign, so it holds values from -2^63 to 2^63 - 1, that is up to 9,223,372,036,854,775,807. An unsigned 64-bit value uses all 64 bits for magnitude, so it goes up to 2^64 - 1, which is 18,446,744,073,709,551,615. Many systems written in C, Go or Rust, and many databases, use unsigned 64-bit ids, so half of their range is out of reach of a plain long.

JavaScript has one Number type, an IEEE 754 double. A double has 53 bits of precision, so it represents every integer exactly only up to 2^53 - 1 = 9,007,199,254,740,991, which JavaScript calls Number.MAX_SAFE_INTEGER. Above that, it can only represent some integers, and the others are rounded to the nearest one it can.

Boundary values across the stack
ValueJava longUnsigned longJavaScript Number
2^53 - 1 = 9007199254740991fitsfitsexact (MAX_SAFE_INTEGER)
2^53 + 1 = 9007199254740993fitsfitsrounded to ...992
2^63 = 9223372036854775808parseLong throwsfitsrounded
2^64 - 1 = 18446744073709551615parseLong throwsfits (bits all 1)rounded
Ids between 2^53 and 2^63 break only in the browser; ids above 2^63 also break Java parsing.

2Why Java Parsing Throws, and Unsigned Parsing in Java

The ids that throw are the ones above 2^63 - 1. Long.parseLong treats the text as signed and rejects anything outside its range. Java has no unsigned long type, but since Java 8 it has methods that treat the 64 bits of a long as unsigned.

Java
Long.parseLong("9223372036854775808"); // NumberFormatException: out of signed range
long id = Long.parseUnsignedLong("18446744073709551615");
System.out.println(id); // -1: same 64 bits, read as signed
System.out.println(Long.toUnsignedString(id)); // 18446744073709551615
System.out.println(Long.compare(id, 1L)); // -1: signed compare says id is smaller
System.out.println(Long.compareUnsigned(id, 1L)); // 1: unsigned compare is correct
BigInteger big = new BigInteger(Long.toUnsignedString(id)); // 18446744073709551615

Notice the danger in the middle lines. The long holds the right bits, but anything that treats it as signed, such as println, sorting, comparisons or a database column of type BIGINT, sees a negative number. So if we store unsigned ids in a long, every place that prints, compares or divides must use the unsigned helpers: toUnsignedString, compareUnsigned, divideUnsigned and remainderUnsigned. That discipline is hard to keep across a team, so for ids that are only passed around, a String or a small value type is often simpler, and for amounts that need arithmetic, BigInteger or BigDecimal is safer.

3Why the UI Shows Wrong Ids

The ids that look wrong in the UI are a different group: anything above 2^53 - 1, even if it fits in a Java long. Jackson writes a long as a JSON number, and the browser's JSON.parse turns every JSON number into a double before our code sees it.

Java
// Java side: a long written as a JSON number
{"orderId": 9007199254740993}
// Browser side
JSON.parse('{"orderId": 9007199254740993}').orderId; // 9007199254740992
Number.MAX_SAFE_INTEGER; // 9007199254740991
Step throughA large id travelling to the browser
Partner APIJava serviceBrowser
id 9007199254740993 as JSON number
parse into long: exact
Jackson writes number 9007199254740993
JSON.parse: double 9007199254740992
GET /orders/9007199254740992
404 or a different order

6 messages in order. Play them, or step through them one at a time.

The value is exact in Java and silently changed in the browser.

The id changes by one in the last digit, which is the worst kind of failure: it looks valid. Links built from it point at another order or at nothing, and when the front end sends it back, the server looks up the wrong record. Nothing throws on either side. JavaScript has BigInt for large integers, but JSON.parse does not produce BigInt values, so the precision is lost before application code can choose.

4Serialise Large Ids and Amounts as Strings

The reliable fix is to never send these values as JSON numbers. A string keeps every digit exactly in every language, and ids are opaque labels anyway: nobody adds two order ids.

Java
public record OrderView(
@JsonFormat(shape = JsonFormat.Shape.STRING) long orderId, // "9007199254740993"
@JsonSerialize(using = ToStringSerializer.class) BigInteger amountMinor, // "18446744073709551615"
String partnerId) {} // unsigned ids kept as text
// JSON produced
{"orderId":"9007199254740993","amountMinor":"18446744073709551615","partnerId":"18446744073709551615"}

@JsonFormat with shape STRING tells Jackson to write the long as a quoted string, and Jackson reads it back from a string just as happily. ToStringSerializer does the same for any type. For partner ids above 2^63 that we never compute with, keeping them as a String in Java removes the unsigned problem entirely. Jackson 3, used by Spring Boot 4, moves the core classes to new packages while the annotations stay in com.fasterxml.jackson.annotation, so check your version when copying imports.

On the way in, we also have to decide what the partner sends. If they send unsigned ids as JSON numbers, Jackson will refuse to bind values above Long.MAX_VALUE to a long, so we bind them to BigInteger or String. Either way, the decision goes into the API contract, for example the OpenAPI schema says type string with a pattern of digits, so the front-end team and every other consumer know what to expect.

5Contract Tests With Boundary Values

Bugs like this come back whenever someone adds a new field or a new consumer. So we pin the behaviour with tests that use the boundary values: 2^53 - 1 and 2^53 + 1, where the browser starts losing precision; 2^63 - 1 and 2^63, where signed parsing stops; and 2^64 - 1, the largest unsigned value. Each test sends the value through the real serialiser and checks that the exact digit string arrives, and a front-end test parses the same JSON and checks the id is a string. These tests are cheap and fail clearly, which is exactly what we want around a silent failure.

6Follow-up questions

Could the front end just use BigInt?

Only if it receives the value as a string first. JSON.parse converts numbers to doubles before our code runs, so the digits are already lost. With string ids, the front end can keep them as strings for display and links, which is all it needs, or convert to BigInt where it truly calculates.

What database type would you use for unsigned 64-bit ids?

PostgreSQL has no unsigned bigint, so the choices are numeric(20), which stores the full range exactly, or bigint holding the same bits as a signed value, which keeps the column compact but makes ordering and display wrong unless every query knows the trick. MySQL has BIGINT UNSIGNED, but then Java must read it as BigInteger or with unsigned helpers. For ids that are only looked up, a text or numeric column is usually the least surprising.

Is this only a problem for ids?

No. Any large integer has the same issue: amounts in minor units for some currencies, counters, nanosecond timestamps, and hashes. Money with fractions should already be a decimal string or BigDecimal; the same API rule, large numbers as strings, covers all of them.

7Conclusion

In this answer, we saw that one id passes through three ranges. Values above 2^63 - 1 make Long.parseLong throw, so we use Long.parseUnsignedLong with toUnsignedString and compareUnsigned, or BigInteger, or simply keep opaque ids as strings. Values above 2^53 - 1 survive Java but are rounded by JavaScript's doubles, so the fix is to serialise such ids and large amounts as JSON strings with @JsonFormat(shape = STRING) or ToStringSerializer, document that in the API contract, and test the boundary values 2^53, 2^63 and 2^64 - 1.

What to say in the interviewAbout 90 seconds aloud

Open with

I would first separate the two symptoms, because they have different causes. The Java parsing failures are ids above Long.MAX_VALUE: they are unsigned 64-bit values, and a signed long only goes to 2 to the 63 minus 1, so parseLong throws.

Then, if they want more

For those I would use Long.parseUnsignedLong, which keeps the right bits in a long, and then use Long.toUnsignedString and Long.compareUnsigned wherever we print or compare, or, if the id is never used in arithmetic, simply keep it as a String or BigInteger, which is harder to misuse.

The UI problem is different: JavaScript numbers are doubles, so integers are only exact up to 2 to the 53 minus 1, about 9 quadrillion. Anything above that is silently rounded by JSON.parse, so the page shows a slightly different id and links go to the wrong record.

The fix end to end is to treat ids and large amounts as strings on the wire: in Jackson, @JsonFormat with shape STRING on long fields, or ToStringSerializer, and on input bind large values to BigInteger or String.

I would write that into the OpenAPI contract so every consumer knows, and add contract tests with boundary values, 2 to the 53 plus 1, 2 to the 63, and 2 to the 64 minus 1, that check the exact digits survive from the partner through our service to the browser.

About this answer

Last updated
12 November 2026
Versions it names
Java 8, Spring Boot 4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "An external API sends 64-bit unsigned identifiers and big integer amounts in JSON, and a...": https://scrivio-interview-library.netlify.app/java/types-and-values/817c8d074a/