Skip to content

[Java][JSON] No way to give one codec to all properties of a type in a class #4213

Open

Description

@pavel-ptashyts

What happens

A model that needs one representation for every property of a given type has to repeat @JsonCodec on each
property. There is no way to declare it once for the class.

Our case is plain notation for BigDecimal (Jackson's WRITE_BIGDECIMAL_AS_PLAIN): without a codec Fory writes
new BigDecimal("1E+1") as 1E+1, and the partners reading our documents expect 10. So every price-like
property carries the annotation:

public class ImpressionEvent {
  @JsonCodec(PlainBigDecimalCodec.class)
  public BigDecimal price;
  @JsonCodec(PlainBigDecimalCodec.class)
  public BigDecimal bidFloor;
  @JsonCodec(contentCodec = PlainBigDecimalCodec.class)
  public Optional<BigDecimal> winPrice = Optional.empty();
  // ... and so on for every BigDecimal property of every model
}

The global route is closed on purpose: registerCodec rejects BigDecimal (and the other built-in scalars),
and the docs point to occurrence-level JsonCodec instead. A module JsonCodecFactory that claims BigDecimal
is accepted but never consulted for a BigDecimal property, so the output stays 1E+1. That is a reasonable rule for a JVM-wide setting, but
it leaves no middle ground between "every BigDecimal in the instance" and "one property". A property that
forgets the annotation is silently written in scientific notation, and nothing catches it until a consumer
complains.

Reproduced on main (4f799472), JDK 25.

Proposal

A repeatable class annotation that gives a default codec to the properties of that class whose type matches:

@JsonDefaultCodec(type = BigDecimal.class, codec = PlainBigDecimalCodec.class)
public class ImpressionEvent {
  public BigDecimal price;                                // PlainBigDecimalCodec
  public Optional<BigDecimal> winPrice = Optional.empty(); // contentCodec = PlainBigDecimalCodec
  public List<BigDecimal> floors = List.of();              // elementCodec = PlainBigDecimalCodec
  @JsonCodec(OtherCodec.class)
  public BigDecimal special;                              // explicit occurrence codec wins
}

Semantics:

  • Target type. The explicit raw type member; primitive and wrapper are one type. The codec's generic
    signature is not read, so a mismatch between type and the codec's value type fails at use, like a wrong
    property @JsonCodec. Two defaults on one class for the same type fail.
  • Matching. A property whose raw type is the target uses the codec as a whole. Otherwise one level of
    standard container is tried, mapped to the existing child slots: Optional/AtomicReference content
    (contentCodec), Collection/object array/AtomicReferenceArray element (elementCodec), and Map value
    (valueCodec). Deeper nesting, wildcard or unresolved type-variable children, and primitive arrays (so a
    Byte default leaves byte[] alone) are not filled.
  • Precedence. Any representation declared on the property itself (@JsonCodec, @JsonFormat,
    @JsonByteArray, @JsonRawValue, @JsonUnwrapped) wins. Otherwise the default behaves exactly as if the
    property declared @JsonCodec(value = ...) or the matching child member, so it takes the place of a property
    annotation in the existing precedence table. A child slot of a container that a registration, factory or type
    declaration owns is not defaulted.
  • Scope. Properties and creator parameters of the annotated class, including those declared in its
    superclasses. Declarations on a superclass apply to subclasses; a subclass may override the default for the
    same type. The default does not propagate into nested object types: their codecs are resolved once per type
    and must not depend on the containing class. Any-properties are not covered.
  • Mixin. An exact-target @JsonMixin can supply, replace or remove the annotation, so it can be applied to
    classes one does not own.

Cost: the selection happens once, while the object codec is built, and results in the same per-property
codec selection an explicit annotation produces today. Generated readers and writers do not change for classes
without the annotation.

Implementation: #4214. An earlier draft inferred the target from the codec's type argument (which also allowed
parameterized targets such as Optional<BigDecimal>); it was replaced by the explicit type member because the
inference needed generic-signature matching, Kotlin variance handling and extra shrinker keep rules, and
mis-targeted Scala codecs over primitives.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions