Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
28a9d3e
feat(java): add class-level default codecs to fory-json
pavel-ptashyts Nov 7, 2026
effc85f
fix(java): address review of class-level default codecs
pavel-ptashyts Nov 7, 2026
bb1d114
fix(java): resolve default child codecs without the container's ordin…
pavel-ptashyts Nov 7, 2026
cdc451d
fix(java): keep semantic Kotlin types and codec supertypes for defaul…
pavel-ptashyts Nov 7, 2026
9d3bf35
style(java): keep overloads of default codec lookup together
pavel-ptashyts Nov 7, 2026
8a1f8d3
fix(java): retain default codecs in KSP and merged models, skip unbox…
pavel-ptashyts Nov 7, 2026
d23c244
fix(java): retain inherited and container default codecs in KSP, reje…
pavel-ptashyts Nov 7, 2026
468793c
fix(kotlin): keep repeated default codecs supplied by a KSP Mixin
pavel-ptashyts Nov 7, 2026
db4463b
fix(java): reject Kotlin unsigned default targets, document and test …
pavel-ptashyts Nov 7, 2026
36c4f60
fix(java): check container ownership inside the resolution snapshot, …
pavel-ptashyts Nov 7, 2026
93b5db3
fix(java): match Kotlin covariant and generic-array targets, reject e…
pavel-ptashyts Nov 7, 2026
b2c02a7
style(kotlin): format default codec diagnostic
pavel-ptashyts Nov 7, 2026
d93b44d
fix(java): match nested Kotlin generic arrays through occurrence tokens
pavel-ptashyts Nov 7, 2026
5dc199f
test(kotlin): pin creator reads of open elements, mixin over inherite…
pavel-ptashyts Nov 7, 2026
cb2623a
fix(java): normalize covariance on targets only, pair container subcl…
pavel-ptashyts Nov 7, 2026
1b0dd69
test(java): pin class default precedence over codec registration
pavel-ptashyts Nov 7, 2026
51dcaa0
fix(java): use an occurrence token only when it is the declared argument
pavel-ptashyts Nov 7, 2026
873fb31
fix(java): read covariant wildcards as bounds only in Kotlin codec ta…
pavel-ptashyts Nov 7, 2026
c7b91d0
fix(java): compare raw array targets by component token
pavel-ptashyts Nov 7, 2026
48a214e
fix(java): select creator defaults from the logical property type, re…
pavel-ptashyts Nov 7, 2026
7fc1c0f
ci(kotlin): expect consumer rules of the default codec corpus models
pavel-ptashyts Nov 7, 2026
6a563c8
fix(java): carry default codec selections without implementing JsonCodec
pavel-ptashyts Nov 7, 2026
6571da5
fix(java): read Kotlin Any arguments in codec targets as Object
pavel-ptashyts Nov 7, 2026
2da0a9d
refactor(java): name the JsonDefaultCodec target type explicitly
pavel-ptashyts Nov 10, 2026
68cee2a
fix(java): keep unresolved type-variable children out of class defaults
pavel-ptashyts Nov 10, 2026
ea1cd08
docs(java): escape the annotation example in JsonDefaultCodec javadoc
pavel-ptashyts Nov 10, 2026
87ba711
Merge remote-tracking branch 'apache/main' into feat/json-declared-ty…
pavel-ptashyts Nov 11, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ci/tasks/kotlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
"PlatformAccount",
"PlatformBox",
"PlatformCircle",
"PlatformContainerDefaults",
"PlatformDefaultCodecs",
"PlatformDefaults",
"PlatformDirectOverride",
"PlatformId",
"PlatformInheritedDefaults",
"PlatformMarker",
"PlatformNullHandling",
"PlatformOpen",
Expand All @@ -47,6 +50,7 @@
CORPUS_MIXIN_TARGETS = {
"PlatformDefaultMixin": "PlatformDefaultTarget",
"PlatformJavaProfileMixin": "PlatformJavaProfile",
"PlatformMixinDefaults": "PlatformMixinDefaultsTarget",
"PlatformMixinOverrideAnnotations": "PlatformMixinOverride",
}

Expand Down
2 changes: 1 addition & 1 deletion docs/json/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ license: |
Fory JSON provides these mapping and validation annotations in
`org.apache.fory.json.annotation`:
`JsonAnyGetter`, `JsonAnyProperty`, `JsonAnySetter`, `JsonByteArray`, `JsonCodec`, `JsonCreator`,
`JsonDuplicateProperties`, `JsonFormat`, `JsonIgnore`, `JsonInclude`, `JsonProperty`, `JsonPropertyOrder`, `JsonRawValue`, `JsonSubTypes`, `JsonUnwrapped`,
`JsonDefaultCodec`, `JsonDuplicateProperties`, `JsonFormat`, `JsonIgnore`, `JsonInclude`, `JsonProperty`, `JsonPropertyOrder`, `JsonRawValue`, `JsonSubTypes`, `JsonUnwrapped`,
`JsonValidator`, and `JsonValue`. `JsonType` is a separate build-time model marker. They are
Fory JSON APIs, not Jackson, Gson, or Fory binary-protocol compatibility annotations.

Expand Down
71 changes: 64 additions & 7 deletions docs/json/custom-codecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,63 @@ The child members have these meanings:
| `keyCodec` | `Map<K, V>` | JSON member name for `K` |
| `valueCodec` | `Map<K, V>` | direct `V` value |

### Class defaults with `JsonDefaultCodec`

Use `@JsonDefaultCodec` on a class when every property of one type needs the same occurrence codec.
Built-in scalar types such as `BigDecimal` cannot be registered with `registerCodec`, so this is
the way to give them one representation across a model without annotating each property:

```java
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import org.apache.fory.json.annotation.JsonDefaultCodec;

@JsonDefaultCodec(type = BigDecimal.class, codec = PlainDecimalCodec.class)
public class Invoice {
public BigDecimal total; // PlainDecimalCodec
public Optional<BigDecimal> discount; // contentCodec = PlainDecimalCodec
public List<BigDecimal> lines; // elementCodec = PlainDecimalCodec
@JsonCodec(OtherDecimalCodec.class)
public BigDecimal tax; // the property declaration wins
}
```

`type` names the raw property type and `codec` the codec used for it. Repeat the annotation for
several types; one class may name each type at most once, and a primitive and its wrapper are the
same type. Type arguments are not part of the match, so `type = List.class` covers every `List`
property; use a property `JsonCodec` where one parameterization needs its own codec.

A property or creator parameter whose raw type equals `type` uses the codec as if it declared
`@JsonCodec(value = ...)`.

Otherwise a `type` equal to the direct child of an object array, `Collection`,
`AtomicReferenceArray`, `Optional`, `AtomicReference`, or the value of a `Map` fills the matching
child member from the table above; a language module's own containers, such as Scala `Option` or
`Seq`, are not filled, and neither are primitive arrays, so a `Byte` default leaves `byte[]` to
`byteArrayFormat`. A child is filled only where the container keeps its standard mapping: a
registered codec or factory, a module, or a type declaration that owns the container keeps it
unchanged. Deeper nesting is not searched, and a wildcard child such as `List<? extends Shape>`
or an unresolved type variable such as `List<T>` in a raw generic class keeps its mapping, as an
explicit child member would reject it.

A default codec is used exactly like a property `JsonCodec`, so it receives and owns null as
described above for every property it reaches. Properties that declare `JsonCodec`, `JsonFormat`,
`JsonByteArray`, `JsonRawValue`, or `JsonUnwrapped`, and JSON Any properties, do not use defaults;
there is no separate per-property opt-out.
A value-class property read and written through its unboxed carrier cannot take a codec, so a
default that matches one fails like a declared `JsonCodec`; a nullable or container occurrence of
the value class is boxed and uses the default. A `type` that is a Kotlin unsigned type, which the
Kotlin module carries as a tagged primitive, is rejected.

Defaults declared on superclasses apply to the subclass, and a subclass default for the same type
replaces the inherited one. Defaults stop at the class boundary: a nested object type uses its own
declarations. A declaration on an interface is rejected; the build-time processors also reject
one on an enum or annotation type, which never gets an object mapping. An exact-target Mixin may
supply or replace the annotation; a Mixin that supplies or removes either the single or the
repeated form replaces both forms declared on the target, and a removal also drops the defaults
inherited from superclasses.

### Kotlin occurrences

Kotlin uses the same codec registrations and `JsonCodec` annotation. Apply property annotations to
Expand Down Expand Up @@ -352,13 +409,13 @@ The first `JsonAnySetter` parameter is the String property name. Its second para

Fory resolves each current value in this order:

| Priority | Source |
| -------: | ------------------------------------------------- |
| 1 | Current property or parameter `JsonCodec` |
| 2 | Exact `registerCodec` registration |
| 3 | Exact-target type `JsonCodec` after Mixin overlay |
| 4 | Inherited-frontier type `JsonCodec` declaration |
| 5 | Built-in or default JSON mapping |
| Priority | Source |
| -------: | ------------------------------------------------------------------------ |
| 1 | Current property or parameter `JsonCodec`, else class `JsonDefaultCodec` |
| 2 | Exact `registerCodec` registration |
| 3 | Exact-target type `JsonCodec` after Mixin overlay |
| 4 | Inherited-frontier type `JsonCodec` declaration |
| 5 | Built-in or default JSON mapping |

One logical property may expose the annotation from its field, getter, setter parameter, creator
parameter, or record propagation. Repeated configurations must be identical; Fory does not merge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public object PlatformCorpusChecks {
)
.values == listOf("kept")
)
val defaults = PlatformDefaultCodecs(PlatformToken("a"), listOf(PlatformToken("b")))
val defaultsText = json.toJson(defaults)
check(defaultsText == "{\"token\":\"t:a\",\"tokens\":[\"t:b\"]}") { defaultsText }
check(json.fromJson(defaultsText, PlatformDefaultCodecs::class.java) == defaults)
val inherited = PlatformInheritedDefaults(PlatformToken("c"))
check(json.toJson(inherited) == "{\"token\":\"t:c\"}") { json.toJson(inherited) }
check(json.fromJson("{\"token\":\"t:c\"}", PlatformInheritedDefaults::class.java) == inherited)
val container = PlatformContainerDefaults(PlatformToken("d"))
check(json.toJson(container) == "{\"token\":\"t:d\"}") { json.toJson(container) }
check(json.fromJson("{\"token\":\"t:d\"}", PlatformContainerDefaults::class.java) == container)
val type = KotlinJsonCorpus.rootType()
val decoded = json.fromJson(KotlinJsonCorpus.rootJson(), type)
verifyRoot(decoded)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package org.apache.fory.integration.kotlin.json.corpus
import kotlin.jvm.JvmInline
import org.apache.fory.json.annotation.JsonByteArray
import org.apache.fory.json.annotation.JsonCodec
import org.apache.fory.json.annotation.JsonDefaultCodec
import org.apache.fory.json.annotation.JsonInclude
import org.apache.fory.json.annotation.JsonMixin
import org.apache.fory.json.annotation.JsonProperty
Expand Down Expand Up @@ -94,6 +95,60 @@ public class PlatformTokenCodec : AbstractJsonValueCodec<PlatformToken>() {
}
}

public class PlatformDefaultTokenCodec : AbstractJsonValueCodec<PlatformToken>() {
override fun write(writer: JsonWriter, value: PlatformToken?) {
if (value == null) writer.writeNull() else writer.writeString("t:" + value.value)
}

override fun read(reader: JsonReader): PlatformToken? {
val value = reader.readString() ?: return null
return PlatformToken(value.removePrefix("t:"))
}
}

@JsonType
@JsonDefaultCodec(type = PlatformToken::class, codec = PlatformDefaultTokenCodec::class)
public data class PlatformDefaultCodecs(
public val token: PlatformToken,
public val tokens: List<PlatformToken>,
)

@JsonDefaultCodec(type = PlatformToken::class, codec = PlatformDefaultTokenCodec::class)
public abstract class PlatformDefaultBase

@JsonType
public data class PlatformInheritedDefaults(public val token: PlatformToken) :
PlatformDefaultBase()

@JsonType
@JsonDefaultCodec.List(
JsonDefaultCodec(type = PlatformToken::class, codec = PlatformDefaultTokenCodec::class)
)
public data class PlatformContainerDefaults(public val token: PlatformToken)

public data class PlatformLabel(public val value: String)

public class PlatformLabelCodec : AbstractJsonValueCodec<PlatformLabel>() {
override fun write(writer: JsonWriter, value: PlatformLabel?) {
if (value == null) writer.writeNull() else writer.writeString("l:" + value.value)
}

override fun read(reader: JsonReader): PlatformLabel? {
val value = reader.readString() ?: return null
return PlatformLabel(value.removePrefix("l:"))
}
}

public data class PlatformMixinDefaultsTarget(
public val token: PlatformToken,
public val label: PlatformLabel,
)

@JsonMixin(target = PlatformMixinDefaultsTarget::class)
@JsonDefaultCodec(type = PlatformToken::class, codec = PlatformDefaultTokenCodec::class)
@JsonDefaultCodec(type = PlatformLabel::class, codec = PlatformLabelCodec::class)
public interface PlatformMixinDefaults

@JsonSubTypes
@JsonCodec(PlatformDirectOverrideCodec::class)
public data class PlatformDirectOverride(public val value: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ public class KotlinJsonCorpusTest {
}
}

@Test
public fun mixinRepeatedDefaults(): Unit {
for (codegen in listOf(false, true)) {
val json =
ForyJsonKotlin.builder()
.withCodegen(codegen)
.withAsyncCompilation(false)
.registerMixin(PlatformMixinDefaults::class.java)
.build()
val value = PlatformMixinDefaultsTarget(PlatformToken("a"), PlatformLabel("b"))
val text = json.toJson(value)
assertEquals(text, "{\"token\":\"t:a\",\"label\":\"l:b\"}")
assertEquals(json.fromJson(text, PlatformMixinDefaultsTarget::class.java), value)
}
}

@Test
public fun sharedRoundTrip(): Unit {
val json =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ public class KspRetentionResourceTest {
rules("PlatformDirectOverride"),
"$PACKAGE.PlatformDirectOverrideCodec",
)
val defaultCodecs = rules("PlatformDefaultCodecs")
assertConstructor(defaultCodecs, "$PACKAGE.PlatformDefaultTokenCodec")
assertTrue(
defaultCodecs.contains("@interface org.apache.fory.json.annotation.JsonDefaultCodec\n"),
defaultCodecs,
)
assertTrue(
defaultCodecs.contains("@interface org.apache.fory.json.annotation.JsonDefaultCodec\$List"),
defaultCodecs,
)
for (model in listOf("PlatformInheritedDefaults", "PlatformContainerDefaults")) {
val text = rules(model)
assertConstructor(text, "$PACKAGE.PlatformDefaultTokenCodec")
assertTrue(
text.contains("@interface org.apache.fory.json.annotation.JsonDefaultCodec\$List"),
text,
)
assertFalse(text.contains("JsonDefaultCodec.List"), text)
}
assertTrue(
rules("PlatformInheritedDefaults").contains("class $PACKAGE.PlatformDefaultBase"),
rules("PlatformInheritedDefaults"),
)
val mixinDefaults = mixinRules("PlatformMixinDefaults")
assertConstructor(mixinDefaults, "$PACKAGE.PlatformDefaultTokenCodec")
assertConstructor(mixinDefaults, "$PACKAGE.PlatformLabelCodec")
val mixin = mixinRules("PlatformJavaProfileMixin")
assertTrue(mixin.contains("class $PACKAGE.PlatformJavaProfile"), mixin)
assertTrue(mixin.contains("class $PACKAGE.PlatformJavaProfileMixin"), mixin)
Expand Down Expand Up @@ -148,17 +174,21 @@ public class KspRetentionResourceTest {
"META-INF/proguard/fory-json-$PACKAGE.PlatformAccount.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformBox.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformCircle.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformContainerDefaults.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformDefaultCodecs.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformDefaults.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformNullHandling.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformDirectOverride.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformId.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformInheritedDefaults.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformMarker.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformOpen.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformRoot.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformShape.pro",
"META-INF/proguard/fory-json-$PACKAGE.PlatformSquare.pro",
"META-INF/proguard/fory-json-mixin-$PACKAGE.PlatformJavaProfileMixin.pro",
"META-INF/proguard/fory-json-mixin-$PACKAGE.PlatformDefaultMixin.pro",
"META-INF/proguard/fory-json-mixin-$PACKAGE.PlatformMixinDefaults.pro",
"META-INF/proguard/fory-json-mixin-$PACKAGE.PlatformMixinOverrideAnnotations.pro",
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ private static Set<String> supportedAnnotations() {
annotations.add(JSON_MIXIN_REMOVE);
annotations.add(JSON_SUB_TYPES);
annotations.add(JSON_SUBTYPE_GENERATION);
annotations.add("org.apache.fory.json.annotation.JsonDefaultCodec");
annotations.add("org.apache.fory.json.annotation.JsonDefaultCodec.List");
return Collections.unmodifiableSet(annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ final class JsonMixinAnnotations {
private static final String JSON_BYTE_ARRAY = JSON_PACKAGE + ".annotation.JsonByteArray";
private static final String JSON_CODEC = JSON_PACKAGE + ".annotation.JsonCodec";
private static final String JSON_CREATOR = JSON_PACKAGE + ".annotation.JsonCreator";
private static final String JSON_DEFAULT_CODEC = JSON_PACKAGE + ".annotation.JsonDefaultCodec";
private static final String JSON_DEFAULT_CODEC_LIST = JSON_DEFAULT_CODEC + ".List";
private static final String JSON_DUPLICATE_PROPERTIES =
JSON_PACKAGE + ".annotation.JsonDuplicateProperties";
private static final String JSON_FORMAT = JSON_PACKAGE + ".annotation.JsonFormat";
Expand All @@ -79,6 +81,8 @@ final class JsonMixinAnnotations {
JSON_BYTE_ARRAY,
JSON_CODEC,
JSON_CREATOR,
JSON_DEFAULT_CODEC,
JSON_DEFAULT_CODEC_LIST,
JSON_DUPLICATE_PROPERTIES,
JSON_FORMAT,
JSON_IGNORE,
Expand Down
Loading
Loading