Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

How do Gson's @Since and @Until annotations with GsonBuilder.setVersion work, and would you use them to version a public API?

@Since(2.0) on a field includes it only when the Gson instance's configured version is 2.0 or higher, and @Until(3.0) excludes it from version 3.0 on; GsonBuilder.setVersion(2.5) chooses which fields are active for that instance, and without setVersion the annotations are ignored. It lets one class serve several wire versions, which is handy for small internal changes, but for a public API it ties versioning to one class and one library, so separate DTOs per version, or additive changes with tolerant readers, are easier to reason about and test.

Why this question is asked

API versioning is one of those topics where a convenient mechanism and a sound strategy are not the same thing. Gson's @Since and @Until look like built-in versioning, and the question is whether we would trust a public API to them. Interviewers ask it to hear us explain the mechanism precisely and then judge it against what public APIs actually need: clear contracts, independent evolution and easy testing. In this answer, we will see exactly how the annotations and setVersion interact, run an example at three versions, look at where they help, and explain what we would use for a public API instead.

1How the Annotations Work

Both annotations take a double that represents a version number. @Since(x) says "this field exists from version x". @Until(x) says "this field exists up to, but not including, version x". Neither does anything on its own: they take effect only when the Gson instance is built with setVersion(v). Gson then includes a field only if its @Since value is at most v and its @Until value is greater than v. A field without either annotation is always included. The same annotations can be placed on a class, to include or exclude a whole type.

Java
public class Product {
private String name; // always present
@Until(2.0) private double price; // removed in 2.0
@Since(2.0) private Money listPrice; // added in 2.0
@Since(3.0) private List<String> tags; // added in 3.0
}
Gson v1 = new GsonBuilder().setVersion(1.0).create();
Gson v25 = new GsonBuilder().setVersion(2.5).create();
Gson v3 = new GsonBuilder().setVersion(3.0).create();
v1.toJson(p); // {"name":"Mug","price":12.5}
v25.toJson(p); // {"name":"Mug","listPrice":"12.50 EUR"}
v3.toJson(p); // {"name":"Mug","listPrice":"12.50 EUR","tags":["kitchen"]}
new Gson().toJson(p); // all four fields: without setVersion the annotations are ignored
Fields active for each configured version
FieldsetVersion(1.0)setVersion(2.5)setVersion(3.0)
nameyesyesyes
@Until(2.0) priceyesnono
@Since(2.0) listPricenoyesyes
@Since(3.0) tagsnonoyes
A field is active when since <= version < until.

The rules apply in both directions, so a v1 Gson also ignores listPrice if a client sends it. The trailing note is important: an instance created without setVersion, for example in a test or by another team, writes every field, which is easy to miss.

2Where This Is Handy

The appeal is that one class describes several wire formats. For small, internal cases, such as a file format written by one service and read by another where both sides use the same Java class, or a cache entry whose shape changed once, @Since and @Until avoid duplicating a class for a single field. The code stays short, and the history of each field is visible right on it.

3Why It Is a Weak Fit for a Public API

Public APIs have different needs. Each version is a contract that external clients depend on, possibly for years, and it must be documented and tested on its own. With version annotations, there is no single place that shows what v2 looks like: we have to evaluate the annotations in our heads, across every class in the object graph. A field's version is a double, so nothing stops someone from writing @Since(2.1) on one field and forgetting the related field elsewhere.

The versioning is also tied to one class and one library. Behaviour changes that are not "field present or absent", a renamed field, a different date format, a value that moved into a nested object, cannot be expressed. The rules live in Gson, so migrating to Jackson or generating an OpenAPI description means re-implementing them. And tests must build a Gson per version and remember that a plain new Gson() writes everything.

Finally, the request path has to pick a version for each call, for example from a URL prefix or header, and build or choose the matching Gson instance. That plumbing is the same work as choosing a DTO per version, without the clarity a DTO gives.

4What We Would Use Instead

For a public API, there are two simpler strategies. The first, and the most common, is to avoid versions for most changes: only add new optional fields, never rename or remove, and make clients tolerant readers that ignore fields they do not know. Most API evolution fits this rule, and no version is needed at all.

The second, for real breaking changes, is a separate DTO per version, mapped from the same domain model.

Java
record ProductV1(String name, double price) { }
record ProductV2(String name, Money listPrice, List<String> tags) { }
@GetMapping("/v1/products/{id}") ProductV1 v1(@PathVariable long id) { return mapper.toV1(service.find(id)); }
@GetMapping("/v2/products/{id}") ProductV2 v2(@PathVariable long id) { return mapper.toV2(service.find(id)); }

Each version is now a visible, testable type, any library can serialize it, tools can document it, and a renamed or reshaped field is just a different record. The cost is a little duplication and a mapping per version, which is cheap compared with reasoning about annotations across a graph. So we would use @Since and @Until, if at all, for small internal formats, and DTOs plus additive changes for public APIs.

5Follow-up questions

What happens if a field has both @Since and @Until?

Both rules apply, so the field is active only in the range since <= version < until. For example @Since(1.5) @Until(3.0) is present for versions 1.5 up to but not including 3.0.

How do clients tell the server which version they want?

Gson does not help with that; it is our API's design. Common choices are a URL prefix such as /v2, a custom header, or a media type parameter. The server then has to choose the matching Gson instance or, better, the matching DTO.

Is a tolerant reader really enough for most changes?

For additions, yes: Gson and Jackson in Spring Boot both ignore unknown properties, so a new optional field breaks nobody. It fails only for removals, renames and meaning changes, and those are the cases where a new version is justified anyway.

6Conclusion

In this answer, we saw that @Since and @Until mark the version range in which a field is active, and that GsonBuilder.setVersion chooses the version for one Gson instance, with the annotations ignored when no version is set. The mechanism is handy for small internal formats that share one class, but for a public API it hides each version's shape inside annotations, ties versioning to Gson, and cannot express renames or format changes. Additive changes with tolerant readers, and separate DTOs per version for breaking changes, are clearer and easier to test.

What to say in the interviewAbout 80 seconds aloud

Open with

With @Since and @Until I annotate fields with a version number. @Since(2.0) means the field is included only when the configured version is 2.0 or higher, and @Until(3.0) means it is excluded from 3.0 onwards, so a field is active when since is at most the version and the version is below until.

Then, if they want more

The version is set per Gson instance with GsonBuilder.setVersion, for example 2.5, and the rules apply to both writing and reading. If no version is set, the annotations are simply ignored and everything is written, which is an easy trap in tests. It is handy for small internal formats where one class serves a couple of versions.

For a public API I would not use it. Each version becomes something you have to reconstruct from annotations spread across a class graph, it is tied to Gson, so a move to Jackson or generating OpenAPI means redoing it, and it cannot express renames or format changes, only presence.

Instead I would make most changes additive, new optional fields only, with clients that ignore unknown fields, and for real breaking changes I would publish a separate DTO per version, such as ProductV1 and ProductV2 records, mapped from the same domain model, so each version is visible and testable.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. 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 "How do Gson's @Since and @Until annotations with GsonBuilder.setVersion work, and would...": https://scrivio-interview-library.netlify.app/java/json-libraries/4b17a51f04/