You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fory.apache.org by ch...@apache.org on 2026/08/20 11:47:58 UTC
(fory) branch main updated: feat(json): add Kotlin JSON support (#3937)
This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git
The following commit(s) were added to refs/heads/main by this push:
new d2c46d591 feat(json): add Kotlin JSON support (#3937)
d2c46d591 is described below
commit d2c46d591b4e56ed89a1ed4c7b31ac70ea09600b
Author: Shawn Yang <sh...@gmail.com>
AuthorDate: Thu Aug 20 19:47:53 2026 +0800
feat(json): add Kotlin JSON support (#3937)
## Why?
Kotlin/JVM models carry constructor defaults, nullability, unsigned and
value-class semantics,
sealed hierarchies, and complete generic type information that Java
`Class` tokens cannot express.
This PR adds an optional Kotlin JSON module while keeping the existing
Java JSON APIs and Fory
binary protocols unchanged.
## What does this PR do?
- Adds `fory-json-kotlin` with `ForyJsonKotlin.builder()` and reusable
`jsonTypeRef<T>()` tokens for
Kotlin object models, defaults, nullability, generics, value classes,
unsigned types, sealed
hierarchies, Mixin overlays, custom codecs, and supported Kotlin/JVM
built-ins.
- Extends the shared JSON resolver, object model, and generated
capability paths so language-owned
metadata can retain direct generated readers and writers, exact semantic
types, nullability, and
primitive/no-box value-class carriers.
- Adds `fory-json-kotlin-ksp` as a retention-only processor that emits
exact R8/ProGuard rules for
`@JsonType` and Kotlin-owned `@JsonMixin` declarations; it does not
generate codecs or application
classes.
- Adds a small shared JVM/Android/GraalVM corpus covering defaults,
value classes and unsigned
values, sealed types, Mixins, custom codecs, and generics, plus JPMS and
release wiring.
- Adds Kotlin JSON documentation and a standalone Gradle/JMH benchmark
project for Fory,
kotlinx.serialization, Moshi, and Jackson Kotlin. Benchmark timing is
not part of CI.
## Related issues
N/A.
## AI Contribution Checklist
- [ ] Substantial AI assistance was used in this PR: `yes` / `no`
- [ ] If `yes`, I included a completed [AI Contribution
Checklist](https://github.com/apache/fory/blob/main/AI_POLICY.md#9-contributor-checklist-for-ai-assisted-prs)
in this PR description and the required `AI Usage Disclosure`.
- [ ] If `yes`, my PR description includes the required `ai_review`
summary and screenshot evidence or equivalent persisted links of the
final clean AI review results from both fresh reviewers described in
`AI_POLICY.md`, the Fory-guided reviewer and the independent general
reviewer, on the current PR diff or current HEAD after the latest code
changes.
## Does this PR introduce any user-facing change?
Yes. It adds the optional Kotlin/JVM JSON runtime, Kotlin type-token and
builder APIs, Android
retention-rule processor, documentation, and platform setup described
above.
- [x] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
---
.github/workflows/ci.yml | 66 +-
...ava-snapshot.yaml => release-jvm-snapshot.yaml} | 18 +-
AGENTS.md | 2 +
benchmarks/kotlin/.gitignore | 4 +
benchmarks/kotlin/README.md | 70 ++
benchmarks/kotlin/build.gradle.kts | 103 ++
benchmarks/kotlin/gradle.properties | 28 +
benchmarks/kotlin/run_json_benchmark.py | 127 ++
benchmarks/kotlin/settings.gradle.kts | 52 +
.../fory/benchmark/json/MediaContentBenchmark.kt | 194 +++
.../apache/fory/benchmark/json/BenchmarkCodecs.kt | 110 ++
.../org/apache/fory/benchmark/json/MediaContent.kt | 71 ++
.../fory/benchmark/json/MediaContentFixture.kt | 76 ++
.../kotlin/src/main/resources/data/eishay.json | 30 +
.../benchmark/json/BenchmarkCorrectnessTest.kt | 86 ++
ci/deploy.sh | 37 +-
ci/release.py | 307 ++++-
ci/run_ci.py | 29 +-
ci/run_ci.sh | 39 +-
ci/tasks/java.py | 42 +-
ci/tasks/kotlin.py | 186 ++-
ci/test_release.py | 88 --
docs/benchmarks/index.md | 8 +
docs/benchmarks/json/kotlin/README.md | 22 +
docs/index.md | 17 +-
docs/introduction/choose-a-format.md | 14 +-
docs/introduction/support-matrix.md | 2 +-
docs/json/android.md | 89 +-
docs/json/annotations.md | 63 +-
docs/json/custom-codecs.md | 45 +
docs/json/getting-started.md | 52 +-
docs/json/graalvm.md | 48 +-
docs/json/index.md | 32 +-
docs/json/kotlin.md | 364 ++++++
docs/json/modules.md | 15 +
docs/json/object-mapping.md | 13 +
docs/json/scala.md | 5 +-
docs/json/security.md | 28 +-
docs/json/troubleshooting.md | 39 +-
docs/start/index.md | 4 +-
docs/start/kotlin.md | 44 +-
integration_tests/android_tests/README.md | 39 +-
integration_tests/android_tests/build.gradle | 47 +
integration_tests/android_tests/proguard-rules.pro | 11 +
.../fory/android/ForyAndroidInstrumentedTest.java | 5 +
.../fory/android/AndroidKotlinJsonScenarios.kt | 103 ++
.../{jpms_tests => graalvm_kotlin_tests}/pom.xml | 78 +-
.../fory/graalvm/kotlin/KotlinJsonProvider.java} | 28 +-
.../java/org/apache/fory/graalvm/kotlin/Main.java | 51 +
.../graalvm-kotlin-tests/native-image.properties | 18 +
integration_tests/grpc_tests/kotlin/pom.xml | 1 +
integration_tests/idl_tests/kotlin/pom.xml | 4 +
integration_tests/jpms_tests/pom.xml | 15 +
.../jpms_tests/src/main/java/module-info.java | 3 +
.../org/apache/fory/integration_tests/Test.java | 23 +
.../integration_tests/JpmsFieldAccessorTest.java | 5 +
integration_tests/kotlin_json_corpus/pom.xml | 179 +++
.../kotlin/json/corpus/PlatformJavaProfile.java} | 25 +-
.../kotlin/json/corpus/KotlinJsonCorpus.kt | 44 +
.../kotlin/json/corpus/PlatformCorpusChecks.kt | 47 +
.../json/corpus/PlatformJavaProfileMixin.kt} | 14 +-
.../kotlin/json/corpus/PlatformModels.kt | 89 ++
.../integration/kotlin/json/corpus/cases/root.json | 9 +
.../kotlin/json/corpus/KotlinJsonCorpusTest.kt} | 21 +-
.../kotlin/json/corpus/KspRetentionResourceTest.kt | 98 ++
.../org/apache/fory/codegen/CodeGenerator.java | 26 +-
.../java/org/apache/fory/codegen/JaninoUtils.java | 392 ++++++
.../main/java/org/apache/fory/meta/FieldTypes.java | 6 +-
.../java/org/apache/fory/meta/TypeExtMeta.java | 32 +-
.../main/java/org/apache/fory/reflect/TypeRef.java | 2 +
.../java/org/apache/fory/reflect/TypeRefTest.java | 20 +-
.../main/java/org/apache/fory/json/ForyJson.java | 267 +++-
.../org/apache/fory/json/JsonCodecFactory.java | 9 +-
.../main/java/org/apache/fory/json/JsonConfig.java | 27 +-
.../fory/json/annotation/ForyJsonProvider.java | 6 +-
.../org/apache/fory/json/annotation/JsonType.java | 31 +-
.../org/apache/fory/json/codec/ArrayCodec.java | 842 +++++++++++--
.../apache/fory/json/codec/ClosedSubtypeCodec.java | 184 ++-
.../apache/fory/json/codec/CollectionCodec.java | 242 ++--
.../fory/json/codec/DateTimeFormatCodec.java | 6 +-
.../fory/json/codec/DirectUnboxedValueCodec.java | 19 +-
.../apache/fory/json/codec/JsonObjectModel.java | 434 ++++++-
.../java/org/apache/fory/json/codec/MapCodec.java | 309 +++--
.../org/apache/fory/json/codec/ObjectCodec.java | 272 ++++-
.../apache/fory/json/codec/ObjectCodecBuilder.java | 602 +++++++--
.../org/apache/fory/json/codec/ScalarCodecs.java | 322 ++++-
.../fory/json/codec/TransparentNullCodec.java | 14 +-
.../json/codec/TransparentUnboxedValueCodec.java | 47 +
.../apache/fory/json/codec/UnboxedValueCodec.java | 77 ++
.../fory/json/codegen/DirectMethodCodegen.java | 274 +++++
.../org/apache/fory/json/codegen/JsonCodegen.java | 465 +++++--
.../apache/fory/json/codegen/JsonCodegenKey.java | 15 +
.../json/codegen/JsonGeneratedCodecBuilder.java | 93 +-
.../fory/json/codegen/JsonReaderCodegen.java | 758 ++++++++++--
.../fory/json/codegen/JsonWriterCodegen.java | 154 ++-
.../fory/json/codegen/Latin1ReaderCodegen.java | 4 +-
.../fory/json/codegen/StringWriterCodegen.java | 17 +-
.../fory/json/codegen/Utf16ReaderCodegen.java | 4 +-
.../fory/json/codegen/Utf8ReaderCodegen.java | 26 +-
.../fory/json/codegen/Utf8WriterCodegen.java | 20 +-
.../fory/json/meta/JsonCreatorDeclaration.java | 111 +-
.../fory/json/meta/JsonCreatorFieldInfo.java | 149 ++-
.../org/apache/fory/json/meta/JsonCreatorInfo.java | 349 +++++-
.../org/apache/fory/json/meta/JsonFieldInfo.java | 386 +++++-
.../org/apache/fory/json/reader/JsonReader.java | 435 +++++--
.../apache/fory/json/reader/Latin1JsonReader.java | 40 +-
.../apache/fory/json/reader/Utf16JsonReader.java | 41 +-
.../apache/fory/json/reader/Utf8JsonReader.java | 81 +-
.../json/resolver/ExactTypeRequiredException.java | 17 +-
.../json/resolver/JsonGeneratedClassRegistry.java | 90 +-
.../fory/json/resolver/JsonMixinAnnotations.java | 24 +
.../fory/json/resolver/JsonSharedRegistry.java | 368 ++++--
.../apache/fory/json/resolver/JsonTypeInfo.java | 66 +-
.../fory/json/resolver/JsonTypeResolver.java | 824 ++++++++-----
.../org/apache/fory/json/writer/JsonWriter.java | 126 +-
.../apache/fory/json/writer/StringJsonWriter.java | 135 +-
.../apache/fory/json/writer/Utf8JsonWriter.java | 159 ++-
.../apache/fory/json/ForyJsonGraalVMFeature.java | 88 +-
.../org/apache/fory/json/JsonAnyPropertyTest.java | 139 ++-
.../apache/fory/json/JsonAsyncCompilationTest.java | 23 +-
.../org/apache/fory/json/JsonContainerTest.java | 83 ++
.../java/org/apache/fory/json/JsonCreatorTest.java | 312 +++++
.../apache/fory/json/JsonFieldNameCacheTest.java | 6 +-
.../fory/json/JsonGeneratedCapabilityKeyTest.java | 223 ++++
.../apache/fory/json/JsonGeneratedCodecTest.java | 99 +-
.../java/org/apache/fory/json/JsonMixinTest.java | 40 +-
.../java/org/apache/fory/json/JsonModuleTest.java | 338 +++++-
.../java/org/apache/fory/json/JsonScalarTest.java | 192 +++
.../org/apache/fory/json/JsonSubTypesTest.java | 198 +++
.../java/org/apache/fory/json/JsonTestSupport.java | 36 +-
.../resolver/JsonGeneratedClassRegistryTest.java | 161 +++
.../json/resolver/JsonMixinAnnotationsTest.java | 60 +
java/pom.xml | 11 +
kotlin/README.md | 5 +-
kotlin/fory-json-kotlin-ksp/README.md | 33 +
kotlin/fory-json-kotlin-ksp/pom.xml | 106 ++
.../kotlin/ksp/ForyJsonKotlinSymbolProcessor.kt | 81 ++
.../ksp/ForyJsonKotlinSymbolProcessorProvider.kt | 16 +-
.../apache/fory/json/kotlin/ksp/JvmDescriptors.kt | 111 ++
.../apache/fory/json/kotlin/ksp/KspModelBuilder.kt | 1283 ++++++++++++++++++++
.../org/apache/fory/json/kotlin/ksp/Model.kt | 38 +-
.../apache/fory/json/kotlin/ksp/R8RulesWriter.kt | 114 ++
...devtools.ksp.processing.SymbolProcessorProvider | 1 +
.../apache/fory/json/kotlin/ksp/KspTestSupport.kt | 144 +++
.../fory/json/kotlin/ksp/ProcessorResourceTest.kt | 150 +++
.../fory/json/kotlin/ksp/R8RulesWriterTest.kt | 422 +++++++
kotlin/fory-json-kotlin/README.md | 96 ++
kotlin/fory-json-kotlin/pom.xml | 116 ++
.../kotlin/KotlinExactUnboxedValueOperations.java | 119 ++
.../kotlin/KotlinExactValueClassOperations.java | 416 +++++++
.../fory/json/kotlin/KotlinTemporalAccess.java | 40 +-
.../fory/json/kotlin/KotlinTimedValueCodec.java | 283 +++++
.../org/apache/fory/json/kotlin/ForyJsonKotlin.kt | 25 +-
.../fory/json/kotlin/KotlinJsonCodecFactory.kt | 107 ++
.../apache/fory/json/kotlin/KotlinMapKeyCodecs.kt | 187 +++
.../fory/json/kotlin/KotlinMetadataModels.kt | 938 ++++++++++++++
.../apache/fory/json/kotlin/KotlinProductCodecs.kt | 107 ++
.../fory/json/kotlin/KotlinProgressionCodecs.kt | 438 +++++++
.../apache/fory/json/kotlin/KotlinRangeCodecs.kt | 349 ++++++
.../fory/json/kotlin/KotlinSingletonCodecs.kt | 103 ++
.../fory/json/kotlin/KotlinTemporalCodecs.kt | 503 ++++++++
.../org/apache/fory/json/kotlin/KotlinTypeRefs.kt | 116 ++
.../fory/json/kotlin/KotlinUnsignedArrayCodecs.kt | 182 +++
.../fory/json/kotlin/KotlinUnsignedCodecs.kt | 396 ++++++
.../fory/json/kotlin/KotlinUnsupportedTypes.kt | 91 ++
.../fory/json/kotlin/KotlinValueClassCodecs.kt | 389 ++++++
.../fory/json/kotlin/KotlinValueClassMapKeys.kt | 237 ++++
.../fory/json/kotlin/KotlinValueClassMetadata.kt | 376 ++++++
.../fory/json/kotlin/KotlinValueClassOperations.kt | 428 +++++++
.../json/kotlin/KotlinValueClassPrimitiveCodecs.kt | 368 ++++++
.../apache/fory/json/kotlin/ForyJsonKotlinTest.kt | 147 +++
.../json/kotlin/KotlinAnnotationRuntimeTest.kt | 395 ++++++
.../fory/json/kotlin/KotlinBuiltInCodecsTest.kt | 736 +++++++++++
.../fory/json/kotlin/KotlinDefaultArgumentsTest.kt | 115 ++
.../fory/json/kotlin/KotlinGenericRuntimeTest.kt | 129 ++
.../fory/json/kotlin/KotlinMetadataModelsTest.kt | 363 ++++++
.../json/kotlin/KotlinNullabilityRuntimeTest.kt | 377 ++++++
.../json/kotlin/KotlinObjectRuntimeMatrixTest.kt | 252 ++++
.../fory/json/kotlin/KotlinRuntimeTestSupport.kt | 168 +++
.../fory/json/kotlin/KotlinSealedRuntimeTest.kt | 165 +++
.../fory/json/kotlin/KotlinTypeRefRuntimeTest.kt | 219 ++++
.../json/kotlin/KotlinUnsupportedRuntimeTest.kt | 93 ++
.../fory/json/kotlin/KotlinValueClassCodecTest.kt | 404 ++++++
kotlin/fory-kotlin-tests/pom.xml | 1 +
kotlin/pom.xml | 92 +-
.../scala/internal/DerivedScalaJsonCodec.scala | 38 +-
.../json/scala/internal/ScalaAlgebraicCodecs.scala | 8 +-
.../scala/internal/ScalaCollectionCodecs.scala | 16 +-
.../scala/internal/ScalaNumericRangeCodec.scala | 4 +-
.../json/scala/internal/ScalaObjectModels.scala | 106 +-
.../json/scala/internal/ScalaProductCodecs.scala | 9 +-
.../fory/json/scala/internal/ScalaTupleCodec.scala | 4 +-
.../scala/internal/ScalaTypeCodecFactory.scala | 45 +-
.../fory/json/scala/ScalaJsonDerivationSuite.scala | 165 ++-
.../apache/fory/json/scala/ScalaJsonSuite.scala | 22 +
195 files changed, 25690 insertions(+), 2351 deletions(-)
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index b9a51d88f..64d48191f 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -57,8 +57,10 @@ jobs:
cpp: ${{ steps.filter.outputs.cpp }}
cpp_code: ${{ steps.filter.outputs.cpp_code }}
java_code: ${{ steps.filter.outputs.java_code }}
+ android: ${{ steps.filter.outputs.android }}
python: ${{ steps.filter.outputs.python }}
graalvm: ${{ steps.filter.outputs.graalvm }}
+ graalvm_kotlin: ${{ steps.filter.outputs.graalvm_kotlin }}
rust: ${{ steps.filter.outputs.rust }}
swift: ${{ steps.filter.outputs.swift }}
javascript: ${{ steps.filter.outputs.javascript }}
@@ -78,8 +80,10 @@ jobs:
echo "cpp=true" >> "$GITHUB_OUTPUT"
echo "cpp_code=true" >> "$GITHUB_OUTPUT"
echo "java_code=true" >> "$GITHUB_OUTPUT"
+ echo "android=true" >> "$GITHUB_OUTPUT"
echo "python=true" >> "$GITHUB_OUTPUT"
echo "graalvm=true" >> "$GITHUB_OUTPUT"
+ echo "graalvm_kotlin=true" >> "$GITHUB_OUTPUT"
echo "rust=true" >> "$GITHUB_OUTPUT"
echo "swift=true" >> "$GITHUB_OUTPUT"
echo "javascript=true" >> "$GITHUB_OUTPUT"
@@ -121,6 +125,12 @@ jobs:
echo "java_code=false" >> "$GITHUB_OUTPUT"
fi
+ if grep -Eq '^(\.github/workflows/ci\.yml$|ci/run_ci\.(py|sh)$|ci/tasks/(common|kotlin)\.py$|java/(pom\.xml$|fory-(core|json|annotation-processor)/)|kotlin/|integration_tests/(kotlin_json_corpus|android_tests)/)' <<< "$changed_files"; then
+ echo "android=true" >> "$GITHUB_OUTPUT"
+ else
+ echo "android=false" >> "$GITHUB_OUTPUT"
+ fi
+
if grep -Eq '^(python/)' <<< "$changed_files"; then
echo "python=true" >> "$GITHUB_OUTPUT"
else
@@ -133,6 +143,12 @@ jobs:
echo "graalvm=false" >> "$GITHUB_OUTPUT"
fi
+ if grep -Eq '^(\.github/workflows/ci\.yml$|ci/run_ci\.(py|sh)$|ci/tasks/(common|kotlin)\.py$|java/(pom\.xml$|fory-(core|json|annotation-processor)/)|kotlin/|integration_tests/(kotlin_json_corpus|graalvm_kotlin_tests)/)' <<< "$changed_files"; then
+ echo "graalvm_kotlin=true" >> "$GITHUB_OUTPUT"
+ else
+ echo "graalvm_kotlin=false" >> "$GITHUB_OUTPUT"
+ fi
+
if grep -Eq '^(rust/)' <<< "$changed_files"; then
echo "rust=true" >> "$GITHUB_OUTPUT"
else
@@ -151,7 +167,7 @@ jobs:
echo "javascript=false" >> "$GITHUB_OUTPUT"
fi
- if grep -Eq '^(java/|kotlin/)' <<< "$changed_files"; then
+ if grep -Eq '^(\.github/workflows/(ci\.yml|release-jvm-snapshot\.yaml)$|ci/run_ci\.(py|sh)$|ci/tasks/(common|java|kotlin)\.py$|ci/release\.py$|java/(pom\.xml$|fory-(core|json|annotation-processor)/)|kotlin/|integration_tests/(kotlin_json_corpus|graalvm_kotlin_tests|jpms_tests|grpc_tests/kotlin|idl_tests/kotlin)/)' <<< "$changed_files"; then
echo "kotlin=true" >> "$GITHUB_OUTPUT"
else
echo "kotlin=false" >> "$GITHUB_OUTPUT"
@@ -287,7 +303,7 @@ jobs:
android:
name: Android Instrumented Tests API ${{ matrix.api-level }}
needs: changes
- if: needs.changes.outputs.java_code == 'true'
+ if: needs.changes.outputs.android == 'true'
runs-on: ubuntu-latest
strategy:
fail-fast: false
@@ -300,6 +316,10 @@ jobs:
with:
java-version: 17
distribution: "temurin"
+ - name: Set up Python 3.11
+ uses: actions/setup-python@v5
+ with:
+ python-version: 3.11
- name: Cache Maven local repository
uses: actions/cache@v4
with:
@@ -311,17 +331,15 @@ jobs:
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e
with:
gradle-version: "8.13"
- - name: Install Fory Java artifacts
- run: |
- cd java
- mvn -T16 --no-transfer-progress -pl fory-json,fory-annotation-processor -am install -DskipTests -Dmaven.javadoc.skip=true -Dmaven.source.skip=true
+ - name: Install Fory Java and Kotlin JSON artifacts
+ run: python ./ci/run_ci.py kotlin --task install
- name: Enable KVM
run: |
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm
- name: Run Android instrumented tests
- uses: reactivecircus/android-emulator-runner@a421e43855164a8197daf9d8d40fe71c6996bb0d # v2.38.0
+ uses: reactivecircus/android-emulator-runner@a421e43855164a8197daf9d8d40fe71c6996bb0d # v2.38.0
with:
api-level: ${{ matrix.api-level }}
arch: x86_64
@@ -329,6 +347,7 @@ jobs:
working-directory: integration_tests/android_tests
script: |
yes | sdkmanager "platforms;android-${{ matrix.api-level }}" "build-tools;35.0.0"
+ if [ "${{ matrix.api-level }}" = "26" ]; then gradle --no-daemon --stacktrace verifyKotlinJsonRules; fi
if [ "${{ matrix.api-level }}" = "26" ]; then gradle --no-daemon --stacktrace -PforyTestBuildType=debug connectedCheck; fi
gradle --no-daemon --stacktrace -PforyTestBuildType=release connectedCheck
- name: Upload Android Test Report
@@ -623,6 +642,29 @@ jobs:
shell: bash
run: ./ci/run_ci.sh graalvm_json_tests
+ graalvm_kotlin_json:
+ name: GraalVM Kotlin JSON CI
+ needs: changes
+ if: needs.changes.outputs.graalvm_kotlin == 'true'
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v5
+ - uses: graalvm/setup-graalvm@6f3fa030c4b8f77c1f554a860f593a654538fa38 # 1.5.6
+ with:
+ java-version: "25"
+ distribution: "graalvm"
+ github-token: ${{ secrets.GITHUB_TOKEN }}
+ native-image-job-reports: "true"
+ - name: Cache Maven local repository
+ uses: actions/cache@v4
+ with:
+ path: ~/.m2/repository
+ key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
+ restore-keys: |
+ ${{ runner.os }}-maven-
+ - name: Build and run Kotlin JSON native image
+ run: python ./ci/run_ci.py kotlin --task native-json
+
kotlin:
name: Kotlin CI
needs: changes
@@ -632,7 +674,7 @@ jobs:
MY_VAR: "PATH"
strategy:
matrix:
- java-version: ["8", "11", "17", "21"]
+ java-version: ["8", "11", "17", "21", "25", "26"]
steps:
- uses: actions/checkout@v5
- name: Set up JDK ${{ matrix.java-version }}
@@ -647,12 +689,10 @@ jobs:
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- - name: Set up Python 3.8
+ - name: Set up Python 3.11
uses: actions/setup-python@v5
with:
- python-version: 3.8
- - name: Install fory java
- run: python ./ci/run_ci.py java --install-jdks --install-fory
+ python-version: 3.11
- name: Run Kotlin CI
run: python ./ci/run_ci.py kotlin
@@ -2011,8 +2051,6 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: 3.8
- - name: Test release documentation updater
- run: python -m unittest ci.test_release
- name: Set up .NET 8
uses: actions/setup-dotnet@v4
with:
diff --git a/.github/workflows/release-java-snapshot.yaml b/.github/workflows/release-jvm-snapshot.yaml
similarity index 79%
rename from .github/workflows/release-java-snapshot.yaml
rename to .github/workflows/release-jvm-snapshot.yaml
index d82dcc6a0..9e66af475 100644
--- a/.github/workflows/release-java-snapshot.yaml
+++ b/.github/workflows/release-jvm-snapshot.yaml
@@ -18,21 +18,24 @@
# All `uses:` action pins in this workflow must come from the Apache action allowlist:
# https://github.com/apache/infrastructure-actions/blob/main/actions.yml
-name: Publish Fory Java Snapshot
+name: Publish Fory JVM Snapshot
on:
push:
branches:
- main
- - release-java-snapshot
+ - release-jvm-snapshot
+
+permissions:
+ contents: read
jobs:
- publish-java:
+ publish-jvm:
runs-on: ubuntu-latest
if: github.repository == 'apache/fory'
steps:
- uses: actions/checkout@v5
- - name: Set up Maven Central Repository
+ - name: Set up Apache snapshot repository
uses: actions/setup-java@v4
with:
java-version: "25"
@@ -42,8 +45,11 @@ jobs:
server-id: apache.snapshots.https
server-username: NEXUS_USERNAME
server-password: NEXUS_PASSWORD
- - name: Publish Fory Java Snapshot
- run: python ./ci/run_ci.py java --version 25 --release
+ - uses: sbt/setup-sbt@9d56cf12e9b58d219605e1d8bfe69a8395fedde0 # v1.5.1
+ with:
+ disk-cache: false
+ - name: Publish Java, Kotlin, and Scala snapshots
+ run: python ./ci/release.py publish_jvm --mode snapshot
env:
NEXUS_USERNAME: ${{ secrets.NEXUS_USER }}
NEXUS_PASSWORD: ${{ secrets.NEXUS_PW }}
diff --git a/AGENTS.md b/AGENTS.md
index 3f24db55c..403a3d003 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -199,6 +199,8 @@ This is the entry point for AI guidance in Apache Fory. Read this file first, th
- Add comments only when behavior is hard to understand or an algorithm is non-obvious.
- Do not remove existing code comments unless they are stale, misleading, redundant, or no longer necessary after the change.
- Only add tests that verify internal behaviors or fix specific bugs; do not create unnecessary tests unless requested.
+- Do not add unit tests for repository scripts. Validate scripts through their owning execution or
+ integration workflow instead of maintaining a parallel script-test suite.
- Do not add cleanup-sentinel tests that only pin deleted APIs or removed fields.
- Tests must exercise the actual code you wrote or changed. Do not write tests that pass by exercising a pre-existing code path that produces similar-looking results. Before writing a test, identify the exact new code path (annotation, codegen output, new API) and verify the test would fail if that code path were removed. When the change involves codegen or annotations, the test must use those annotations on real structs, run through the codegen pipeline, and verify the generated output [...]
- Keep test method names concise. Name the behavior under test without encoding the whole scenario or expected result in the method name.
diff --git a/benchmarks/kotlin/.gitignore b/benchmarks/kotlin/.gitignore
new file mode 100644
index 000000000..b3653f78a
--- /dev/null
+++ b/benchmarks/kotlin/.gitignore
@@ -0,0 +1,4 @@
+.gradle/
+build/
+reports/
+__pycache__/
diff --git a/benchmarks/kotlin/README.md b/benchmarks/kotlin/README.md
new file mode 100644
index 000000000..b9dbef386
--- /dev/null
+++ b/benchmarks/kotlin/README.md
@@ -0,0 +1,70 @@
+# Kotlin JSON Benchmarks
+
+This standalone Gradle/JMH project compares Fory JSON Kotlin, kotlinx.serialization, Moshi, and
+Jackson Kotlin on one immutable Eishay `MediaContent` model. It is a repository benchmark project,
+not a published Fory artifact.
+
+The model uses only `val` properties and has no public zero-argument constructor. All four
+libraries consume the same model and fixture; no library-specific data transfer object or measured
+model conversion is used. The fixture SHA-256 is
+`8faba2f57ab397f319aced5cf1e8411a76785557d4c7d1703ec9d540354310a1`.
+
+## Compared operations
+
+The suite contains exactly 16 methods: four libraries multiplied by String serialization, UTF-8
+byte serialization, String deserialization, and UTF-8 byte deserialization.
+
+Each library uses a retained declared-type or generated serializer API:
+
+- Fory retains `jsonTypeRef<MediaContent>()`, disables asynchronous compilation for deterministic
+ setup, enables null emission, and warms all four generated paths before measurement.
+- kotlinx.serialization retains `MediaContent.serializer()` and uses its String and stream APIs.
+- Moshi retains its KSP-generated adapter and uses its String and Okio buffer APIs.
+- Jackson Kotlin retains one `ObjectReader` and one `ObjectWriter` and uses its direct String and
+ byte APIs.
+
+The final byte materialization required by a library remains inside its measured byte-serialization
+method. Deserialization likewise includes any fresh in-memory stream or buffer required by that
+library. No byte method routes through a prebuilt String.
+
+## Correctness gates
+
+Before timing, setup verifies that every library:
+
+- decodes the exact fixture from String and UTF-8 bytes to the independent expected object;
+- emits structurally equivalent JSON from String and byte APIs; and
+- round-trips its own String and byte output.
+
+The Gradle build also fails unless the Moshi KSP adapter is present for every object model. Fory
+uses its normal HotSpot metadata path in this benchmark; Android retention rules do not participate
+in the measured runtime.
+
+## Build and run
+
+Install the current `fory-json-kotlin` artifact in Maven local first. Use Gradle 9.3.0 and a JDK 17
+or later toolchain.
+
+Build the correctness and JMH artifacts:
+
+```bash
+gradle --no-daemon test verifyGeneratedJsonArtifacts jmhJar
+```
+
+Run the standard JMH benchmark:
+
+```bash
+python run_json_benchmark.py --output-dir reports/json
+```
+
+The runner invokes JMH once for all 16 methods. By default, each method uses three 2-second warmup
+iterations and five 2-second measurement iterations. Standard JMH JSON is written to
+`benchmark_results.json`; the full process log is retained in `benchmark.log`. For a quick harness
+check without collecting publishable numbers, pass
+`--warmup-iterations 1 --iterations 1 --duration 100ms`.
+
+Use `--prepare-only` for CI correctness and Moshi adapter-generation checks without performance
+timing.
+
+See the [published Kotlin JSON benchmark report](../../docs/benchmarks/json/kotlin/README.md). The
+published page explicitly remains pending until a complete measured run is available; the tooling
+does not synthesize results or publish reports automatically.
diff --git a/benchmarks/kotlin/build.gradle.kts b/benchmarks/kotlin/build.gradle.kts
new file mode 100644
index 000000000..310103d35
--- /dev/null
+++ b/benchmarks/kotlin/build.gradle.kts
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.net.URLClassLoader
+
+plugins {
+ id("org.jetbrains.kotlin.jvm")
+ id("org.jetbrains.kotlin.plugin.serialization")
+ id("com.google.devtools.ksp")
+ id("me.champeau.jmh")
+}
+
+group = "org.apache.fory.benchmark"
+version = "1.0-SNAPSHOT"
+
+val foryVersion: String by project
+val kotlinxSerializationVersion: String by project
+val moshiVersion: String by project
+val jacksonVersion: String by project
+val benchmarkJmhVersion = providers.gradleProperty("jmhVersion").get()
+
+dependencies {
+ implementation("org.apache.fory:fory-json-kotlin:$foryVersion")
+ implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinxSerializationVersion")
+ implementation("com.squareup.moshi:moshi:$moshiVersion")
+ implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion")
+
+ ksp("com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion")
+
+ testImplementation("org.junit.jupiter:junit-jupiter:5.14.1")
+ testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.14.1")
+}
+
+kotlin {
+ jvmToolchain(17)
+ compilerOptions {
+ jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8)
+ }
+}
+
+java {
+ sourceCompatibility = JavaVersion.VERSION_1_8
+ targetCompatibility = JavaVersion.VERSION_1_8
+}
+
+tasks.test {
+ useJUnitPlatform()
+}
+
+jmh {
+ jmhVersion = benchmarkJmhVersion
+ benchmarkMode = listOf("thrpt")
+ timeUnit = "s"
+ warmupIterations = 3
+ iterations = 5
+ fork = 1
+ threads = 1
+ timeOnIteration = "2s"
+ warmup = "2s"
+ resultFormat = "JSON"
+}
+
+val verifyGeneratedJsonArtifacts = tasks.register("verifyGeneratedJsonArtifacts") {
+ dependsOn(tasks.named("classes"))
+ doLast {
+ val runtimeFiles = sourceSets.main.get().runtimeClasspath.files
+ URLClassLoader(runtimeFiles.map { it.toURI().toURL() }.toTypedArray(), null).use { loader ->
+ val modelNames =
+ listOf(
+ "org.apache.fory.benchmark.json.MediaContent",
+ "org.apache.fory.benchmark.json.Media",
+ "org.apache.fory.benchmark.json.Image",
+ )
+ for (modelName in modelNames) {
+ loader.loadClass(modelName + "JsonAdapter")
+ }
+ }
+ }
+}
+
+tasks.named("check") {
+ dependsOn(verifyGeneratedJsonArtifacts)
+}
+
+tasks.matching { it.name == "compileJmhKotlin" || it.name == "jmhClasses" }.configureEach {
+ dependsOn(verifyGeneratedJsonArtifacts)
+}
diff --git a/benchmarks/kotlin/gradle.properties b/benchmarks/kotlin/gradle.properties
new file mode 100644
index 000000000..c49f62017
--- /dev/null
+++ b/benchmarks/kotlin/gradle.properties
@@ -0,0 +1,28 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+foryVersion=1.7.0-SNAPSHOT
+kotlinVersion=2.3.20
+kspVersion=2.3.8
+kotlinxSerializationVersion=1.11.0
+moshiVersion=1.15.2
+jacksonVersion=2.22.1
+jmhVersion=1.37
+jmhPluginVersion=0.7.3
+gradleVersion=9.3.0
+org.gradle.caching=true
+org.gradle.jvmargs=-Xmx2g
diff --git a/benchmarks/kotlin/run_json_benchmark.py b/benchmarks/kotlin/run_json_benchmark.py
new file mode 100644
index 000000000..210bd9bcb
--- /dev/null
+++ b/benchmarks/kotlin/run_json_benchmark.py
@@ -0,0 +1,127 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Build and run the Kotlin JSON JMH benchmark."""
+
+from __future__ import annotations
+
+import argparse
+import json
+import subprocess
+from pathlib import Path
+
+BENCHMARK = "org.apache.fory.benchmark.json.MediaContentBenchmark.*"
+
+
+def parse_args() -> argparse.Namespace:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--output-dir", default="reports/json")
+ parser.add_argument("--warmup-iterations", type=int, default=3)
+ parser.add_argument("--iterations", type=int, default=5)
+ parser.add_argument("--duration", default="2s")
+ parser.add_argument("--skip-build", action="store_true")
+ parser.add_argument("--prepare-only", action="store_true")
+ args = parser.parse_args()
+ if args.warmup_iterations <= 0 or args.iterations <= 0:
+ parser.error("iteration counts must be positive")
+ return args
+
+
+def run(command: list[str], cwd: Path, log: Path) -> None:
+ print(f"Running in {cwd}: {' '.join(command)}", flush=True)
+ with log.open("w", encoding="utf-8") as output:
+ subprocess.run(
+ command,
+ cwd=cwd,
+ stdout=output,
+ stderr=subprocess.STDOUT,
+ text=True,
+ check=True,
+ )
+
+
+def main() -> None:
+ args = parse_args()
+ benchmark_dir = Path(__file__).resolve().parent
+ output_dir = Path(args.output_dir)
+ if not output_dir.is_absolute():
+ output_dir = benchmark_dir / output_dir
+ output_dir.mkdir(parents=True, exist_ok=True)
+
+ if not args.skip_build:
+ run(
+ [
+ "gradle",
+ "--no-daemon",
+ "clean",
+ "test",
+ "verifyGeneratedJsonArtifacts",
+ "jmhJar",
+ ],
+ benchmark_dir,
+ output_dir / "build.log",
+ )
+
+ jars = sorted((benchmark_dir / "build" / "libs").glob("*-jmh.jar"))
+ if len(jars) != 1:
+ raise RuntimeError(f"Expected one JMH JAR, found {len(jars)}")
+ if args.prepare_only:
+ print(f"Kotlin JSON benchmark JAR: {jars[0]}")
+ return
+
+ result = output_dir / "benchmark_results.json"
+ run(
+ [
+ "java",
+ "-jar",
+ str(jars[0]),
+ BENCHMARK,
+ "-f",
+ "1",
+ "-wi",
+ str(args.warmup_iterations),
+ "-i",
+ str(args.iterations),
+ "-t",
+ "1",
+ "-w",
+ args.duration,
+ "-r",
+ args.duration,
+ "-bm",
+ "thrpt",
+ "-tu",
+ "s",
+ "-rf",
+ "json",
+ "-rff",
+ str(result),
+ ],
+ benchmark_dir,
+ output_dir / "benchmark.log",
+ )
+ results = json.loads(result.read_text(encoding="utf-8"))
+ if not isinstance(results, list) or len(results) != 16:
+ raise RuntimeError(
+ f"Expected 16 JMH results, found {len(results)}; see benchmark.log"
+ )
+ print(f"Kotlin JSON benchmark results: {result}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/benchmarks/kotlin/settings.gradle.kts b/benchmarks/kotlin/settings.gradle.kts
new file mode 100644
index 000000000..1fb58d604
--- /dev/null
+++ b/benchmarks/kotlin/settings.gradle.kts
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.gradle.util.GradleVersion
+
+pluginManagement {
+ val kotlinVersion = providers.gradleProperty("kotlinVersion").get()
+ val kspVersion = providers.gradleProperty("kspVersion").get()
+ val jmhPluginVersion = providers.gradleProperty("jmhPluginVersion").get()
+ plugins {
+ id("org.jetbrains.kotlin.jvm") version kotlinVersion
+ id("org.jetbrains.kotlin.plugin.serialization") version kotlinVersion
+ id("com.google.devtools.ksp") version kspVersion
+ id("me.champeau.jmh") version jmhPluginVersion
+ }
+ repositories {
+ gradlePluginPortal()
+ mavenCentral()
+ }
+}
+
+dependencyResolutionManagement {
+ repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
+ repositories {
+ providers.gradleProperty("foryMavenRepository").orNull?.let { maven { url = uri(it) } }
+ mavenLocal()
+ mavenCentral()
+ }
+}
+
+val requiredGradle = providers.gradleProperty("gradleVersion").get()
+check(GradleVersion.current() == GradleVersion.version(requiredGradle)) {
+ "Kotlin JSON benchmarks require Gradle $requiredGradle, found ${GradleVersion.current()}"
+}
+
+rootProject.name = "fory-kotlin-json-benchmarks"
diff --git a/benchmarks/kotlin/src/jmh/kotlin/org/apache/fory/benchmark/json/MediaContentBenchmark.kt b/benchmarks/kotlin/src/jmh/kotlin/org/apache/fory/benchmark/json/MediaContentBenchmark.kt
new file mode 100644
index 000000000..86eac2c4c
--- /dev/null
+++ b/benchmarks/kotlin/src/jmh/kotlin/org/apache/fory/benchmark/json/MediaContentBenchmark.kt
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fory.benchmark.json
+
+import java.io.ByteArrayInputStream
+import java.io.ByteArrayOutputStream
+import java.util.concurrent.TimeUnit
+import kotlinx.serialization.ExperimentalSerializationApi
+import kotlinx.serialization.json.decodeFromStream
+import kotlinx.serialization.json.encodeToStream
+import okio.Buffer
+import org.openjdk.jmh.annotations.Benchmark
+import org.openjdk.jmh.annotations.BenchmarkMode
+import org.openjdk.jmh.annotations.Fork
+import org.openjdk.jmh.annotations.Measurement
+import org.openjdk.jmh.annotations.Mode
+import org.openjdk.jmh.annotations.OutputTimeUnit
+import org.openjdk.jmh.annotations.Scope
+import org.openjdk.jmh.annotations.Setup
+import org.openjdk.jmh.annotations.State
+import org.openjdk.jmh.annotations.Threads
+import org.openjdk.jmh.annotations.Warmup
+
+@State(Scope.Thread)
+open class BenchmarkState {
+ lateinit var codecs: BenchmarkCodecs
+ lateinit var expected: MediaContent
+ lateinit var fixtureString: String
+ lateinit var fixtureBytes: ByteArray
+
+ @Setup
+ fun setup() {
+ codecs = BenchmarkCodecs()
+ expected = MediaContentFixture.expected()
+ fixtureBytes = MediaContentFixture.bytes()
+ fixtureString = MediaContentFixture.text(fixtureBytes)
+ verifyFixtureReads()
+ verifyEncodedTreesAndRoundTrips()
+ warmForyPaths()
+ }
+
+ private fun verifyFixtureReads() {
+ check(codecs.foryFromString(fixtureString) == expected)
+ check(codecs.foryFromBytes(fixtureBytes) == expected)
+ check(codecs.kotlinxFromString(fixtureString) == expected)
+ check(codecs.kotlinxFromBytes(fixtureBytes) == expected)
+ check(codecs.moshiFromString(fixtureString) == expected)
+ check(codecs.moshiFromBytes(fixtureBytes) == expected)
+ check(codecs.jacksonFromString(fixtureString) == expected)
+ check(codecs.jacksonFromBytes(fixtureBytes) == expected)
+ }
+
+ private fun verifyEncodedTreesAndRoundTrips() {
+ val foryString = codecs.foryToString(expected)
+ val foryBytes = codecs.foryToBytes(expected)
+ val kotlinxString = codecs.kotlinxToString(expected)
+ val kotlinxBytes = codecs.kotlinxToBytes(expected)
+ val moshiString = codecs.moshiToString(expected)
+ val moshiBytes = codecs.moshiToBytes(expected)
+ val jacksonString = codecs.jacksonToString(expected)
+ val jacksonBytes = codecs.jacksonToBytes(expected)
+
+ val expectedTree = codecs.tree(foryString)
+ for (tree in
+ listOf(
+ codecs.tree(foryBytes),
+ codecs.tree(kotlinxString),
+ codecs.tree(kotlinxBytes),
+ codecs.tree(moshiString),
+ codecs.tree(moshiBytes),
+ codecs.tree(jacksonString),
+ codecs.tree(jacksonBytes)
+ )) {
+ check(tree == expectedTree) { "JSON libraries emitted structurally different output" }
+ }
+
+ check(codecs.foryFromString(foryString) == expected)
+ check(codecs.foryFromBytes(foryBytes) == expected)
+ check(codecs.kotlinxFromString(kotlinxString) == expected)
+ check(codecs.kotlinxFromBytes(kotlinxBytes) == expected)
+ check(codecs.moshiFromString(moshiString) == expected)
+ check(codecs.moshiFromBytes(moshiBytes) == expected)
+ check(codecs.jacksonFromString(jacksonString) == expected)
+ check(codecs.jacksonFromBytes(jacksonBytes) == expected)
+ }
+
+ private fun warmForyPaths() {
+ repeat(32) {
+ codecs.foryToString(expected)
+ codecs.foryToBytes(expected)
+ codecs.foryFromString(fixtureString)
+ codecs.foryFromBytes(fixtureBytes)
+ }
+ }
+}
+
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.SECONDS)
+@Warmup(iterations = 3, time = 2)
+@Measurement(iterations = 5, time = 2)
+@Fork(1)
+@Threads(1)
+@OptIn(ExperimentalSerializationApi::class)
+open class MediaContentBenchmark {
+ @Benchmark
+ fun foryStringSerialization(state: BenchmarkState): String =
+ state.codecs.fory.toJson(state.expected, state.codecs.foryType)
+
+ @Benchmark
+ fun foryUtf8BytesSerialization(state: BenchmarkState): ByteArray =
+ state.codecs.fory.toJsonBytes(state.expected, state.codecs.foryType)
+
+ @Benchmark
+ fun foryStringDeserialization(state: BenchmarkState): MediaContent =
+ state.codecs.fory.fromJson(state.fixtureString, state.codecs.foryType)
+
+ @Benchmark
+ fun foryUtf8BytesDeserialization(state: BenchmarkState): MediaContent =
+ state.codecs.fory.fromJson(state.fixtureBytes, state.codecs.foryType)
+
+ @Benchmark
+ fun kotlinxStringSerialization(state: BenchmarkState): String =
+ state.codecs.kotlinx.encodeToString(state.codecs.kotlinxSerializer, state.expected)
+
+ @Benchmark
+ fun kotlinxUtf8BytesSerialization(state: BenchmarkState): ByteArray {
+ val output = ByteArrayOutputStream()
+ state.codecs.kotlinx.encodeToStream(state.codecs.kotlinxSerializer, state.expected, output)
+ return output.toByteArray()
+ }
+
+ @Benchmark
+ fun kotlinxStringDeserialization(state: BenchmarkState): MediaContent =
+ state.codecs.kotlinx.decodeFromString(state.codecs.kotlinxSerializer, state.fixtureString)
+
+ @Benchmark
+ fun kotlinxUtf8BytesDeserialization(state: BenchmarkState): MediaContent =
+ state.codecs.kotlinx.decodeFromStream(
+ state.codecs.kotlinxSerializer,
+ ByteArrayInputStream(state.fixtureBytes),
+ )
+
+ @Benchmark
+ fun moshiStringSerialization(state: BenchmarkState): String =
+ state.codecs.moshiAdapter.toJson(state.expected)
+
+ @Benchmark
+ fun moshiUtf8BytesSerialization(state: BenchmarkState): ByteArray {
+ val buffer = Buffer()
+ state.codecs.moshiAdapter.toJson(buffer, state.expected)
+ return buffer.readByteArray()
+ }
+
+ @Benchmark
+ fun moshiStringDeserialization(state: BenchmarkState): MediaContent? =
+ state.codecs.moshiAdapter.fromJson(state.fixtureString)
+
+ @Benchmark
+ fun moshiUtf8BytesDeserialization(state: BenchmarkState): MediaContent? =
+ state.codecs.moshiAdapter.fromJson(Buffer().write(state.fixtureBytes))
+
+ @Benchmark
+ fun jacksonStringSerialization(state: BenchmarkState): String =
+ state.codecs.jacksonWriter.writeValueAsString(state.expected)
+
+ @Benchmark
+ fun jacksonUtf8BytesSerialization(state: BenchmarkState): ByteArray =
+ state.codecs.jacksonWriter.writeValueAsBytes(state.expected)
+
+ @Benchmark
+ fun jacksonStringDeserialization(state: BenchmarkState): MediaContent =
+ state.codecs.jacksonReader.readValue(state.fixtureString)
+
+ @Benchmark
+ fun jacksonUtf8BytesDeserialization(state: BenchmarkState): MediaContent =
+ state.codecs.jacksonReader.readValue(state.fixtureBytes)
+}
diff --git a/benchmarks/kotlin/src/main/kotlin/org/apache/fory/benchmark/json/BenchmarkCodecs.kt b/benchmarks/kotlin/src/main/kotlin/org/apache/fory/benchmark/json/BenchmarkCodecs.kt
new file mode 100644
index 000000000..18addb47e
--- /dev/null
+++ b/benchmarks/kotlin/src/main/kotlin/org/apache/fory/benchmark/json/BenchmarkCodecs.kt
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fory.benchmark.json
+
+import com.fasterxml.jackson.annotation.JsonInclude
+import com.fasterxml.jackson.databind.JsonNode
+import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.databind.ObjectReader
+import com.fasterxml.jackson.databind.ObjectWriter
+import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
+import com.squareup.moshi.JsonAdapter
+import com.squareup.moshi.Moshi
+import java.io.ByteArrayInputStream
+import java.io.ByteArrayOutputStream
+import kotlinx.serialization.ExperimentalSerializationApi
+import kotlinx.serialization.KSerializer
+import kotlinx.serialization.json.Json
+import kotlinx.serialization.json.decodeFromStream
+import kotlinx.serialization.json.encodeToStream
+import okio.Buffer
+import org.apache.fory.json.ForyJson
+import org.apache.fory.json.kotlin.ForyJsonKotlin
+import org.apache.fory.json.kotlin.jsonTypeRef
+import org.apache.fory.reflect.TypeRef
+
+@OptIn(ExperimentalSerializationApi::class)
+class BenchmarkCodecs {
+ val foryType: TypeRef<MediaContent> = jsonTypeRef()
+ val fory: ForyJson =
+ ForyJsonKotlin.builder().withAsyncCompilation(false).writeNullFields(true).build()
+
+ val kotlinx: Json = Json {
+ encodeDefaults = true
+ explicitNulls = true
+ }
+ val kotlinxSerializer: KSerializer<MediaContent> = MediaContent.serializer()
+
+ val moshiAdapter: JsonAdapter<MediaContent> =
+ Moshi.Builder().build().adapter(MediaContent::class.java).serializeNulls()
+
+ val jackson: ObjectMapper =
+ jacksonObjectMapper().setDefaultPropertyInclusion(JsonInclude.Include.ALWAYS)
+ val jacksonReader: ObjectReader = jackson.readerFor(MediaContent::class.java)
+ val jacksonWriter: ObjectWriter = jackson.writerFor(MediaContent::class.java)
+
+ fun foryToString(value: MediaContent): String = fory.toJson(value, foryType)
+
+ fun foryToBytes(value: MediaContent): ByteArray = fory.toJsonBytes(value, foryType)
+
+ fun foryFromString(value: String): MediaContent = fory.fromJson(value, foryType)
+
+ fun foryFromBytes(value: ByteArray): MediaContent = fory.fromJson(value, foryType)
+
+ fun kotlinxToString(value: MediaContent): String =
+ kotlinx.encodeToString(kotlinxSerializer, value)
+
+ fun kotlinxToBytes(value: MediaContent): ByteArray {
+ val output = ByteArrayOutputStream()
+ kotlinx.encodeToStream(kotlinxSerializer, value, output)
+ return output.toByteArray()
+ }
+
+ fun kotlinxFromString(value: String): MediaContent =
+ kotlinx.decodeFromString(kotlinxSerializer, value)
+
+ fun kotlinxFromBytes(value: ByteArray): MediaContent =
+ kotlinx.decodeFromStream(kotlinxSerializer, ByteArrayInputStream(value))
+
+ fun moshiToString(value: MediaContent): String = moshiAdapter.toJson(value)
+
+ fun moshiToBytes(value: MediaContent): ByteArray {
+ val buffer = Buffer()
+ moshiAdapter.toJson(buffer, value)
+ return buffer.readByteArray()
+ }
+
+ fun moshiFromString(value: String): MediaContent = checkNotNull(moshiAdapter.fromJson(value))
+
+ fun moshiFromBytes(value: ByteArray): MediaContent =
+ checkNotNull(moshiAdapter.fromJson(Buffer().write(value)))
+
+ fun jacksonToString(value: MediaContent): String = jacksonWriter.writeValueAsString(value)
+
+ fun jacksonToBytes(value: MediaContent): ByteArray = jacksonWriter.writeValueAsBytes(value)
+
+ fun jacksonFromString(value: String): MediaContent = jacksonReader.readValue(value)
+
+ fun jacksonFromBytes(value: ByteArray): MediaContent = jacksonReader.readValue(value)
+
+ fun tree(value: String): JsonNode = jackson.readTree(value)
+
+ fun tree(value: ByteArray): JsonNode = jackson.readTree(value)
+}
diff --git a/benchmarks/kotlin/src/main/kotlin/org/apache/fory/benchmark/json/MediaContent.kt b/benchmarks/kotlin/src/main/kotlin/org/apache/fory/benchmark/json/MediaContent.kt
new file mode 100644
index 000000000..b385a1245
--- /dev/null
+++ b/benchmarks/kotlin/src/main/kotlin/org/apache/fory/benchmark/json/MediaContent.kt
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fory.benchmark.json
+
+import com.squareup.moshi.JsonClass
+import kotlinx.serialization.Serializable
+import org.apache.fory.json.annotation.JsonType
+
+@JsonType
+@Serializable
+@JsonClass(generateAdapter = true)
+data class MediaContent(val images: List<Image>, val media: Media)
+
+@JsonType
+@Serializable
+@JsonClass(generateAdapter = true)
+data class Media(
+ val bitrate: Int,
+ val duration: Long,
+ val format: String,
+ val height: Int,
+ val persons: List<String>,
+ val player: Player,
+ val size: Long,
+ val title: String,
+ val uri: String,
+ val width: Int,
+ val copyright: String? = null,
+ val hasBitrate: Boolean = false,
+)
+
+@JsonType
+@Serializable
+@JsonClass(generateAdapter = true)
+data class Image(
+ val height: Int,
+ val size: ImageSize,
+ val title: String,
+ val uri: String,
+ val width: Int,
+ val media: Media? = null,
+)
+
+@Serializable
+enum class Player {
+ JAVA,
+ FLASH
+}
+
+@Serializable
+enum class ImageSize {
+ SMALL,
+ LARGE
+}
diff --git a/benchmarks/kotlin/src/main/kotlin/org/apache/fory/benchmark/json/MediaContentFixture.kt b/benchmarks/kotlin/src/main/kotlin/org/apache/fory/benchmark/json/MediaContentFixture.kt
new file mode 100644
index 000000000..d90a18f08
--- /dev/null
+++ b/benchmarks/kotlin/src/main/kotlin/org/apache/fory/benchmark/json/MediaContentFixture.kt
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fory.benchmark.json
+
+import java.nio.charset.StandardCharsets
+import java.security.MessageDigest
+
+object MediaContentFixture {
+ const val SHA256: String = "8faba2f57ab397f319aced5cf1e8411a76785557d4c7d1703ec9d540354310a1"
+
+ fun bytes(): ByteArray {
+ val stream =
+ checkNotNull(javaClass.classLoader.getResourceAsStream("data/eishay.json")) {
+ "Missing data/eishay.json"
+ }
+ val bytes = stream.use { it.readBytes() }
+ check(sha256(bytes) == SHA256) { "Eishay fixture SHA-256 does not match $SHA256" }
+ return bytes
+ }
+
+ fun text(bytes: ByteArray): String = String(bytes, StandardCharsets.UTF_8)
+
+ fun expected(): MediaContent =
+ MediaContent(
+ images =
+ listOf(
+ Image(
+ height = 768,
+ size = ImageSize.LARGE,
+ title = "Javaone Keynote",
+ uri = "http://javaone.com/keynote_large.jpg",
+ width = 1024,
+ ),
+ Image(
+ height = 240,
+ size = ImageSize.SMALL,
+ title = "Javaone Keynote",
+ uri = "http://javaone.com/keynote_small.jpg",
+ width = 320,
+ ),
+ ),
+ media =
+ Media(
+ bitrate = 262144,
+ duration = 18000000,
+ format = "video/mpg4",
+ height = 480,
+ persons = listOf("Bill Gates", "Steve Jobs"),
+ player = Player.JAVA,
+ size = 58982400,
+ title = "Javaone Keynote",
+ uri = "http://javaone.com/keynote.mpg",
+ width = 640,
+ ),
+ )
+
+ private fun sha256(bytes: ByteArray): String =
+ MessageDigest.getInstance("SHA-256").digest(bytes).joinToString("") { "%02x".format(it) }
+}
diff --git a/benchmarks/kotlin/src/main/resources/data/eishay.json b/benchmarks/kotlin/src/main/resources/data/eishay.json
new file mode 100644
index 000000000..a96a3a898
--- /dev/null
+++ b/benchmarks/kotlin/src/main/resources/data/eishay.json
@@ -0,0 +1,30 @@
+{"images": [{
+ "height":768,
+ "size":"LARGE",
+ "title":"Javaone Keynote",
+ "uri":"http://javaone.com/keynote_large.jpg",
+ "width":1024
+ }, {
+ "height":240,
+ "size":"SMALL",
+ "title":"Javaone Keynote",
+ "uri":"http://javaone.com/keynote_small.jpg",
+ "width":320
+ }
+ ],
+ "media": {
+ "bitrate":262144,
+ "duration":18000000,
+ "format":"video/mpg4",
+ "height":480,
+ "persons": [
+ "Bill Gates",
+ "Steve Jobs"
+ ],
+ "player":"JAVA",
+ "size":58982400,
+ "title":"Javaone Keynote",
+ "uri":"http://javaone.com/keynote.mpg",
+ "width":640
+ }
+}
diff --git a/benchmarks/kotlin/src/test/kotlin/org/apache/fory/benchmark/json/BenchmarkCorrectnessTest.kt b/benchmarks/kotlin/src/test/kotlin/org/apache/fory/benchmark/json/BenchmarkCorrectnessTest.kt
new file mode 100644
index 000000000..0e59890dc
--- /dev/null
+++ b/benchmarks/kotlin/src/test/kotlin/org/apache/fory/benchmark/json/BenchmarkCorrectnessTest.kt
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fory.benchmark.json
+
+import java.lang.reflect.Modifier
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.Assertions.assertFalse
+import org.junit.jupiter.api.Test
+
+class BenchmarkCorrectnessTest {
+ @Test
+ fun modelsHaveNoPublicZeroArgConstructor() {
+ for (type in listOf(MediaContent::class.java, Media::class.java, Image::class.java)) {
+ assertFalse(
+ type.constructors.any { Modifier.isPublic(it.modifiers) && it.parameterCount == 0 },
+ "${type.name} must not regress to a mutable Java-style model",
+ )
+ }
+ }
+
+ @Test
+ fun allLibrariesUseEquivalentShapes() {
+ val codecs = BenchmarkCodecs()
+ val expected = MediaContentFixture.expected()
+ val fixtureBytes = MediaContentFixture.bytes()
+ val fixtureString = MediaContentFixture.text(fixtureBytes)
+
+ assertEquals(expected, codecs.foryFromString(fixtureString))
+ assertEquals(expected, codecs.foryFromBytes(fixtureBytes))
+ assertEquals(expected, codecs.kotlinxFromString(fixtureString))
+ assertEquals(expected, codecs.kotlinxFromBytes(fixtureBytes))
+ assertEquals(expected, codecs.moshiFromString(fixtureString))
+ assertEquals(expected, codecs.moshiFromBytes(fixtureBytes))
+ assertEquals(expected, codecs.jacksonFromString(fixtureString))
+ assertEquals(expected, codecs.jacksonFromBytes(fixtureBytes))
+
+ val foryString = codecs.foryToString(expected)
+ val foryBytes = codecs.foryToBytes(expected)
+ val kotlinxString = codecs.kotlinxToString(expected)
+ val kotlinxBytes = codecs.kotlinxToBytes(expected)
+ val moshiString = codecs.moshiToString(expected)
+ val moshiBytes = codecs.moshiToBytes(expected)
+ val jacksonString = codecs.jacksonToString(expected)
+ val jacksonBytes = codecs.jacksonToBytes(expected)
+ val tree = codecs.tree(foryString)
+
+ for (actual in
+ listOf(
+ codecs.tree(foryBytes),
+ codecs.tree(kotlinxString),
+ codecs.tree(kotlinxBytes),
+ codecs.tree(moshiString),
+ codecs.tree(moshiBytes),
+ codecs.tree(jacksonString),
+ codecs.tree(jacksonBytes)
+ )) {
+ assertEquals(tree, actual)
+ }
+
+ assertEquals(expected, codecs.foryFromString(foryString))
+ assertEquals(expected, codecs.foryFromBytes(foryBytes))
+ assertEquals(expected, codecs.kotlinxFromString(kotlinxString))
+ assertEquals(expected, codecs.kotlinxFromBytes(kotlinxBytes))
+ assertEquals(expected, codecs.moshiFromString(moshiString))
+ assertEquals(expected, codecs.moshiFromBytes(moshiBytes))
+ assertEquals(expected, codecs.jacksonFromString(jacksonString))
+ assertEquals(expected, codecs.jacksonFromBytes(jacksonBytes))
+ }
+}
diff --git a/ci/deploy.sh b/ci/deploy.sh
index 6e2e9a953..4e926ecab 100755
--- a/ci/deploy.sh
+++ b/ci/deploy.sh
@@ -74,22 +74,6 @@ bump_javascript_version() {
python "$ROOT/ci/release.py" bump_version -l javascript -version "$1"
}
-deploy_jars() {
- local java_version java_major
- java_version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2; exit}')
- if [[ "$java_version" == 1.* ]]; then
- java_major=$(echo "$java_version" | cut -d. -f2)
- else
- java_major=$(echo "$java_version" | cut -d. -f1)
- fi
- if [[ "$java_major" -lt 25 ]]; then
- echo "Java releases must run on JDK25+ so MR-JAR entries are packaged"
- exit 1
- fi
- cd "$ROOT/java"
- mvn -T10 clean deploy --no-transfer-progress -DskipTests -Prelease
-}
-
build_pyfory() {
echo "$($PYTHON_CMD -V), path $(which "$PYTHON_CMD")"
install_pyarrow
@@ -158,22 +142,5 @@ install_pyarrow() {
fi
}
-deploy_scala() {
- echo "Start to build jars"
- sbt +publishSigned
- echo "Start to prepare upload"
- sbt sonatypePrepare
- echo "Start to upload jars"
- sbt sonatypeBundleUpload
- echo "Deploy scala jars succeed!"
-}
-
-case "$1" in
-java) # Deploy jars to maven repository.
- deploy_jars
- ;;
-*)
- echo "Execute command $*"
- "$@"
- ;;
-esac
+echo "Execute command $*"
+"$@"
diff --git a/ci/release.py b/ci/release.py
index aad936b52..f73ef427c 100644
--- a/ci/release.py
+++ b/ci/release.py
@@ -55,17 +55,59 @@ GRAALVM_FEATURE_SERVICE_ENTRY = (
MAVEN_RELEASE_CMD = (
"mvn -T10 clean deploy --no-transfer-progress -DskipTests -Papache-release"
)
-SCALA_RELEASE_CMDS = (
+MAVEN_SNAPSHOT_CMD = (
+ "mvn -T10 clean deploy --no-transfer-progress -DskipTests "
+ "-Dgpg.skip=true -Psnapshot-publication"
+)
+KOTLIN_RELEASE_PACKAGE_CMD = (
+ "mvn -T10 clean package --no-transfer-progress -DskipTests -Papache-release"
+)
+KOTLIN_RELEASE_DEPLOY_CMD = (
+ "mvn -T10 deploy --no-transfer-progress -DskipTests -Papache-release"
+)
+KOTLIN_SNAPSHOT_PACKAGE_CMD = (
+ "mvn -T10 clean package --no-transfer-progress -DskipTests "
+ "-Dgpg.skip=true -Psnapshot-publication"
+)
+KOTLIN_SNAPSHOT_DEPLOY_CMD = (
+ "mvn -T10 deploy --no-transfer-progress -DskipTests "
+ "-Dgpg.skip=true -Psnapshot-publication"
+)
+SCALA_RELEASE_COMMANDS = (
"sbt clean",
"sbt 'project fory-scala' +publishSigned",
"sbt 'project fory-json-scala' +publishSigned",
"sbt sonatypePrepare",
"sbt sonatypeBundleUpload",
)
+SCALA_SNAPSHOT_COMMANDS = (
+ "sbt clean",
+ "sbt 'project fory-scala' +publish",
+ "sbt 'project fory-json-scala' +publish",
+)
+JVM_PUBLICATION_MODES = ("release", "snapshot")
+JVM_PUBLICATION_CREDENTIALS = ("NEXUS_USERNAME", "NEXUS_PASSWORD")
+KOTLIN_PUBLIC_ARTIFACTS = (
+ "fory-kotlin",
+ "fory-kotlin-ksp",
+ "fory-json-kotlin",
+ "fory-json-kotlin-ksp",
+)
+KOTLIN_MODULE_NAMES = {
+ "fory-json-kotlin": "org.apache.fory.json.kotlin",
+ "fory-json-kotlin-ksp": "org.apache.fory.json.kotlin.ksp",
+}
+KOTLIN_SERVICE_PROVIDERS = {
+ "fory-kotlin-ksp": "org.apache.fory.kotlin.ksp.ForyKotlinSymbolProcessorProvider",
+ "fory-json-kotlin-ksp": (
+ "org.apache.fory.json.kotlin.ksp.ForyJsonKotlinSymbolProcessorProvider"
+ ),
+}
RELEASE_DOC_ROOTS = (
"README.md",
"java/README.md",
"java/fory-json/README.md",
+ "kotlin/README.md",
"rust/README.md",
"scala/README.md",
"scala/fory-scala/README.md",
@@ -196,55 +238,85 @@ def verify(v):
logger.info("Verified checksum successfully")
-def publish_jvm(languages="all"):
- """Publish Java, Kotlin, and Scala artifacts."""
+def publish_jvm(languages="all", mode="release"):
+ """Publish Java, Kotlin, and Scala artifacts through one ordered JVM owner."""
langs = _jvm_release_langs(languages)
+ _require_publication_authority(mode)
_ensure_openjdk25()
+ if "java" not in langs:
+ _verify_fory_core_mr_jar()
for lang in langs:
if lang == "java":
- _publish_java()
+ _publish_java(mode)
_verify_fory_core_mr_jar()
elif lang == "kotlin":
- _publish_kotlin()
+ _publish_kotlin(mode)
elif lang == "scala":
- _publish_scala()
+ _publish_scala(mode)
else:
raise NotImplementedError(f"Unsupported JVM release language: {lang}")
- _verify_fory_core_mr_jar()
-
-
-def publish_java():
- publish_jvm("java")
-
-
-def publish_kotlin():
- publish_jvm("kotlin")
-
-
-def publish_scala():
- publish_jvm("scala")
def _jvm_release_langs(languages):
if languages in (None, "", "all"):
return list(JVM_RELEASE_LANGS)
- langs = [lang.strip() for lang in languages.split(",") if lang.strip()]
- unsupported = [lang for lang in langs if lang not in JVM_RELEASE_LANGS]
+ selected = {lang.strip() for lang in languages.split(",") if lang.strip()}
+ if not selected:
+ raise ValueError("JVM release language selection is empty")
+ unsupported = sorted(selected.difference(JVM_RELEASE_LANGS))
if unsupported:
raise ValueError(f"Unsupported JVM release language(s): {unsupported}")
- return langs
+ return [lang for lang in JVM_RELEASE_LANGS if lang in selected]
+
+
+def _require_publication_authority(mode):
+ if mode not in JVM_PUBLICATION_MODES:
+ raise ValueError(f"Unsupported JVM publication mode: {mode}")
+ missing = [name for name in JVM_PUBLICATION_CREDENTIALS if not os.environ.get(name)]
+ if missing:
+ raise RuntimeError(f"JVM {mode} publication requires: {', '.join(missing)}")
+ if mode == "release" and not _has_gpg_secret_key():
+ raise RuntimeError("JVM release publication requires a GPG secret key")
+ os.environ["SONATYPE_USERNAME"] = os.environ["NEXUS_USERNAME"]
+ os.environ["SONATYPE_PASSWORD"] = os.environ["NEXUS_PASSWORD"]
+
+
+def _has_gpg_secret_key():
+ gpg = shutil.which("gpg")
+ if not gpg:
+ return False
+ result = subprocess.run(
+ [gpg, "--batch", "--list-secret-keys", "--with-colons"],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.DEVNULL,
+ text=True,
+ check=False,
+ )
+ return result.returncode == 0 and any(
+ line.startswith("sec:") for line in result.stdout.splitlines()
+ )
-def _publish_java():
- _run_release_cmd(MAVEN_RELEASE_CMD, "java")
+def _publish_java(mode="release"):
+ command = MAVEN_RELEASE_CMD if mode == "release" else MAVEN_SNAPSHOT_CMD
+ _run_release_cmd(command, "java")
-def _publish_kotlin():
- _run_release_cmd(MAVEN_RELEASE_CMD, "kotlin")
+def _publish_kotlin(mode="release"):
+ if mode == "release":
+ package_command = KOTLIN_RELEASE_PACKAGE_CMD
+ deploy_command = KOTLIN_RELEASE_DEPLOY_CMD
+ else:
+ package_command = KOTLIN_SNAPSHOT_PACKAGE_CMD
+ deploy_command = KOTLIN_SNAPSHOT_DEPLOY_CMD
+ _run_release_cmd(package_command, "kotlin")
+ verify_kotlin_artifacts()
+ _run_release_cmd(deploy_command, "kotlin")
-def _publish_scala():
- for command in SCALA_RELEASE_CMDS:
+def _publish_scala(mode="release"):
+ commands = SCALA_RELEASE_COMMANDS if mode == "release" else SCALA_SNAPSHOT_COMMANDS
+ for command in commands:
_run_release_cmd(command, "scala")
@@ -481,12 +553,103 @@ def _verify_fory_core_mr_jar():
if not re.search(feature_declaration, javap.stdout):
raise RuntimeError(f"{FORY_CORE_FEATURE} must remain a non-public final class")
logger.info(
- "Verified fory-core Multi-Release release jars: %s, %s",
- jar_path,
- sources_jar_path,
+ "Verified fory-core Multi-Release jars: %s, %s", jar_path, sources_jar_path
)
+def verify_kotlin_artifacts():
+ """Open every public Kotlin artifact and validate its publication surface."""
+ version = _read_kotlin_version()
+ for artifact in KOTLIN_PUBLIC_ARTIFACTS:
+ module_dir = os.path.join(PROJECT_ROOT_DIR, "kotlin", artifact)
+ target_dir = os.path.join(module_dir, "target")
+ binary_path = os.path.join(target_dir, f"{artifact}-{version}.jar")
+ sources_path = os.path.join(target_dir, f"{artifact}-{version}-sources.jar")
+ javadoc_path = os.path.join(target_dir, f"{artifact}-{version}-javadoc.jar")
+ pom_path = os.path.join(module_dir, "pom.xml")
+ for path in (binary_path, sources_path, javadoc_path, pom_path):
+ if not os.path.exists(path):
+ raise FileNotFoundError(f"Missing Kotlin publication artifact: {path}")
+ ET.parse(pom_path)
+ with zipfile.ZipFile(binary_path) as binary:
+ binary_names = binary.namelist()
+ for required in (
+ "META-INF/LICENSE",
+ "META-INF/NOTICE",
+ "META-INF/DEPENDENCIES",
+ ):
+ if required not in binary_names:
+ raise RuntimeError(f"{binary_path} is missing {required}")
+ if not any(name.endswith(".class") for name in binary_names):
+ raise RuntimeError(f"{binary_path} contains no classes")
+ for name in binary_names:
+ if not name.endswith(".class"):
+ continue
+ class_bytes = binary.read(name)
+ major_version = int.from_bytes(class_bytes[6:8], "big")
+ if major_version != 52:
+ raise RuntimeError(
+ f"{binary_path}!/{name} is JVM class version {major_version}, expected 52"
+ )
+ module_name = KOTLIN_MODULE_NAMES.get(artifact)
+ if module_name:
+ manifest = binary.read("META-INF/MANIFEST.MF").decode("utf-8")
+ if f"Automatic-Module-Name: {module_name}" not in manifest:
+ raise RuntimeError(
+ f"{binary_path} is missing Automatic-Module-Name: {module_name}"
+ )
+ provider = KOTLIN_SERVICE_PROVIDERS.get(artifact)
+ if provider:
+ service_path = (
+ "META-INF/services/"
+ "com.google.devtools.ksp.processing.SymbolProcessorProvider"
+ )
+ if service_path not in binary_names:
+ raise RuntimeError(f"{binary_path} is missing {service_path}")
+ providers = binary.read(service_path).decode("utf-8").splitlines()
+ providers = [line.strip() for line in providers if line.strip()]
+ if providers != [provider]:
+ raise RuntimeError(
+ f"{binary_path}!/{service_path} must contain only {provider}; "
+ f"found {providers}"
+ )
+ with zipfile.ZipFile(sources_path) as sources:
+ source_names = set(sources.namelist())
+ expected_sources = set()
+ for source_root in ("src/main/kotlin", "src/main/java"):
+ root = os.path.join(module_dir, source_root)
+ if not os.path.isdir(root):
+ continue
+ for directory, _, files in os.walk(root):
+ for filename in files:
+ if not filename.endswith((".kt", ".java")):
+ continue
+ expected_sources.add(
+ os.path.relpath(
+ os.path.join(directory, filename), root
+ ).replace(os.sep, "/")
+ )
+ if not expected_sources:
+ raise RuntimeError(
+ f"{module_dir} contains no authored Kotlin or Java sources"
+ )
+ packaged_sources = {
+ name for name in source_names if name.endswith((".kt", ".java"))
+ }
+ if packaged_sources != expected_sources:
+ raise RuntimeError(
+ f"{sources_path} has an incomplete or stale source surface: "
+ f"{sorted(packaged_sources ^ expected_sources)}"
+ )
+ with zipfile.ZipFile(javadoc_path) as javadocs:
+ javadoc_names = javadocs.namelist()
+ if "index.html" not in javadoc_names or not any(
+ name.endswith(".html") and name != "index.html" for name in javadoc_names
+ ):
+ raise RuntimeError(f"{javadoc_path} contains no Kotlin API documentation")
+ logger.info("Verified Kotlin publication artifacts for %s", artifact)
+
+
def _fory_core_jar_path(classifier=None):
version = _read_java_version()
classifier_suffix = f"-{classifier}" if classifier else ""
@@ -511,6 +674,18 @@ def _read_java_version():
return version
+def _read_kotlin_version():
+ pom = os.path.join(PROJECT_ROOT_DIR, "kotlin", "pom.xml")
+ root = ET.parse(pom).getroot()
+ namespace = {"m": "http://maven.apache.org/POM/4.0.0"}
+ artifact = root.findtext("m:artifactId", namespaces=namespace)
+ packaging = root.findtext("m:packaging", namespaces=namespace)
+ version = root.findtext("m:version", namespaces=namespace)
+ if artifact != "fory-kotlin-parent" or packaging != "pom" or not version:
+ raise ValueError("Cannot find kotlin/fory-kotlin-parent version")
+ return version
+
+
def bump_version(**kwargs):
new_version = kwargs["version"]
langs = kwargs["l"]
@@ -696,11 +871,36 @@ def bump_kotlin_version(new_version):
for p in [
"kotlin/fory-kotlin",
"kotlin/fory-kotlin-ksp",
+ "kotlin/fory-json-kotlin",
+ "kotlin/fory-json-kotlin-ksp",
"kotlin/fory-kotlin-tests",
+ "integration_tests/kotlin_json_corpus",
+ "integration_tests/graalvm_kotlin_tests",
"integration_tests/grpc_tests/kotlin",
"integration_tests/idl_tests/kotlin",
]:
_bump_version(p, "pom.xml", new_version, _update_pom_parent_version)
+ for file in ["build.gradle", "README.md"]:
+ _bump_version(
+ "integration_tests/android_tests",
+ file,
+ new_version,
+ _update_android_kotlin_version,
+ )
+ _bump_version(
+ "benchmarks/kotlin",
+ "gradle.properties",
+ new_version,
+ _update_kotlin_benchmark_version,
+ )
+ for path, file in [
+ ("kotlin/fory-json-kotlin", "README.md"),
+ ("kotlin/fory-json-kotlin-ksp", "README.md"),
+ ("docs/json", "kotlin.md"),
+ ("docs/json", "getting-started.md"),
+ ("docs/start", "kotlin.md"),
+ ]:
+ _bump_version(path, file, new_version, _update_release_doc_lines)
def bump_cpp_version(new_version):
@@ -844,6 +1044,24 @@ def _update_android_tests_dependency_version(lines, new_version):
return lines
+def _update_android_kotlin_version(lines, new_version):
+ for index, line in enumerate(lines):
+ lines[index] = re.sub(
+ r"(org\.apache\.fory:(?:fory-json-kotlin(?:-ksp)?|kotlin-json-corpus):)[^'`)\s]+",
+ r"\g<1>" + new_version,
+ line,
+ )
+ return lines
+
+
+def _update_kotlin_benchmark_version(lines, new_version):
+ for index, line in enumerate(lines):
+ if line.startswith("foryVersion="):
+ lines[index] = f"foryVersion={new_version}\n"
+ return lines
+ raise ValueError("No foryVersion entry found in Kotlin benchmark properties")
+
+
def _update_scala_version(lines, v):
v = _normalize_java_version(v)
for index, line in enumerate(lines):
@@ -1435,30 +1653,23 @@ def _parse_args():
default="all",
help="comma separated JVM languages: java,kotlin,scala",
)
- publish_jvm_parser.set_defaults(func=publish_jvm)
-
- publish_java_parser = subparsers.add_parser(
- "publish_java",
- description="Publish Java artifacts",
- )
- publish_java_parser.set_defaults(func=publish_java)
-
- publish_kotlin_parser = subparsers.add_parser(
- "publish_kotlin",
- description="Publish Kotlin artifacts",
+ publish_jvm_parser.add_argument(
+ "--mode",
+ choices=JVM_PUBLICATION_MODES,
+ default="release",
+ help="release stages signed artifacts; snapshot publishes unsigned snapshots",
)
- publish_kotlin_parser.set_defaults(func=publish_kotlin)
+ publish_jvm_parser.set_defaults(func=publish_jvm)
- publish_scala_parser = subparsers.add_parser(
- "publish_scala",
- description="Publish Scala artifacts",
+ verify_kotlin_parser = subparsers.add_parser(
+ "verify_kotlin_artifacts",
+ description="Verify Kotlin binary, source, API documentation, and POM artifacts",
)
- publish_scala_parser.set_defaults(func=publish_scala)
+ verify_kotlin_parser.set_defaults(func=verify_kotlin_artifacts)
args = parser.parse_args()
arg_dict = dict(vars(args))
del arg_dict["func"]
- print(arg_dict)
args.func(**arg_dict)
diff --git a/ci/run_ci.py b/ci/run_ci.py
index f0a84c1b6..4fefc9928 100644
--- a/ci/run_ci.py
+++ b/ci/run_ci.py
@@ -54,23 +54,21 @@ logging.basicConfig(
# USE_PYTHON_RUST - Rust implementation
# USE_PYTHON_JAVASCRIPT - JavaScript implementation
# USE_PYTHON_JAVA - Java implementation
-# USE_PYTHON_KOTLIN - Kotlin implementation
# USE_PYTHON_PYTHON - Python implementation
# USE_PYTHON_GO - Go implementation
# USE_PYTHON_FORMAT - Format implementation
#
# By default, JavaScript, Rust, and C++ use the Python implementation,
-# while Java, Kotlin, Python, Go, and Format use the shell script implementation.
+# while Java, Python, Go, and Format use the shell script implementation.
# Environment variables to control which languages use the Python implementation
# Default to the status quo before migration:
# - JavaScript, Rust, and C++ use Python implementation
-# - Java, Kotlin, Python, Go, and Format use shell script implementation
+# - Java, Python, Go, and Format use shell script implementation
USE_PYTHON_CPP = os.environ.get("USE_PYTHON_CPP", "1") == "1"
USE_PYTHON_RUST = os.environ.get("USE_PYTHON_RUST", "1") == "1"
USE_PYTHON_JAVASCRIPT = os.environ.get("USE_PYTHON_JAVASCRIPT", "1") == "1"
USE_PYTHON_JAVA = os.environ.get("USE_PYTHON_JAVA", "0") == "1"
-USE_PYTHON_KOTLIN = os.environ.get("USE_PYTHON_KOTLIN", "0") == "1"
USE_PYTHON_PYTHON = os.environ.get("USE_PYTHON_PYTHON", "0") == "1"
USE_PYTHON_GO = os.environ.get("USE_PYTHON_GO", "0") == "1"
USE_PYTHON_FORMAT = os.environ.get("USE_PYTHON_FORMAT", "0") == "1"
@@ -324,11 +322,6 @@ def parse_args():
default=None,
help="Java version to use for testing",
)
- java_parser.add_argument(
- "--release",
- action="store_true",
- help="Release to Maven Central",
- )
java_parser.add_argument(
"--install-jdks",
action="store_true",
@@ -348,6 +341,12 @@ def parse_args():
help="Run Kotlin CI",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
+ kotlin_parser.add_argument(
+ "--task",
+ choices=("tests", "install", "install-kotlin", "native-json"),
+ default="tests",
+ help="Kotlin CI task to execute",
+ )
kotlin_parser.set_defaults(func=kotlin.run)
# Python subparser
@@ -407,13 +406,8 @@ def parse_args():
return
# Map Python version argument to shell script command
version = arg_dict.get("version", "17")
- release = arg_dict.get("release", False)
-
- if release:
- logging.info("Release mode requested - using Python implementation")
- func(**arg_dict)
# For windows_java21 on Windows, use the Python implementation directly
- elif version == "windows_java21" and is_windows():
+ if version == "windows_java21" and is_windows():
logging.info(
"Using Python implementation for windows_java21 on Windows"
)
@@ -448,10 +442,7 @@ def parse_args():
else:
run_shell_script("javascript")
elif command == "kotlin":
- if USE_PYTHON_KOTLIN:
- func()
- else:
- run_shell_script("kotlin")
+ func(**arg_dict)
elif command == "python":
if USE_PYTHON_PYTHON:
func()
diff --git a/ci/run_ci.sh b/ci/run_ci.sh
index fe0e9dad9..ebdab570b 100755
--- a/ci/run_ci.sh
+++ b/ci/run_ci.sh
@@ -24,13 +24,12 @@
# USE_PYTHON_RUST=0 # Use shell script for Rust
# USE_PYTHON_JAVASCRIPT=0 # Use shell script for JavaScript
# USE_PYTHON_JAVA=0 # Use shell script for Java
-# USE_PYTHON_KOTLIN=0 # Use shell script for Kotlin
# USE_PYTHON_PYTHON=0 # Use shell script for Python
# USE_PYTHON_GO=0 # Use shell script for Go
# USE_PYTHON_FORMAT=0 # Use shell script for Format
#
# By default, JavaScript, Rust, and C++ use the Python implementation,
-# while Java, Kotlin, Python, Go, and Format use the shell script implementation.
+# while Java, Python, Go, and Format use the shell script implementation.
set -e
set -x
@@ -178,6 +177,7 @@ install_jdk25_fory_artifacts() {
cd "$ROOT"/benchmarks/java
mvn -T10 -B --no-transfer-progress -Pjmh -DskipTests install
unset JDK_JAVA_OPTIONS
+ python "$ROOT/ci/run_ci.py" kotlin --task install-kotlin
echo "Verify JPMS tests on JDK25"
cd "$ROOT"/integration_tests/jpms_tests
mvn -T10 -B --no-transfer-progress clean test
@@ -257,6 +257,7 @@ jdk17_plus_tests() {
fi
if [[ "$java_major" -ge 25 ]]; then
unset JDK_JAVA_OPTIONS
+ python "$ROOT/ci/run_ci.py" kotlin --task install-kotlin
echo "Executing JDK${java_major} JPMS tests"
cd "$ROOT/integration_tests/jpms_tests"
mvn -T10 --batch-mode --no-transfer-progress clean test
@@ -268,37 +269,6 @@ jdk17_plus_tests() {
echo "Executing fory java tests succeeds"
}
-kotlin_tests() {
- echo "Executing fory kotlin tests"
- cd "$ROOT/kotlin"
- set +e
- # The KSP Maven plugin discovers processors from JAR artifacts. Build and install the
- # processor first so the generated-test module does not see the reactor classes directory as
- # its processor artifact.
- mvn -T16 --batch-mode --no-transfer-progress -pl fory-kotlin,fory-kotlin-ksp -am -DskipTests install
- testcode=$?
- if [[ $testcode -ne 0 ]]; then
- exit $testcode
- fi
- java_version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2; exit}')
- if [[ "$java_version" == 1.* ]]; then
- java_major=$(echo "$java_version" | cut -d. -f2)
- else
- java_major=$(echo "$java_version" | cut -d. -f1)
- fi
- if [[ "$java_major" -ge 17 ]]; then
- mvn -T16 --batch-mode --no-transfer-progress test -DfailIfNoTests=false
- else
- echo "Skipping fory-kotlin-tests on JDK < 17 because ksp-maven-plugin requires Java 17+"
- mvn -T16 --batch-mode --no-transfer-progress -pl fory-kotlin,fory-kotlin-ksp -am test -DfailIfNoTests=false
- fi
- testcode=$?
- if [[ $testcode -ne 0 ]]; then
- exit $testcode
- fi
- echo "Executing fory kotlin tests succeeds"
-}
-
windows_java21_test() {
java -version
echo "Executing fory java tests"
@@ -348,9 +318,6 @@ case $1 in
java26)
jdk17_plus_tests
;;
- kotlin)
- kotlin_tests
- ;;
windows_java21)
windows_java21_test
;;
diff --git a/ci/tasks/java.py b/ci/tasks/java.py
index 86bd15ecf..7be7feacc 100644
--- a/ci/tasks/java.py
+++ b/ci/tasks/java.py
@@ -19,7 +19,7 @@ import logging
import os
import subprocess
import re
-from . import common
+from . import common, kotlin
def get_jdk_major_version():
@@ -151,6 +151,7 @@ def install_jdk25_fory_artifacts():
common.exec_cmd("mvn -T10 -B --no-transfer-progress -Pjmh -DskipTests install")
logging.info("Verify JPMS tests on JDK25")
os.environ.pop("JDK_JAVA_OPTIONS", None)
+ kotlin.install_artifacts(include_corpus=True)
common.cd_project_subdir("integration_tests/jpms_tests")
common.exec_cmd("mvn -T10 -B --no-transfer-progress clean test")
finally:
@@ -264,6 +265,7 @@ def run_jdk17_plus(java_version="17"):
common.exec_cmd("mvn -T10 --batch-mode --no-transfer-progress clean install")
os.environ.pop("JDK_JAVA_OPTIONS", None)
logging.info(f"Executing JDK{java_version} JPMS tests")
+ kotlin.install_artifacts(include_corpus=True)
common.cd_project_subdir("integration_tests/jpms_tests")
common.exec_cmd("mvn -T10 --batch-mode --no-transfer-progress clean test")
else:
@@ -393,47 +395,13 @@ def run_graalvm_json_tests():
run_graalvm_tests("org.apache.fory.graalvm.ForyJsonExample")
-def run_release():
- """Release to Maven Central."""
- logging.info("Starting release to Maven Central with Java")
- java_major = get_jdk_major_version()
- if java_major is None or java_major < 25:
- raise RuntimeError(
- "Java releases must run on JDK25+ so MR-JAR entries are packaged"
- )
- common.cd_project_subdir("java")
-
- previous_jdk_options = os.environ.get("JDK_JAVA_OPTIONS")
- os.environ["JDK_JAVA_OPTIONS"] = " ".join(jdk25_javac_options())
- try:
- # Clean and install without tests first
- logging.info("Cleaning and installing dependencies")
- common.exec_cmd("mvn -T10 -B --no-transfer-progress clean install -DskipTests")
-
- # Deploy to Maven Central
- logging.info("Deploying to Maven Central")
- common.exec_cmd(
- "mvn -T10 -B --no-transfer-progress clean deploy -Dgpg.skip -DskipTests -Papache-release"
- )
- finally:
- if previous_jdk_options is None:
- os.environ.pop("JDK_JAVA_OPTIONS", None)
- else:
- os.environ["JDK_JAVA_OPTIONS"] = previous_jdk_options
-
- logging.info("Release to Maven Central completed successfully")
-
-
-def run(version=None, release=False, install_jdks=False, install_fory=False):
+def run(version=None, install_jdks=False, install_fory=False):
"""Run Java CI tasks based on the specified Java version."""
if install_jdks:
globals()["install_jdks"]()
if install_fory:
globals()["install_fory"]()
- if release:
- logging.info("Release mode enabled - will release to Maven Repository")
- run_release()
- elif version == "8":
+ if version == "8":
run_java8()
elif version == "11":
run_java11()
diff --git a/ci/tasks/kotlin.py b/ci/tasks/kotlin.py
index 1e81eed01..c3a6d02c9 100644
--- a/ci/tasks/kotlin.py
+++ b/ci/tasks/kotlin.py
@@ -16,11 +16,34 @@
# under the License.
import logging
+import os
import re
import subprocess
+import xml.etree.ElementTree as ET
+import zipfile
+from pathlib import Path
+
from . import common
+PRODUCTION_MODULES = "fory-kotlin,fory-kotlin-ksp,fory-json-kotlin,fory-json-kotlin-ksp"
+LOW_JDK_MODULES = "fory-kotlin,fory-kotlin-ksp,fory-json-kotlin"
+CORPUS_MODULE_NAME = "org.apache.fory.integration.kotlin.json.corpus"
+CORPUS_PACKAGE = "org.apache.fory.integration.kotlin.json.corpus"
+CORPUS_RULE_MODELS = (
+ "PlatformAccount",
+ "PlatformBox",
+ "PlatformCircle",
+ "PlatformId",
+ "PlatformMarker",
+ "PlatformRoot",
+ "PlatformShape",
+)
+CORPUS_MIXIN_TARGETS = {
+ "PlatformJavaProfileMixin": "PlatformJavaProfile",
+}
+
+
def java_major_version():
"""Return the active Java runtime's major version."""
version_output = subprocess.check_output(
@@ -35,29 +58,170 @@ def java_major_version():
return int(version.split(".")[0])
-def run():
- """Run Kotlin CI tasks."""
- logging.info("Executing fory kotlin tests")
- common.cd_project_subdir("kotlin")
+def install_java_json(include_jpms=False):
+ """Install the Java artifacts consumed by Kotlin JSON modules."""
+ modules = "fory-json,fory-annotation-processor"
+ major = java_major_version()
+ # JDK25+ activates multi-release verifiers whose test-owned main classes and source JAR inputs
+ # must exist even when this installation does not run tests.
+ test_option = "-DskipTests" if major >= 25 else "-Dmaven.test.skip=true"
+ if include_jpms:
+ modules = (
+ "fory-json,fory-format,fory-test-core,fory-testsuite,"
+ "fory-annotation-processor"
+ )
+ # The JDK25 multi-release verifier is test-owned and must still compile.
+ test_option = "-DskipTests"
+ install_options = [test_option, "-Dmaven.javadoc.skip=true"]
+ if major < 25:
+ install_options.append("-Dmaven.source.skip=true")
+ common.cd_project_subdir("java")
+ common.exec_cmd(
+ "mvn -T16 --batch-mode --no-transfer-progress "
+ f"-pl {modules} -am install {' '.join(install_options)}"
+ )
- # The KSP Maven plugin discovers processors from JAR artifacts. Build and install the
- # processor first so the generated-test module does not see the reactor classes directory as
- # its processor artifact.
+
+def install_artifacts(include_corpus=True, modules=PRODUCTION_MODULES):
+ """Install Kotlin production artifacts and the shared JSON corpus."""
+ common.cd_project_subdir("kotlin")
common.exec_cmd(
"mvn -T16 --batch-mode --no-transfer-progress "
- "-pl fory-kotlin,fory-kotlin-ksp -am -DskipTests install"
+ f"-pl {modules} -am clean install -DskipTests "
+ "-Ddokka.skip=true -Dmaven.source.skip=true"
+ )
+ if include_corpus:
+ common.cd_project_subdir("integration_tests/kotlin_json_corpus")
+ common.exec_cmd(
+ "mvn -T16 --batch-mode --no-transfer-progress clean install "
+ "-DskipTests -Ddokka.skip=true -Dmaven.source.skip=true"
+ )
+ verify_corpus_artifact()
+
+
+def verify_corpus_artifact():
+ """Verify exact KSP consumer-rule packaging in the shared platform corpus JAR."""
+ module_dir = Path(
+ common.PROJECT_ROOT_DIR, "integration_tests", "kotlin_json_corpus"
+ )
+ version = _kotlin_version()
+ jar_path = module_dir / "target" / f"kotlin-json-corpus-{version}.jar"
+ if not jar_path.is_file():
+ raise FileNotFoundError(f"Missing Kotlin JSON corpus artifact: {jar_path}")
+
+ expected_rules = {
+ f"META-INF/proguard/fory-json-{CORPUS_PACKAGE}.{model}.pro"
+ for model in CORPUS_RULE_MODELS
+ }
+ expected_rules.update(
+ f"META-INF/proguard/fory-json-mixin-{CORPUS_PACKAGE}.{model}.pro"
+ for model in CORPUS_MIXIN_TARGETS
)
- if java_major_version() >= 17:
+ with zipfile.ZipFile(jar_path) as jar:
+ names = set(jar.namelist())
+ manifest = jar.read("META-INF/MANIFEST.MF").decode("utf-8")
+ expected_module = f"Automatic-Module-Name: {CORPUS_MODULE_NAME}"
+ if expected_module not in manifest:
+ raise RuntimeError(f"{jar_path} is missing {expected_module}")
+ missing = sorted(expected_rules - names)
+ if missing:
+ raise RuntimeError(f"{jar_path} is missing consumer rules: {missing}")
+ actual_rules = {
+ name
+ for name in names
+ if name.startswith("META-INF/proguard/fory-json-") and name.endswith(".pro")
+ }
+ if actual_rules != expected_rules:
+ raise RuntimeError(
+ f"{jar_path} has unexpected consumer rules: "
+ f"{sorted(actual_rules ^ expected_rules)}"
+ )
+ stale_classes = {
+ name
+ for name in names
+ if name.endswith("_ForyJsonCodec.class")
+ or name.endswith("_Operations.class")
+ }
+ if stale_classes:
+ raise RuntimeError(
+ f"{jar_path} contains generated codec classes: {stale_classes}"
+ )
+
+
+def _kotlin_version():
+ pom = Path(common.PROJECT_ROOT_DIR, "kotlin", "pom.xml")
+ root = ET.parse(pom).getroot()
+ namespace = {"m": "http://maven.apache.org/POM/4.0.0"}
+ version = root.findtext("m:version", namespaces=namespace)
+ if not version:
+ raise ValueError(f"Cannot find Kotlin parent version in {pom}")
+ return version
+
+
+def run_tests():
+ """Run the Kotlin JVM matrix for the active JDK."""
+ logging.info("Executing fory kotlin tests")
+ os.environ.setdefault("ENABLE_FORY_DEBUG_OUTPUT", "1")
+ major = java_major_version()
+ install_java_json(include_jpms=major == 25)
+ modules = PRODUCTION_MODULES if major >= 17 else LOW_JDK_MODULES
+ install_artifacts(include_corpus=major >= 17, modules=modules)
+ common.cd_project_subdir("kotlin")
+ if major >= 17:
common.exec_cmd(
"mvn -T16 --batch-mode --no-transfer-progress test -DfailIfNoTests=false"
)
+ common.exec_cmd("mvn -T16 --batch-mode --no-transfer-progress spotless:check")
+ common.cd_project_subdir("integration_tests/kotlin_json_corpus")
+ common.exec_cmd(
+ "mvn -T16 --batch-mode --no-transfer-progress clean test "
+ "-DfailIfNoTests=false"
+ )
else:
logging.info(
- "Skipping fory-kotlin-tests on JDK < 17 because ksp-maven-plugin requires Java 17+"
+ "Skipping KSP generation tests on JDK < 17 because ksp-maven-plugin requires Java 17+"
+ )
+ common.exec_cmd(
+ "mvn -T16 --batch-mode --no-transfer-progress "
+ f"-pl {LOW_JDK_MODULES} -am test -DfailIfNoTests=false"
)
+ if major == 25:
+ common.cd_project_subdir("kotlin")
common.exec_cmd(
"mvn -T16 --batch-mode --no-transfer-progress "
- "-pl fory-kotlin,fory-kotlin-ksp -am test -DfailIfNoTests=false"
+ f"-pl {PRODUCTION_MODULES} -am package -DskipTests "
+ "-Dgpg.skip=true -Papache-release"
)
+ common.cd_project_subdir("")
+ common.exec_cmd("python ci/release.py verify_kotlin_artifacts")
+ common.cd_project_subdir("integration_tests/jpms_tests")
+ common.exec_cmd("mvn -T10 --batch-mode --no-transfer-progress clean test")
logging.info("Executing fory kotlin tests succeeds")
+
+
+def run_native_json():
+ """Build and execute the dedicated Kotlin JSON Native Image fixture."""
+ os.environ.setdefault("ENABLE_FORY_DEBUG_OUTPUT", "1")
+ install_java_json()
+ install_artifacts(include_corpus=True)
+ common.cd_project_subdir("integration_tests/graalvm_kotlin_tests")
+ common.exec_cmd(
+ "mvn --batch-mode --no-transfer-progress -DskipTests=true -Pnative clean package"
+ )
+ common.exec_cmd("./target/main")
+
+
+def run(task="tests"):
+ """Run the selected Kotlin CI task."""
+ if task == "tests":
+ run_tests()
+ elif task == "install":
+ install_java_json()
+ install_artifacts(include_corpus=True)
+ elif task == "install-kotlin":
+ install_artifacts(include_corpus=True)
+ elif task == "native-json":
+ run_native_json()
+ else:
+ raise ValueError(f"Unsupported Kotlin CI task: {task}")
diff --git a/ci/test_release.py b/ci/test_release.py
deleted file mode 100644
index e34809152..000000000
--- a/ci/test_release.py
+++ /dev/null
@@ -1,88 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-
-import unittest
-from unittest import mock
-
-if __package__:
- from . import release
-else:
- import release
-
-
-class ReleaseDocVersionTest(unittest.TestCase):
- def test_updates_release_doc_dependencies(self):
- lines = [
- "python -m pip install pyfory==1.5.0\n",
- 'python -m pip install "pyfory[format]==1.5.0"\n',
- "go get github.com/apache/fory/go/fory@v1.5.0\n",
- "npm install @apache-fory/core@1.5.0 @apache-fory/hps@1.5.0\n",
- "python -m pip install pyfory==1.5.0 grpcio==1.73.0 # Fory 1.4.0\n",
- "go get github.com/apache/fory/go/fory@v1.5.0 # Fory 1.4.0\n",
- "npm install @apache-fory/core@1.5.0 grpc@1.73.0 @apache-fory/hps@1.5.0\n",
- 'fory = "1.5.0"\n',
- " fory: 1.5.0\n",
- ' .package(url: "https://github.com/apache/fory.git", exact: "1.5.0")\n',
- 'implementation("org.apache.fory:fory-core:1.5.0")\n',
- 'libraryDependencies += "org.apache.fory" %% "fory-scala" % "1.5.0"\n',
- 'libraryDependencies += "org.apache.fory" %% "fory-json-scala" % "1.5.0"\n',
- 'implementation("io.grpc:grpc-api:1.73.0")\n',
- "The wire format was introduced in version 1.5.0.\n",
- ]
- expected = [
- "python -m pip install pyfory==1.6.0\n",
- 'python -m pip install "pyfory[format]==1.6.0"\n',
- "go get github.com/apache/fory/go/fory@v1.6.0\n",
- "npm install @apache-fory/core@1.6.0 @apache-fory/hps@1.6.0\n",
- "python -m pip install pyfory==1.6.0 grpcio==1.73.0 # Fory 1.4.0\n",
- "go get github.com/apache/fory/go/fory@v1.6.0 # Fory 1.4.0\n",
- "npm install @apache-fory/core@1.6.0 grpc@1.73.0 @apache-fory/hps@1.6.0\n",
- 'fory = "1.6.0"\n',
- " fory: 1.6.0\n",
- ' .package(url: "https://github.com/apache/fory.git", exact: "1.6.0")\n',
- 'implementation("org.apache.fory:fory-core:1.6.0")\n',
- 'libraryDependencies += "org.apache.fory" %% "fory-scala" % "1.6.0"\n',
- 'libraryDependencies += "org.apache.fory" %% "fory-json-scala" % "1.6.0"\n',
- 'implementation("io.grpc:grpc-api:1.73.0")\n',
- "The wire format was introduced in version 1.5.0.\n",
- ]
-
- updated = release._update_release_doc_lines(lines, "1.6.0")
-
- self.assertEqual(expected, updated)
- self.assertEqual(expected, release._update_release_doc_lines(updated, "1.6.0"))
-
-
-class ScalaReleaseTest(unittest.TestCase):
- @mock.patch.object(release, "_run_release_cmd")
- def test_publishes_each_module(self, run_release_cmd):
- release._publish_scala()
-
- self.assertEqual(
- [
- mock.call("sbt clean", "scala"),
- mock.call("sbt 'project fory-scala' +publishSigned", "scala"),
- mock.call("sbt 'project fory-json-scala' +publishSigned", "scala"),
- mock.call("sbt sonatypePrepare", "scala"),
- mock.call("sbt sonatypeBundleUpload", "scala"),
- ],
- run_release_cmd.call_args_list,
- )
-
-
-if __name__ == "__main__":
- unittest.main()
diff --git a/docs/benchmarks/index.md b/docs/benchmarks/index.md
index 0b38f7ef8..fd940fac3 100644
--- a/docs/benchmarks/index.md
+++ b/docs/benchmarks/index.md
@@ -49,6 +49,14 @@ The Java benchmark section compares Fory against popular Java serialization fram
For additional benchmark notes, raw data, and the complete Java benchmark README, see [Java Benchmarks](object-serialization/native/java/README.md).
+## Kotlin JSON Benchmark
+
+The Kotlin JSON harness compares Fory JSON Kotlin, kotlinx.serialization, Moshi, and Jackson Kotlin
+with one immutable model and 16 isolated String/UTF-8 operations. The
+[Kotlin JSON report](json/kotlin/README.md) is intentionally marked pending until a complete
+measured run is published; no placeholder performance result is inferred from another language or
+project.
+
## Python Benchmark
Fory Python demonstrates strong performance compared to `pickle` and Protobuf across object and list workloads.
diff --git a/docs/benchmarks/json/kotlin/README.md b/docs/benchmarks/json/kotlin/README.md
new file mode 100644
index 000000000..02220e384
--- /dev/null
+++ b/docs/benchmarks/json/kotlin/README.md
@@ -0,0 +1,22 @@
+# Kotlin JSON Benchmark Report
+
+Measured results are pending. This page will be replaced only by a complete run of the repository
+Kotlin JSON benchmark harness; no performance number or chart is inferred from the Java, Scala, or
+historical external benchmark projects.
+
+The source-aligned harness is in
+[`benchmarks/kotlin`](https://github.com/apache/fory/tree/main/benchmarks/kotlin).
+It compares Fory JSON Kotlin, kotlinx.serialization, Moshi, and Jackson Kotlin with:
+
+- one immutable `MediaContent` model with no public zero-argument constructor;
+- the Eishay fixture whose SHA-256 is
+ `8faba2f57ab397f319aced5cf1e8411a76785557d4c7d1703ec9d540354310a1`;
+- String serialization, UTF-8 byte serialization, String deserialization, and UTF-8 byte
+ deserialization for each library;
+- one standard JMH invocation for all 16 methods;
+- retained declared-type serializers, adapters, readers, and writers prepared outside timing;
+- fixture decode, own-output round-trip, and all-output JSON-tree equivalence checks before timing.
+
+The eventual measured report will record the source revision, date, hardware, operating system,
+JDK, Kotlin, JMH, Moshi codegen KSP plugin, library versions, and the exact benchmark settings.
+Standard JMH JSON and the process log remain available together.
diff --git a/docs/index.md b/docs/index.md
index 49ca9e707..e15eb4497 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -31,13 +31,13 @@ rows, standard JSON, schema-driven models, and Fory-backed gRPC services.
## Capabilities
-| Capability | Use it for | Documentation |
-| --------------------- | ---------------------------------------------------- | ----------------------------------------------------- |
-| Object Serialization | Reconstruct xlang or language-native object graphs | [Object Serialization](object-serialization/index.md) |
-| Row Format | Random and partial access to trusted analytical data | [Row Format](row-format/index.md) |
-| Fory JSON | High-throughput standard JSON mapped to Java objects | [Fory JSON](json/index.md) |
-| Fory IDL and compiler | Generate language-native models from a shared schema | [Compiler](compiler/index.md) |
-| Fory gRPC | Generate gRPC companions that marshal Fory models | [Fory gRPC](grpc/index.md) |
+| Capability | Use it for | Documentation |
+| --------------------- | ----------------------------------------------------- | ----------------------------------------------------- |
+| Object Serialization | Reconstruct xlang or language-native object graphs | [Object Serialization](object-serialization/index.md) |
+| Row Format | Random and partial access to trusted analytical data | [Row Format](row-format/index.md) |
+| Fory JSON | High-throughput standard JSON for Java, Kotlin, Scala | [Fory JSON](json/index.md) |
+| Fory IDL and compiler | Generate language-native models from a shared schema | [Compiler](compiler/index.md) |
+| Fory gRPC | Generate gRPC companions that marshal Fory models | [Fory gRPC](grpc/index.md) |
## Language guides
@@ -50,6 +50,9 @@ Binary Object Serialization provides multi-page language guides for
[Dart](object-serialization/dart/index.md), [Scala](object-serialization/scala/index.md), and
[Kotlin](object-serialization/kotlin/index.md).
+Fory JSON has language guides for [Kotlin](json/kotlin.md) and [Scala](json/scala.md) in addition
+to its Java API documentation.
+
## Reference and development
- Wire and type-system details remain in the separate [Specification](specification/xlang_serialization_spec.md) surface.
diff --git a/docs/introduction/choose-a-format.md b/docs/introduction/choose-a-format.md
index f2fb2683c..44bd11761 100644
--- a/docs/introduction/choose-a-format.md
+++ b/docs/introduction/choose-a-format.md
@@ -19,12 +19,12 @@ license: |
limitations under the License.
---
-| Format or mode | Data model | Use it when | Interoperability | Start here |
-| -------------- | ------------------------------------ | ------------------------------------------------------------- | --------------------------------------------------------------------------- | -------------------------------------------------------- |
-| Xlang binary | Portable object graphs | Data crosses language boundaries | Shared wire format across supported Fory implementations | [Cross-language guide](../object-serialization/xlang.md) |
-| Native binary | Language-native object graphs | Producer and consumer use one Fory implementation family | One Fory implementation family only | [Object Serialization](../object-serialization/index.md) |
-| Row Format | Random-access binary rows | You need random field access or analytics-style partial reads | Standard Row is shared by Java, Python, C++, and Rust; Compact is Java-only | [Row Format guide](../row-format/index.md) |
-| Fory JSON | Standard JSON mapped to Java objects | Java applications need high-performance standard JSON | Standard JSON text | [Fory JSON guide](../json/index.md) |
+| Format or mode | Data model | Use it when | Interoperability | Start here |
+| -------------- | ----------------------------------- | ------------------------------------------------------------- | --------------------------------------------------------------------------- | -------------------------------------------------------- |
+| Xlang binary | Portable object graphs | Data crosses language boundaries | Shared wire format across supported Fory implementations | [Cross-language guide](../object-serialization/xlang.md) |
+| Native binary | Language-native object graphs | Producer and consumer use one Fory implementation family | One Fory implementation family only | [Object Serialization](../object-serialization/index.md) |
+| Row Format | Random-access binary rows | You need random field access or analytics-style partial reads | Standard Row is shared by Java, Python, C++, and Rust; Compact is Java-only | [Row Format guide](../row-format/index.md) |
+| Fory JSON | Standard JSON mapped to JVM objects | Java, Kotlin, or Scala applications need standard JSON | Standard JSON text | [Fory JSON guide](../json/index.md) |
Xlang and native are sibling modes of Object Serialization. Use them when the receiver needs to
reconstruct an object graph. Row Format and Fory JSON are separate formats, not additional Object
@@ -61,7 +61,7 @@ coordination across every reader and writer.
Choose xlang or native mode when you need to reconstruct object graphs. Choose Row Format for
trusted analytical data that benefits from random field access. Choose Fory JSON for standard JSON
-in Java applications. Use Fory IDL and the compiler when multiple teams need one schema-first
+in Java, Kotlin, or Scala applications. Use Fory IDL and the compiler when multiple teams need one schema-first
contract; it generates models that use the relevant Fory capability.
## Related capabilities
diff --git a/docs/introduction/support-matrix.md b/docs/introduction/support-matrix.md
index 953619cd9..16e815144 100644
--- a/docs/introduction/support-matrix.md
+++ b/docs/introduction/support-matrix.md
@@ -28,7 +28,7 @@ does not imply support for every Fory capability.
| Native object serialization | Java, Python, C++, Go, Rust, Scala, Kotlin | One Fory implementation family only |
| Standard Row Format | Java, Python, C++, Rust | Shared Standard Row layout |
| Compact Row Format | Java | Java-only compact layout |
-| Fory JSON | Java | Standard JSON text |
+| Fory JSON | Java, Kotlin, Scala | Standard JSON text |
| Fory compiler output | Java, Python, C++, Go, Rust, JavaScript/TypeScript, C#, Swift, Dart, Scala, Kotlin | Generated models use supported Fory APIs |
| Fory gRPC | Java, Python, C++, Go, Rust, JavaScript/TypeScript, C#, Dart, Scala, Kotlin | Peers must use matching generated Fory service contracts |
diff --git a/docs/json/android.md b/docs/json/android.md
index 33bea7ad0..e0277f884 100644
--- a/docs/json/android.md
+++ b/docs/json/android.md
@@ -1,6 +1,6 @@
---
title: Android
-sidebar_position: 8
+sidebar_position: 9
id: android
license: |
Licensed to the Apache Software Foundation (ASF) under one or more
@@ -23,6 +23,9 @@ Fory JSON supports ordinary classes on Android API level 26 and later through th
`fory-json` artifact. Runtime JSON code generation and asynchronous compilation are disabled
automatically, so `ForyJson.builder().build()` uses the interpreted object mapper.
+Kotlin applications use the ordinary `fory-json-kotlin` runtime, which reads Kotlin/JVM metadata
+directly. KSP is needed only when R8 or ProGuard can rename or remove Kotlin model members.
+
## Installation and Codec Model
Add Fory JSON to the application:
@@ -33,6 +36,36 @@ dependencies {
}
```
+For Kotlin, apply Kotlin 2.3.20 and add the Kotlin JSON runtime:
+
+```kotlin
+plugins {
+ kotlin("android") version "2.3.20"
+}
+
+dependencies {
+ implementation("org.apache.fory:fory-json-kotlin:${foryVersion}")
+}
+```
+
+If the application enables R8 or ProGuard, also apply KSP 2.3.8 and add the retention-rule
+processor:
+
+```kotlin
+plugins {
+ id("com.google.devtools.ksp") version "2.3.8"
+}
+
+dependencies {
+ ksp("org.apache.fory:fory-json-kotlin-ksp:${foryVersion}")
+}
+```
+
+Create the runtime with `ForyJsonKotlin.builder()`. For a minified build, annotate every required
+Kotlin source model with `@JsonType`. For a third-party Kotlin target, declare a source-owned exact
+`@JsonMixin` instead. KSP emits exact R8 and ProGuard retention resources; it does not generate
+codecs or construction operations. Ensure those resources are packaged in the final application.
+
## Custom Codecs
`@JsonCodec` has the same declaration behavior on Android and the JVM. It supports complete values,
@@ -60,7 +93,7 @@ Child codecs act on one direct level only. For example, `elementCodec` on `Money
`Money[]`, and `elementCodec` on `AtomicReferenceArray<Money>` handles each `Money`. Use a complete
`value` codec when deeper custom behavior is required.
-## Generated Access and R8 Rules
+## Java Generated Access and R8 Rules
Add the annotation processor and mark application object models with `JsonType` to generate direct
field, getter, setter, Record constructor, `JsonCreator`, and `JsonValidator` operations together
@@ -108,19 +141,19 @@ ForyJson json =
ForyJson.builder().registerMixin(ThirdPartyInvoiceMixin.class).build();
```
-Compile every non-empty Mixin source with `fory-annotation-processor`. The processor emits exact
-R8 rules and any pair-specific target operations that the built `ForyJson` instance can use. Registered codecs,
-effective type codecs, and built-in mappings keep their normal codec-selection precedence. An empty Mixin
-produces no generated output.
+Compile a non-empty Java-source Mixin for a Java target with `fory-annotation-processor`. The
+processor emits exact R8 rules and any pair-specific target operations that the built `ForyJson`
+instance can use. Registered codecs, effective type codecs, and built-in mappings keep their normal
+codec-selection precedence. An empty Mixin produces no generated output.
A Mixin may place `JsonValidator` on a public abstract zero-argument `void` method that exactly
matches a public target method. The generated pair calls that target method directly. The target
does not need `JsonType` solely for a Mixin validator.
The target does not need `JsonType` merely because it has a Mixin. `JsonMixin` is itself the
-processor entry point for the pair. If a target also uses `JsonType`, the built `ForyJson` instance selects the
-pair-specific companion for a non-empty registered Mixin instead of combining the overlay with the
-target's direct companion.
+processor entry point for the pair. If a target also uses `JsonType`, the built `ForyJson` instance
+selects the pair-specific generated Java operations for a non-empty registered Mixin instead of
+combining the overlay with the target's directly generated operations.
Only one source is enabled for an exact target in one built `ForyJson` instance. A later registration for that
target replaces an earlier registration on the builder, and `build()` snapshots the selected
@@ -129,6 +162,9 @@ only the last registered source.
Use the processor-generated R8 rules for non-empty Mixins instead of broad package keep rules.
+If either the Mixin source or its exact target is Kotlin, process the source request with
+`fory-json-kotlin-ksp` instead. KSP emits only the exact retention rules for the target and Mixin.
+
## Reflection-Based Models
Ordinary non-Record classes that omit `JsonType` can supply equivalent exact rules themselves unless
@@ -154,13 +190,21 @@ validator:
The same exact-rule approach supports every `JsonCodec` member; it is not limited to complete-value
codecs. `JsonType` is not required for codec selection on an ordinary class.
-For `@JsonType` models, generated operations and R8 rules also cover effective `JsonValidator`
-methods, `JsonValue` fields and effective methods, fixed `JsonRawValue` and `JsonBase64` fields and
-getters, `JsonFormat` date/time fields, their runtime annotations, and the Base64 codec constructor.
-Without `@JsonType`, the value, raw, Base64, format, and codec annotations still work through
-reflection, but a release-minified application must keep the exact annotated members, annotation
-attributes, and codec constructor itself. A `JsonValue` method may use a non-JavaBean name, so its
-manual rule must name that method explicitly. `JsonFormat` keeps the same direct-field and
+This reflection-based section applies to Java models. Kotlin immutable models use validated
+Kotlin/JVM metadata on both the JVM and Android. For a minified Android build, use the Kotlin KSP
+processor to emit exact rules instead of writing broad package keep rules.
+
+For Java `@JsonType` models, generated operations and R8 rules also cover effective
+`JsonValidator` methods, `JsonValue` fields and effective methods, fixed `JsonRawValue` and
+`JsonBase64` fields and getters, `JsonFormat` date/time fields, their runtime annotations, and the
+Base64 codec constructor. Without `@JsonType`, those annotations still work through reflection,
+but a release-minified application must keep the exact annotated members, annotation attributes,
+and codec constructor itself. A `JsonValue` method may use a non-JavaBean name, so its manual rule
+must name that method explicitly.
+
+Kotlin models use the same effective annotations through metadata. The KSP retention rules preserve
+the corresponding constructors, accessors, fields, annotations, generic signatures, and compiler
+default operations in a minified build. `JsonFormat` keeps the same direct-field and
one-wrapper-level behavior as on the JVM, including `timezone` for `Instant`, `ZonedDateTime`, and
`OffsetDateTime`.
@@ -170,15 +214,16 @@ classes follow the normal creator rules instead. Retain every field and method u
or use an application codec when a model cannot satisfy those requirements. `JsonUnwrapped`
supports mutable classes, creator-backed classes, and Records through their normal property and
construction paths. When the containing model and its unwrapped children use `JsonType`, their
-generated companions supply those operations.
+generated Java operations or Kotlin metadata supply those operations. A minified Kotlin build must
+also package the exact KSP retention rules for every Kotlin model in the path.
## Records
Android-desugared Records require processor-generated operations from either a direct `@JsonType`
declaration or a compiled exact `@JsonMixin` pair. Manual R8 rules alone cannot reconstruct Record
component order because Android does not provide the Java Record reflection APIs. This also applies
-to a Record whose complete representation is a `JsonValue` String: the generated companion
-identifies the propagated component accessor and calls an annotated one-String canonical
-constructor directly. Generated child codecs act on one level exactly as they do on the JVM. Every
-Record in a `JsonUnwrapped` path needs its own direct `JsonType` declaration or compiled exact
-`JsonMixin` pair. Use a complete value codec for deeper nested behavior.
+to a Record whose complete representation is a `JsonValue` String: the generated Java operations
+identify the propagated component accessor and call an annotated one-String canonical constructor
+directly. Generated child codecs act on one level exactly as they do on the JVM. Every Record in a
+`JsonUnwrapped` path needs its own direct `JsonType` declaration or compiled exact `JsonMixin` pair.
+Use a complete value codec for deeper nested behavior.
diff --git a/docs/json/annotations.md b/docs/json/annotations.md
index 9b91c3d89..68f7e00dd 100644
--- a/docs/json/annotations.md
+++ b/docs/json/annotations.md
@@ -23,21 +23,51 @@ Fory JSON provides these mapping and validation annotations in
`org.apache.fory.json.annotation`:
`JsonAnyGetter`, `JsonAnyProperty`, `JsonAnySetter`, `JsonBase64`, `JsonCodec`, `JsonCreator`, `JsonFormat`,
`JsonIgnore`, `JsonProperty`, `JsonPropertyOrder`, `JsonRawValue`, `JsonSubTypes`, `JsonUnwrapped`,
-`JsonValidator`, and `JsonValue`. `JsonType` is a separate build-time generation marker. They are
+`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.
-`JsonType` asks the annotation processor to generate direct property and creator operations plus
-exact retention rules on the JVM and Android. It is not inherited, so annotate each eligible
-concrete model that needs a generated companion on those platforms. A directly annotated
-`JsonValue` Record also receives a companion for its value accessor and canonical constructor.
-Ordinary unannotated classes may still use reflection; on Android they need application-authored
-exact R8 rules. Android-desugared Records require processor-generated operations from either a
-direct `JsonType` declaration or a compiled exact `JsonMixin` pair. Outside Native Image, a
-directly annotated model that uses the default object codec fails during codec creation if its
-generated companion is missing. GraalVM Native Image discovers `JsonType` directly and does not use
-annotation-processor output.
-See the [GraalVM guide](graalvm.md) and
-[Android guide](android.md) for the platform workflows.
+`JsonType` is not inherited, so mark each eligible concrete model that must participate in a
+platform build workflow. For Java source, the Fory annotation processor generates direct property
+and creator operations plus exact retention rules on the JVM and Android. A directly annotated
+`JsonValue` Record also receives generated value-access and canonical-constructor operations.
+Ordinary unannotated Java classes may still use reflection; on Android they need
+application-authored exact R8 rules. Android-desugared Records require processor-generated
+operations from either a direct `JsonType` declaration or a compiled exact `JsonMixin` pair.
+Outside Native Image, a directly annotated Java model that uses the default object codec fails
+during codec creation if its generated Java operations are missing.
+
+Kotlin/JVM models are mapped from validated Kotlin metadata and do not require generated
+construction operations. In Android builds that use R8 or ProGuard, Kotlin KSP emits exact
+retention rules for Kotlin `@JsonType` models. It also processes an exact Mixin declared in
+application source when either the Mixin or its exact target is Kotlin. KSP does not generate codecs
+or construction operations. GraalVM Native Image discovers reachable Java and Kotlin `JsonType`
+declarations directly, and provider-selected configurations generate codecs while the image is
+built. See the [GraalVM guide](graalvm.md) and [Android guide](android.md) for the platform workflows.
+
+## Kotlin use-site targets
+
+Kotlin annotations merge into the same logical property as their Java field, accessor, or selected
+constructor parameter. Use explicit targets so behavior does not depend on Kotlin's default-target
+policy:
+
+| Kotlin site | Logical declaration |
+| ------------ | ---------------------------------------------------------- |
+| `@field:` | backing field |
+| `@get:` | getter |
+| `@set:` | setter |
+| `@param:` | selected constructor parameter |
+| `@setparam:` | setter value parameter for supported parameter annotations |
+
+`@property:` is unsupported because Fory JSON annotations do not target Kotlin-only property
+metadata. `@setparam:JsonProperty` is rejected because setter-parameter naming is not a JSON
+property-name contract. `@setparam:JsonIgnore`, `@setparam:JsonCodec`, and
+`@setparam:JsonUnwrapped` apply to the exact one-argument setter property. An effective
+`@set:JsonCodec` is also supported directly.
+
+`JsonProperty` members merge individually when their explicit values agree; conflicting names,
+indexes, or inclusion policies fail. `JsonIgnore` read/write directions merge monotonically, and
+repeated `JsonCodec` declarations must be identical. Mixin replacement or removal happens before
+this merge. See [Kotlin](kotlin.md#annotations-and-use-site-targets) for an idiomatic example.
## Mixins
@@ -96,9 +126,10 @@ A `JsonCodec` supplied by a Mixin is the target's effective annotation. An exact
`registerCodec` registration still wins, while the effective type annotation wins over a built-in
mapping.
-On Android, compile non-empty Mixins with the Fory annotation processor so required generated
-operations and platform configuration are available. GraalVM Native Image discovers reachable
-Mixins directly. See the platform guides linked above.
+On Android, compile a Java-source Mixin for a Java target with the Fory annotation processor. If
+either the Mixin source or its exact target is Kotlin, apply the Kotlin KSP processor instead so the
+pair's exact retention rules are packaged. GraalVM Native Image discovers reachable Mixins
+directly. See the platform guides linked above.
## `JsonProperty`
diff --git a/docs/json/custom-codecs.md b/docs/json/custom-codecs.md
index f8d1dbfe3..bd4ed8502 100644
--- a/docs/json/custom-codecs.md
+++ b/docs/json/custom-codecs.md
@@ -72,6 +72,26 @@ ForyJson json =
.build();
```
+Use `JsonCodecFactory` when one factory owns a family of declared or parameterized types:
+
+```java
+import org.apache.fory.json.JsonCodecFactory;
+
+JsonCodecFactory factory =
+ (type, resolver, runtimeType) ->
+ type.getRawType() == Money.class ? new MoneyCodec() : null;
+
+ForyJson json =
+ ForyJson.builder()
+ .withModule(context -> context.registerCodecFactory(factory))
+ .build();
+```
+
+`runtimeType` is `true` only when the factory is selecting a codec for the actual class of a value
+during a dynamic write. Declared roots and composite child types receive `false`. A composite codec
+that needs this distinction after construction must retain the flag for its later `resolveTypes`
+call; it must not infer the value from resolver state.
+
The containing property still controls its name, ignore direction, and null-inclusion policy. If a
null property is omitted, the value codec is not called. If the property is emitted, or the value
is an array element, collection element, map value, Optional value, or atomic-reference value, the
@@ -179,6 +199,31 @@ The child members have these meanings:
| `keyCodec` | `Map<K, V>` | JSON member name for `K` |
| `valueCodec` | `Map<K, V>` | direct `V` value |
+### Kotlin occurrences
+
+Kotlin uses the same codec registrations and `JsonCodec` annotation. Apply property annotations to
+an explicit supported use site, for example:
+
+```kotlin
+import org.apache.fory.json.annotation.JsonCodec
+
+data class Ledger(
+ @field:JsonCodec(value = MoneyCodec::class)
+ val total: Money,
+ @field:JsonCodec(elementCodec = MoneyCodec::class)
+ val entries: List<Money>,
+)
+```
+
+The complete-value codec owns the whole JSON value. Child codecs leave the standard array,
+collection, Optional/atomic, or map representation in control. Kotlin nullability is still the
+declared occurrence contract around a selected application codec: after a non-null JSON token, the
+codec must return the exact declared type and must not return null for a non-null occurrence.
+
+An unsigned or eligible value-class map key can use the built-in member-name mapping without an
+annotation. Use an explicit `keyCodec` or whole-map codec when the key has a different tagged text
+shape. An exact application registration still takes precedence over the Kotlin module defaults.
+
A custom Map-key codec converts between the declared key and a JSON member name:
```java
diff --git a/docs/json/getting-started.md b/docs/json/getting-started.md
index bd38a396f..64f02ead3 100644
--- a/docs/json/getting-started.md
+++ b/docs/json/getting-started.md
@@ -24,25 +24,68 @@ license: |
Fory JSON supports Java 8 and later on standard JDKs, GraalVM native images, and Android. Java
records are supported on Java 17 and later.
-Fory JSON is available from Maven Central.
+Released Fory JSON artifacts are available from Maven Central, and development snapshots are
+available from the Apache snapshot repository. The repository declarations below support either
+form. Keep every Fory module on the same version shown in its coordinates.
Maven:
```xml
+<repositories>
+ <repository>
+ <id>apache-snapshots</id>
+ <url>https://repository.apache.org/snapshots/</url>
+ <releases><enabled>false</enabled></releases>
+ <snapshots><enabled>true</enabled></snapshots>
+ </repository>
+</repositories>
+
<dependency>
<groupId>org.apache.fory</groupId>
<artifactId>fory-json</artifactId>
- <version>1.6.1</version>
+ <version>1.7.0-SNAPSHOT</version>
</dependency>
```
Gradle:
```kotlin
-implementation("org.apache.fory:fory-json:1.6.1")
+repositories {
+ maven("https://repository.apache.org/snapshots/") {
+ mavenContent { snapshotsOnly() }
+ }
+ mavenCentral()
+}
+
+implementation("org.apache.fory:fory-json:1.7.0-SNAPSHOT")
+```
+
+### Kotlin
+
+Kotlin/JVM applications add the optional Kotlin JSON runtime and use its single builder entry:
+
+```kotlin title="build.gradle.kts"
+dependencies {
+ implementation("org.apache.fory:fory-json-kotlin:1.7.0-SNAPSHOT")
+}
+```
+
+```kotlin
+import org.apache.fory.json.kotlin.ForyJsonKotlin
+import org.apache.fory.json.kotlin.jsonTypeRef
+
+data class User(val id: Long, val name: String)
+
+val json = ForyJsonKotlin.builder().build()
+val userType = jsonTypeRef<User>()
+val text = json.toJson(User(7, "Alice"), userType)
+val decoded = json.fromJson(text, userType)
```
-Use the same version for every Fory module in one application.
+The runtime reads Kotlin/JVM metadata directly. Add `fory-json-kotlin-ksp` only to Android builds
+that use R8 or ProGuard; it emits exact retention rules for Kotlin `@JsonType` models and
+source-owned exact Mixins. GraalVM Native Image uses the normal `@ForyJsonProvider` workflow. The
+complete setup and Kotlin type behavior are in the [Kotlin JSON guide](kotlin.md).
### JDK 25 and later
@@ -61,6 +104,7 @@ For a module-path application:
```
The JPMS module name of Fory JSON is `org.apache.fory.json`.
+The Kotlin integration module name is `org.apache.fory.json.kotlin`.
## Quick start
diff --git a/docs/json/graalvm.md b/docs/json/graalvm.md
index f4d96d0cc..e3b240ebd 100644
--- a/docs/json/graalvm.md
+++ b/docs/json/graalvm.md
@@ -1,6 +1,6 @@
---
title: GraalVM Native Image
-sidebar_position: 9
+sidebar_position: 10
id: graalvm
license: |
Licensed to the Apache Software Foundation (ASF) under one or more
@@ -21,8 +21,9 @@ license: |
## Reachable Models
-Fory JSON has its own Native Image Feature and does not use the Fory annotation processor. Add
-`@JsonType` to each reachable concrete object model that the native executable reads or writes:
+Fory JSON has one Native Image Feature. Java models are discovered from reachable annotations; the
+Feature does not use the Java annotation processor. Add `@JsonType` to each reachable concrete
+Java object model that the native executable reads or writes:
```java
import org.apache.fory.json.ForyJson;
@@ -95,10 +96,40 @@ needed, and the provider package does not need to be exported or opened to Fory.
methods and fields are not supported.
Only configurations returned by a provider receive generated codecs. The default configuration is
-not generated implicitly. If a codegen-enabled `ForyJson` configuration was not included, Fory JSON
-uses its prepared interpreted codecs and logs one process-wide warning recommending a reachable
-`@ForyJsonProvider`. `withCodegen(false)` explicitly selects interpreted codecs and does not request
-generated-codec lookup. Asynchronous compilation is disabled in a native executable.
+not generated implicitly. If a codegen-enabled `ForyJson` configuration was not included, ordinary
+Java models and complete value codecs use their prepared interpreted codecs, and Fory JSON logs one
+process-wide warning recommending a reachable `@ForyJsonProvider`. Language-module object models
+that require hosted capabilities fail before reading or writing a value. `withCodegen(false)`
+explicitly selects interpreted codecs and does not request generated-codec lookup. Asynchronous
+compilation is disabled in a native executable.
+
+### Kotlin configurations
+
+Kotlin Native Image support uses the same Feature and provider API. Add the Kotlin runtime, then
+return a codegen-enabled configuration that installs `ForyJsonKotlin`:
+
+```kotlin
+import org.apache.fory.json.ForyJson
+import org.apache.fory.json.annotation.ForyJsonProvider
+import org.apache.fory.json.kotlin.ForyJsonKotlin
+
+@ForyJsonProvider
+class JsonConfigs {
+ fun api(): ForyJson = ForyJsonKotlin.builder().build()
+}
+```
+
+Annotate each reachable concrete Kotlin model with `@JsonType`, or register an exact reachable
+Mixin for a third-party target. Fory reads and validates Kotlin metadata while building the image,
+then generates the provider-selected codecs. A provider configuration with disabled code
+generation or an unsupported metadata ABI fails image construction. A Kotlin-enabled runtime
+configuration that was not returned by a provider fails before it reads or writes a Kotlin object;
+it never falls back to reflective construction.
+
+An exact generic Kotlin root is available only when its complete binding is reached through a
+property, constructor argument, container/map child, or closed subtype of a provider-selected
+concrete root. Keep using `jsonTypeRef<T>()` at the direct root call; no public root registry or
+reflection configuration is needed.
## Mixins
@@ -146,7 +177,8 @@ by a class name resolved at runtime is not reachable;
`JsonSubTypes.Type.className` is therefore unsupported in a native image.
Do not add application reflection configuration as a replacement for the generated configuration.
-The native executable resolves the same effective annotations as the JVM.
+The native executable resolves the same effective annotations as the JVM. Kotlin applications use
+the provider workflow above and must also avoid package-wide opens or reflection configuration.
## Annotations and Custom Codecs
diff --git a/docs/json/index.md b/docs/json/index.md
index fe8438ac2..97a436f06 100644
--- a/docs/json/index.md
+++ b/docs/json/index.md
@@ -19,9 +19,10 @@ license: |
limitations under the License.
---
-Fory JSON is Apache Fory's thread-safe JSON codec for Java and Scala. It provides interpreted and
-runtime-generated codecs for Java objects, records, immutable creator-based classes, common JDK
-types, generic containers, Scala models and collections, and custom complete-value codecs.
+Fory JSON is Apache Fory's thread-safe JSON codec for Java, Kotlin, and Scala. It provides
+interpreted and generated codecs for Java objects, records, immutable creator-based classes,
+common JDK types, generic containers, Kotlin models and semantic types, Scala models and
+collections, and custom complete-value codecs.
Fory JSON is a separate data format from Fory's binary native and xlang protocols. Use it when a
system must exchange ordinary JSON with browsers, APIs, logs, configuration, or another JSON
@@ -30,18 +31,19 @@ reference identity, circular graphs, or Fory's binary-only features.
## Documentation map
-| Goal | Page |
-| -------------------------------------------------------------- | ------------------------------------- |
-| First runnable JSON round trip | [Getting Started](getting-started.md) |
-| Understand Java object mapping and configuration | [Object Mapping](object-mapping.md) |
-| Configure properties, creators, values, validators, and mixins | [Annotations](annotations.md) |
-| Extend complete values, children, and map keys | [Custom Codecs](custom-codecs.md) |
-| Package and distribute reusable JSON extensions | [Modules](modules.md) |
-| Use case classes, Scala collections, and Scala enums | [Scala](scala.md) |
-| Deploy on Android | [Android](android.md) |
-| Build a GraalVM native image | [GraalVM Native Image](graalvm.md) |
-| Decode input safely | [Security](security.md) |
-| Diagnose failures | [Troubleshooting](troubleshooting.md) |
+| Goal | Page |
+| --------------------------------------------------------------- | ------------------------------------- |
+| First runnable JSON round trip | [Getting Started](getting-started.md) |
+| Understand Java object mapping and configuration | [Object Mapping](object-mapping.md) |
+| Configure properties, creators, values, validators, and mixins | [Annotations](annotations.md) |
+| Extend complete values, children, and map keys | [Custom Codecs](custom-codecs.md) |
+| Package and distribute reusable JSON extensions | [Modules](modules.md) |
+| Use data classes, Kotlin nullability, defaults, and value types | [Kotlin](kotlin.md) |
+| Use case classes, Scala collections, and Scala enums | [Scala](scala.md) |
+| Deploy on Android | [Android](android.md) |
+| Build a GraalVM native image | [GraalVM Native Image](graalvm.md) |
+| Decode input safely | [Security](security.md) |
+| Diagnose failures | [Troubleshooting](troubleshooting.md) |
## Performance
diff --git a/docs/json/kotlin.md b/docs/json/kotlin.md
new file mode 100644
index 000000000..96e75d3b7
--- /dev/null
+++ b/docs/json/kotlin.md
@@ -0,0 +1,364 @@
+---
+title: Kotlin
+sidebar_position: 8
+id: kotlin
+license: |
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+---
+
+Fory JSON Kotlin maps Kotlin/JVM types to ordinary JSON while preserving Kotlin constructor
+defaults, nullability, value types, and generic arguments. It is an optional module layered on
+Fory JSON; it does not change Fory's binary protocols.
+
+## Installation
+
+The runtime supports Kotlin/JVM metadata ABI 2.3 and is built with Kotlin 2.3.20. Use the same Fory
+version for every module:
+
+```kotlin title="build.gradle.kts"
+plugins {
+ kotlin("jvm") version "2.3.20"
+}
+
+repositories {
+ maven("https://repository.apache.org/snapshots/") {
+ mavenContent { snapshotsOnly() }
+ }
+ mavenCentral()
+}
+
+dependencies {
+ implementation("org.apache.fory:fory-json-kotlin:1.7.0-SNAPSHOT")
+}
+```
+
+The runtime reads Kotlin/JVM metadata directly; do not add `kotlin-reflect`. KSP is not required for
+JVM applications or unminified Android builds. Android applications that use R8 or ProGuard should
+also apply KSP and add the retention-rule processor:
+
+```kotlin title="build.gradle.kts"
+plugins {
+ id("com.google.devtools.ksp") version "2.3.8"
+}
+
+dependencies {
+ ksp("org.apache.fory:fory-json-kotlin-ksp:1.7.0-SNAPSHOT")
+}
+```
+
+Annotate every Kotlin source model that needs exact minification retention with `@JsonType`. For a
+third-party target, declare an exact `@JsonMixin` in application source instead. Use Kotlin KSP for
+a Mixin when either the Mixin or its exact target is Kotlin. The processor emits only the exact R8
+and ProGuard rules for those declarations; it does not generate codecs or change the JSON mapping.
+
+## Quick start
+
+Use `ForyJsonKotlin.builder()` to install the Kotlin module. Retain a `jsonTypeRef<T>()` for every
+declared Kotlin root whose nullability, unsigned identity, value-class identity, or generic
+arguments matter:
+
+```kotlin
+import org.apache.fory.json.kotlin.ForyJsonKotlin
+import org.apache.fory.json.kotlin.jsonTypeRef
+
+data class Account(
+ val id: ULong,
+ val name: String,
+ val nickname: String? = null,
+)
+
+val json = ForyJsonKotlin.builder().build()
+val accountType = jsonTypeRef<Account>()
+
+val text = json.toJson(Account(7u, "Alice"), accountType)
+val decoded = json.fromJson(text, accountType)
+```
+
+`jsonTypeRef<T>()` is a type token, not a codec lookup. Construct it once and reuse it. A Java
+`Class` or ordinary Java `TypeRef` cannot express distinctions such as `List<Account?>`, `UInt`, or
+a logical value class lowered to a primitive carrier.
+
+The builder can also install the module explicitly:
+
+```kotlin
+import org.apache.fory.json.ForyJson
+import org.apache.fory.json.kotlin.ForyJsonKotlin
+
+val json = ForyJson.builder().withModule(ForyJsonKotlin).build()
+```
+
+There is no automatic classpath installation or Kotlin-specific encode/decode alias.
+
+## Immutable classes and compiler defaults
+
+An ordinary or data class is mapped as a named JSON object. Fory selects one valid public Kotlin
+constructor and reconstructible properties; `copy` and `componentN` functions do not define the
+schema. A compiler-generated default is used only when its JSON member is absent:
+
+```kotlin
+data class Request(
+ val id: Long,
+ val label: String? = "new",
+ val retries: Int = 3,
+)
+```
+
+The primary constructor is automatic. A public secondary constructor or target-owned public static
+factory is used only when explicitly selected by `JsonCreator`; a companion factory qualifies only
+through its real outer-class `@JvmStatic` bridge. `@JvmOverloads` artifacts are not separate
+creator candidates, and a selected static factory cannot have compiler-default parameters.
+Private/protected, vararg, executable-generic, context-parameter, local,
+anonymous, `inner`, or synthetic construction requires an exact application codec.
+
+For this model:
+
+- `{"id":1}` invokes both compiler defaults.
+- `{"id":1,"label":null}` passes an explicit null and does not invoke the `label` default.
+- a missing `id` fails before constructor invocation.
+- `{"id":1,"retries":null}` fails; null never asks Kotlin to use a default.
+
+Normal body `var` properties preserve their initializer when absent and are assigned after
+construction when present. A `lateinit` property is required. Automatic creator and deferred
+properties must be reconstructible in both read and write directions.
+
+A body `val`, computed or delegated property, getter-only property, or delegated `var` must be
+ignored or handled by an exact custom codec. Present deferred setters run in fixed property order
+after construction, followed by validators; input member order does not choose application call
+order. Kotlin class instances are always constructed normally, so primary-constructor initialization and
+validation are not bypassed.
+
+Fory must be able to read its own output under the same configuration. Consequently, nullable
+constructor parameters and nullable deferred properties are emitted explicitly when null, even
+when the builder's general Java default is to omit null fields. An explicit
+`JsonProperty.Include.NON_NULL` on such a property is rejected if omission could fail or invoke a
+different compiler default.
+
+## Nullability
+
+Kotlin occurrence nullability is enforced at roots, properties, container elements, map values,
+and generic children:
+
+| Declaration | Missing member | Explicit JSON `null` |
+| --------------------------------- | --------------------- | -------------------- |
+| `val value: String` | Fails | Fails |
+| `val value: String?` | Fails | Passes null |
+| `val value: String = expression` | Evaluates the default | Fails |
+| `val value: String? = expression` | Evaluates the default | Passes null |
+
+`List<String?>` accepts null elements; `List<String>` rejects them. Map keys must be non-null.
+Platform/unknown nullability is not guessed for automatic Kotlin construction.
+
+Transparent wrappers must have one unambiguous null meaning. For example, non-null
+`Optional<T>` uses JSON null for `Optional.empty()`, so `Optional<T>?` and `Optional<T?>` are
+rejected. `AtomicReference<T>` and value classes follow the same injective-shape rule. Use an exact
+custom tagged codec when two logical states would otherwise share JSON null.
+
+An application codec selected by registration or `@JsonCodec` remains trusted application code.
+For a non-null JSON token, it must return the exact declared type and honor Kotlin occurrence
+nullability.
+
+## Annotations and use-site targets
+
+Kotlin annotations feed the same logical-property merge used by Java. Prefer explicit use-site
+targets:
+
+```kotlin
+import org.apache.fory.json.annotation.JsonCodec
+import org.apache.fory.json.annotation.JsonIgnore
+import org.apache.fory.json.annotation.JsonProperty
+import org.apache.fory.json.annotation.JsonType
+
+@JsonType
+data class Profile(
+ @param:JsonProperty("user_id")
+ val id: Long,
+
+ @field:JsonIgnore
+ val localCacheKey: String? = null,
+
+ @field:JsonProperty(include = JsonProperty.Include.ALWAYS)
+ val nickname: String? = null,
+
+ @field:JsonCodec(elementCodec = AccountIdCodec::class)
+ val accountIds: List<AccountId>,
+)
+```
+
+Use explicit JVM use-site targets as shown so behavior does not depend on Kotlin's default-target
+policy. See [Annotations](annotations.md#kotlin-use-site-targets) for the complete supported-target
+and conflict rules, and [Custom Codecs](custom-codecs.md) for complete-value, element, content, key,
+and map-value codecs.
+
+## Generics and collections
+
+Use complete declared types:
+
+```kotlin
+import org.apache.fory.json.kotlin.jsonTypeRef
+
+val accountsType = jsonTypeRef<List<Account?>>()
+val accounts = json.fromJson("""[null,{"id":7,"name":"Alice"}]""", accountsType)
+```
+
+Kotlin raw strings can be passed directly to `fromJson`; JSON double quotes do not need backslash
+escaping.
+
+Raw generic types, `in` projections, and star projections are rejected for typed restoration. An
+`out X` projection is accepted only when it normalizes to one exact final or closed readable
+schema. Recursive generic models are supported when recursion returns to the same exact binding;
+an active raw declaration that expands to a different binding is rejected.
+
+Kotlin read-only and mutable collection interfaces use the normal JSON collection/map behavior.
+Read-only does not mean immutable: standard interfaces materialize as `ArrayList`,
+`LinkedHashSet`, or `LinkedHashMap`. Declare public interfaces instead of JDK/Kotlin private empty,
+singleton, unmodifiable, or builder implementation classes. `Iterable`, `Sequence`, iterators,
+`EnumEntries`, and a directly declared `Map.Entry` are not automatic value schemas.
+
+Unsigned and eligible non-null value-class map keys use JSON object member names through the
+normal map codec. A value-class key chain must terminate in String, enum, signed integer, or
+unsigned integer semantics. Floating, Boolean, nullable, and arbitrary object keys require an
+explicit key or whole-map codec.
+
+## Value classes, objects, and closed hierarchies
+
+A user value class has the transparent JSON shape of its underlying value:
+
+```kotlin
+import org.apache.fory.json.kotlin.jsonTypeRef
+
+@JvmInline
+value class AccountId(val value: ULong)
+
+val idType = jsonTypeRef<AccountId>()
+val id = json.fromJson("18446744073709551615", idType)
+```
+
+Fory executes the compiler's validated constructor operation, so value-class initialization checks
+still run. If both the outer value and its underlying value can be null, transparent JSON is
+ambiguous and automatic mapping is rejected. Use a tagged exact codec for that case.
+
+A stateless `object` or `data object` uses strict `{}` and returns the canonical singleton. A
+stateful object and a companion object require an exact custom codec. `Unit` also uses `{}`;
+`Nothing` is rejected, while `Nothing?` accepts only JSON null through its explicit Kotlin type
+token.
+
+Sealed classes are not discovered from input or package metadata. Declare the complete logical-name
+table with `JsonSubTypes`; only listed exact runtime classes are accepted. The input contains a
+logical subtype name, never a JVM class name. See [Annotations](annotations.md#jsonsubtypes) for
+the available property and wrapper shapes.
+
+## Supported Kotlin types
+
+Java/JDK scalar, temporal, Optional, atomic, array, collection, map, enum, and JSON tree types keep
+their normal Fory JSON representation when used from Kotlin:
+
+| Core family | Kotlin-visible behavior |
+| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `Any` / `Any?` | natural JSON Boolean, number, String, array, object, or null; dynamic runtime dispatch on writes |
+| signed scalars and boxes | `Boolean`, `Byte`, `Short`, `Int`, `Long`, `Float`, `Double`, `Char`, and `Number` use the core scalar codecs; non-finite floating values use quoted core forms |
+| text | `String`, exact `CharSequence`, `StringBuilder`, and `StringBuffer` use String shapes |
+| arbitrary/reduced-precision number | `BigInteger`, `BigDecimal`, Fory `Float16`, and `BFloat16` use their core numeric shapes and limits |
+| enum | quoted enum constant name |
+| Java/Kotlin arrays | normal JSON arrays; `ByteArray` is numeric unless `JsonBase64` selects binary; unsigned semantic arrays are listed below |
+| Optional and atomic | `Optional<T>`, primitive Optionals, atomic scalars/references, and atomic arrays keep their transparent core shapes subject to the nullability rules above |
+| quoted JDK values | `Currency`, `File`, `URI`, `Path`, `Pattern`, `UUID`, `Locale`, `Charset`, and `TimeZone` keep their core String shapes |
+| legacy date/time | `Date`, `Calendar`, and available `java.sql.Date`, `Time`, and `Timestamp` keep their epoch-millisecond shapes |
+| Java time | `LocalDate`, `LocalTime`, `LocalDateTime`, `Instant`, `java.time.Duration`, `ZoneOffset`, `ZoneId`, `ZonedDateTime`, `Year`, `YearMonth`, `MonthDay`, `Period`, `OffsetTime`, `OffsetDateTime`, and supported chronology dates keep their exact core text grammars |
+| other core values | `BitSet`, `ByteBuffer`, `JsonArray`, and `JsonObject` keep their normal core shapes |
+| collections/maps | supported Java/Kotlin `Collection` and `Map` interfaces and implementations use core array/object shapes; supported Guava immutable carriers remain optional |
+| map keys | String, enum, signed `Byte`/`Short`/`Int`/`Long`, and the Kotlin unsigned/value-class additions below; Boolean, floating, nullable, and arbitrary object keys need an explicit key or whole-map codec |
+| fixed rejections | `Class`, URL/network/socket/address families, unsupported JDK internal collection implementations, and unregistered `Number`/`CharSequence` subclasses remain rejected; an application may explicitly own a permitted custom representation subject to the normal type and security checks |
+
+See [Object Mapping](object-mapping.md#supported-java-types) for the detailed core shapes.
+Kotlin-specific behavior is:
+
+| Type family | Automatic JSON shape or decision |
+| ------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------- |
+| ordinary, data, and nested classes | named JSON object |
+| `inner` class, ordinary abstract class/interface | closed `JsonSubTypes` or exact custom codec only |
+| sealed class/interface | explicit closed `JsonSubTypes` table only |
+| enum | quoted enum name |
+| stateless `object`, `data object`, `Unit` | strict `{}` |
+| stateful object, companion object | exact custom codec only |
+| value class | transparent underlying value; exact binding required |
+| `Nothing?` / `Nothing` | null-only / rejected |
+| `Pair`, `Triple` | named objects with `first`, `second`, and `third` |
+| `Result`, `Lazy`, Kotlin standard property delegates | exact custom codec only |
+| signed primitives, primitive arrays, `Array<T>` | normal core number/Boolean/character and array shapes |
+| `UByte`, `UShort`, `UInt`, `ULong` and their arrays | unsigned decimal numbers and arrays |
+| read-only and mutable collections/maps, Kotlin `ArrayDeque` | normal core arrays/maps |
+| `Map.Entry`, private collection carriers, `Iterable`, `Sequence`, iterators, `EnumEntries` | rejected automatically |
+| `CharRange`, signed/unsigned integer ranges | `{"start":...,"endInclusive":...}` |
+| corresponding progressions | `{"first":...,"last":...,"step":...}` |
+| `ClosedRange`, `OpenEndRange`, abstract/open/floating ranges | rejected automatically |
+| `kotlin.time.Duration` | quoted canonical Kotlin ISO duration |
+| `kotlin.time.Instant` | quoted canonical Kotlin ISO instant |
+| `TimedValue<T>` | `{"value":...,"duration":...}` |
+| `DurationUnit`, `RegexOption` | quoted enum name |
+| clocks, time sources/marks, Regex/match state, Random | exact custom codec only |
+| `kotlin.uuid.Uuid` | quoted canonical dashed UUID |
+| complete generic class / declaration-site variance | exact substituted schema |
+| `out X` / `in X` / star projection | exact final-or-closed `X` only / rejected / rejected |
+| recursive generic | same exact recursive binding only; active expansion to another binding is rejected |
+| typealias | its fully expanded type |
+| function/suspend function, reflection types, coroutine/flow/channel state | rejected |
+| eligible third-party immutable Kotlin model | automatic on the JVM; register an exact Mixin when applying annotation overlays |
+
+Kotlin experimental opt-in requirements for time and UUID APIs still apply to application source.
+The Fory artifact's supported compiler and metadata boundary does not turn an experimental Kotlin
+API into a cross-version Kotlin guarantee.
+
+## Security
+
+`jsonTypeRef<T>()`, annotations, Mixins, and codec registrations are application-declared schema.
+JSON input cannot select an arbitrary class, constructor, compiler default, object, companion,
+module, codec, or callable. A sealed hierarchy accepts only the logical subtype names in its
+declared `@JsonSubTypes` table.
+
+Kotlin arrays, collections, maps, and objects use the same `maxDepth`, graph-memory, input-buffer,
+field-name-cache, and type-checker controls as the core JSON runtime. There are no Kotlin-specific
+collection or workspace limits to configure. Model constructors, compiler defaults, validators,
+and custom codecs remain trusted application code and may have application-defined allocation or
+side effects.
+
+See [Security](security.md) before decoding untrusted input.
+
+## GraalVM and Android
+
+On GraalVM Native Image, use the existing `@ForyJsonProvider` workflow, install
+`ForyJsonKotlin`, and enable code generation in the returned configuration. Annotate each reachable
+concrete Kotlin model with `@JsonType`, or register an exact reachable Mixin for a third-party
+target. Fory reads the Kotlin metadata and prepares generated codecs while building the image.
+Only exact generic bindings reachable through provider-selected concrete roots are available. Do
+not add reflection configuration or package-wide opens.
+
+On Android, use API 26 or later. The runtime reads Kotlin metadata in both debug and release builds,
+and runtime JSON code generation remains disabled. Follow the [installation](#installation) above
+and the [Android guide](android.md) when R8 or ProGuard is enabled.
+
+Kotlin/Native, Kotlin/JS, and Kotlin/Wasm are not supported by this JVM module.
+
+See [GraalVM Native Image](graalvm.md) and [Android](android.md) for complete platform setup.
+
+## Troubleshooting
+
+See [Troubleshooting](troubleshooting.md) for Kotlin metadata, nullability, generic binding,
+Android shrinking, Native Image, syntax, limits, custom codecs, subtypes, and root-operation
+failures.
+
+The source-aligned four-library benchmark methodology and publication status are in the
+[Kotlin JSON benchmark report](../benchmarks/json/kotlin/README.md). No Kotlin result is inferred
+from the Java or Scala benchmark.
diff --git a/docs/json/modules.md b/docs/json/modules.md
index 0e573b8ef..9eb4dd489 100644
--- a/docs/json/modules.md
+++ b/docs/json/modules.md
@@ -74,6 +74,21 @@ Application registrations made directly on `ForyJsonBuilder` take precedence ove
registrations. Conflicting module registrations fail during `build()` instead of depending on
installation order.
+## Kotlin Module
+
+`ForyJsonKotlin` is the optional module for Kotlin/JVM models. Prefer its builder when an
+application uses Kotlin models:
+
+```kotlin
+import org.apache.fory.json.kotlin.ForyJsonKotlin
+
+val json = ForyJsonKotlin.builder().build()
+```
+
+This is equivalent to `ForyJson.builder().withModule(ForyJsonKotlin)`. It does not scan the
+classpath or register application models. Exact application codec registrations retain normal
+precedence. See [Kotlin](kotlin.md) for type tokens and optional Android minification setup.
+
## Module Identity
`moduleKey()` identifies the module configuration for generated-code reuse and conflict checking.
diff --git a/docs/json/object-mapping.md b/docs/json/object-mapping.md
index 1563bc04d..4e32d2032 100644
--- a/docs/json/object-mapping.md
+++ b/docs/json/object-mapping.md
@@ -100,6 +100,19 @@ use ordinary-constructor side effects as a deserialization completion hook: when
constructor runs, property assignment happens afterward, and constructor-bypassing paths do not run
it at all.
+## Kotlin object mapping
+
+Install `fory-json-kotlin` and use `ForyJsonKotlin.builder()` for Kotlin/JVM classes. Kotlin
+ordinary and data classes use their selected constructor, exact property types, compiler defaults,
+and declared nullability; they do not use Java's constructor-bypassing fallback. A default applies
+only when the member is missing. An explicit JSON null remains a present value and is rejected for
+a non-null parameter.
+
+Use `jsonTypeRef<T>()` for generic, nullable, unsigned, and value-class roots. Standard arrays,
+collections, and maps continue to use their normal Fory JSON representation. The complete language
+type table, singleton/value-class behavior, and omission rules are in the
+[Kotlin guide](kotlin.md).
+
## Supported Java types
The following groups have built-in mappings. Exact wire representations are stable JSON values, but
diff --git a/docs/json/scala.md b/docs/json/scala.md
index 20270da79..3def6467b 100644
--- a/docs/json/scala.md
+++ b/docs/json/scala.md
@@ -115,9 +115,12 @@ Use a complete `TypeRef` when reading a parameterized Scala type:
import org.apache.fory.reflect.TypeRef
val typeRef = new TypeRef[Map[String, Option[Int]]]() {}
-val value = json.fromJson("{\"count\":1}", typeRef)
+val value = json.fromJson("""{"count":1}""", typeRef)
```
+Scala raw strings can be passed directly to `fromJson`; JSON double quotes do not need backslash
+escaping.
+
Scala value-type arguments can erase to `Object` in a normal JVM signature. `ScalaTypeRef` is a
compile-time type-token constructor that preserves those arguments on Scala 2.13 and Scala 3:
diff --git a/docs/json/security.md b/docs/json/security.md
index 1c6580a9d..267c9f56d 100644
--- a/docs/json/security.md
+++ b/docs/json/security.md
@@ -1,6 +1,6 @@
---
title: Security
-sidebar_position: 10
+sidebar_position: 11
id: security
license: |
Licensed to the Apache Software Foundation (ASF) under one or more
@@ -19,12 +19,18 @@ license: |
limitations under the License.
---
-Use Fory JSON with untrusted input only after defining which Java types may be
+Use Fory JSON with untrusted input only after defining which JVM types may be
materialized and which resource limits the endpoint will enforce. Fory JSON
does not derive arbitrary Java class names from JSON input, but annotations,
declared target types, and custom codecs still define an application-controlled
object surface.
+Kotlin type tokens and metadata are trusted schema declarations, not input authority. The Kotlin
+module validates the logical type, physical JVM carrier, and constructor/default operations before
+parsing. JSON input cannot select a class, constructor, compiler default target, object, companion,
+module, codec, or callable. A closed `JsonSubTypes` value selects only a logical name from the
+application-declared finite table.
+
## Type Policy And Class Loading
Fory JSON always applies its fixed disallow list. Add an application allow-list
@@ -80,9 +86,9 @@ storage is reserved in 1024-item batches before each batch's final child and at
the tail. Repeated set elements and duplicate or overwritten map members are
therefore charged for every input occurrence. A reference array is charged
even when every element is a leaf, and an object is charged when all its
-properties are leaves. `AtomicReference`, `AtomicReferenceArray`, and generic
-`Optional<T>` values include wrapper and reference storage; primitive
-optionals and atomic primitive values are leaves.
+properties are leaves. `AtomicReference`, `AtomicReferenceArray`, and generic `Optional<T>` values
+include wrapper and reference storage. An allocated primitive Optional or atomic primitive wrapper
+is also charged once; a cached empty Optional singleton is not a new graph owner.
Dedicated leaf codecs are excluded from graph accounting: null, strings,
characters, booleans, numeric values including arbitrary-precision numbers,
@@ -104,6 +110,18 @@ parsing storage, custom-codec allocations that the codec does not reserve, or
unrelated process memory. Actual memory use can therefore exceed the configured
budget.
+Kotlin does not add separate collection, input, or workspace limits. Arrays, collections, maps,
+and ordinary objects use the same core depth and graph-memory accounting as Java and Scala.
+Interpreted constructor argument arrays are fixed from trusted model metadata, not an
+input-declared count, and are not retained in the decoded graph. Singleton and `Unit` reads return
+existing instances. A boxed value-class result is charged once when that wrapper is materialized.
+
+Compiler defaults, model constructors, validators, and application codecs are trusted application
+code. Their internal allocation and side effects are not sandboxed or charged by the graph budget.
+Their exceptions still fail the root operation and clear root parsing state, but a later trailing-
+input failure cannot undo code that already ran. Validate side effects accordingly when decoding
+untrusted input.
+
## External Controls And Verification
Fory JSON does not authenticate, authorize, encrypt, sign, or impose an HTTP
diff --git a/docs/json/troubleshooting.md b/docs/json/troubleshooting.md
index 3315d59fc..94839267c 100644
--- a/docs/json/troubleshooting.md
+++ b/docs/json/troubleshooting.md
@@ -1,6 +1,6 @@
---
title: Troubleshooting
-sidebar_position: 11
+sidebar_position: 12
id: troubleshooting
license: |
Licensed to the Apache Software Foundation (ASF) under one or more
@@ -19,22 +19,27 @@ license: |
limitations under the License.
---
-| Symptom | Likely cause and action |
-| ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
-| `ForyJsonException` while parsing | Invalid JSON grammar, type mismatch, unsupported mapping, depth or graph-memory violation, validator failure, or trailing content |
-| `InsecureException` | Fory's disallow list or the configured `JsonTypeChecker` rejected a class |
-| `IllegalArgumentException` from a builder | Check the configured depth, graph-memory, concurrency, retained-buffer, and cached-field-name limits |
-| Declared write is rejected | The value is not assignable to the declared type, the type contains a wildcard/type variable, or null was supplied for a primitive |
-| Immutable value is not populated | Use a record, a valid `JsonCreator`, or an exact custom codec |
-| `JsonValue` read fails | Add one plain `String` `JsonCreator`, or register an exact custom codec |
-| Raw JSON output is invalid | Supply exactly one trusted, complete JSON value to the `JsonRawValue` property |
-| Ordinary object cannot be constructed | Add a usable no-argument constructor, use a record or `JsonCreator`, or register a custom codec; Android and GraalVM native image are stricter |
-| Ordinary accessor annotation fails | The method is not an eligible public JavaBean accessor, or field mode is enabled |
-| Any annotation fails | Use exactly one field-backed form or one valid method-backed pair with resolved `Map<String, V>` types; method annotations require non-field mode |
-| Codec annotation fails | Resolve same-node or hierarchy conflicts, remove a hidden nested override, or use a public no-argument codec class |
-| Subtype is rejected | The base is not declared on the write, the runtime class is not an exact table entry, or the input wire shape differs from the configured inclusion |
-| Collection cannot be read | Target a supported interface/common implementation or register a custom codec |
-| OutputStream write fails | The underlying `IOException` is wrapped as the cause of `ForyJsonException` |
+| Symptom | Likely cause and action |
+| ------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
+| `ForyJsonException` while parsing | Invalid JSON grammar, type mismatch, unsupported mapping, depth or graph-memory violation, validator failure, or trailing content |
+| `InsecureException` | Fory's disallow list or the configured `JsonTypeChecker` rejected a class |
+| `IllegalArgumentException` from a builder | Check the configured depth, graph-memory, concurrency, retained-buffer, and cached-field-name limits |
+| Declared write is rejected | The value is not assignable to the declared type, the type contains a wildcard/type variable, or null was supplied for a primitive |
+| Immutable value is not populated | Use a record, a valid `JsonCreator`, or an exact custom codec |
+| `JsonValue` read fails | Add one plain `String` `JsonCreator`, or register an exact custom codec |
+| Raw JSON output is invalid | Supply exactly one trusted, complete JSON value to the `JsonRawValue` property |
+| Ordinary object cannot be constructed | Add a usable no-argument constructor, use a record or `JsonCreator`, or register a custom codec; Android and GraalVM native image are stricter |
+| Ordinary accessor annotation fails | The method is not an eligible public JavaBean accessor, or field mode is enabled |
+| Any annotation fails | Use exactly one field-backed form or one valid method-backed pair with resolved `Map<String, V>` types; method annotations require non-field mode |
+| Codec annotation fails | Resolve same-node or hierarchy conflicts, remove a hidden nested override, or use a public no-argument codec class |
+| Subtype is rejected | The base is not declared on the write, the runtime class is not an exact table entry, or the input wire shape differs from the configured inclusion |
+| Collection cannot be read | Target a supported interface/common implementation or register a custom codec |
+| OutputStream write fails | The underlying `IOException` is wrapped as the cause of `ForyJsonException` |
+| Kotlin null or missing member fails | Check the exact `jsonTypeRef`, constructor default, and nullable occurrence; null does not request a compiler default |
+| Raw/star/projected Kotlin generic fails | Supply a complete `jsonTypeRef<T>()`; `in` and star projections cannot reconstruct one exact schema |
+| Unsupported Kotlin metadata | Compile the model with a supported Kotlin 2.3 compiler and ensure its validated JVM members match the metadata |
+| Kotlin model fails after Android shrinking | Apply KSP; for an exact Mixin, use it when either its source or target is Kotlin, and verify that the generated rules are packaged |
+| Kotlin model is absent in Native Image | Install `ForyJsonKotlin` from a reachable `ForyJsonProvider`, enable code generation, and make the exact binding reachable from that configuration |
Fory JSON mapping, syntax, codec, depth, graph-memory, validator, and output failures use
`ForyJsonException`. User codec code may still throw its own runtime exception. Creator and
diff --git a/docs/start/index.md b/docs/start/index.md
index cfc88ef68..9520de1ed 100644
--- a/docs/start/index.md
+++ b/docs/start/index.md
@@ -38,7 +38,7 @@ packages. For source downloads, see the Apache Foryâ„¢
## Choose a Language
-Each language page provides a release-pinned installation snippet, a minimal
+Each language page provides a version-pinned installation snippet, a minimal
round trip for an application project, and the next capability-specific steps:
| Language | Setup |
@@ -61,7 +61,7 @@ round trip for an application project, and the next capability-specific steps:
| -------------------- | ------------------------------------------------------------------------------ | ---------------------------------------------------------------- | -------------------------------------------------------- |
| Object Serialization | Reconstruct object graphs, including shared references and schema changes | All languages | [Object Serialization](../object-serialization/index.md) |
| Row Format | Trusted analytical data with zero-copy, random, or partial field access | Java, Python, C++, Rust | [Row Format](../row-format/index.md) |
-| Fory JSON | High-performance standard JSON mapping | Java | [Fory JSON](../json/index.md) |
+| Fory JSON | High-performance standard JSON mapping | Java, Kotlin, Scala | [Fory JSON](../json/index.md) |
| Fory IDL | Generate native models and serializers from Fory, protobuf, or FlatBuffers IDL | All languages | [Fory IDL and Compiler](../compiler/index.md) |
| Fory gRPC | Use generated models over normal gRPC transports with Fory-encoded messages | Java, Python, C++, Go, Rust, JavaScript, C#, Dart, Scala, Kotlin | [Fory gRPC](../grpc/index.md) |
diff --git a/docs/start/kotlin.md b/docs/start/kotlin.md
index bf3218a10..e431cb928 100644
--- a/docs/start/kotlin.md
+++ b/docs/start/kotlin.md
@@ -19,8 +19,8 @@ license: |
limitations under the License.
---
-Fory Kotlin provides binary Object Serialization, generated models, Fory gRPC,
-and Android support. It runs on Fory Java and supports Java 8 and later.
+Fory Kotlin provides binary Object Serialization, standard JSON mapping, generated models, Fory
+gRPC, and Android support. It runs on Fory Java and supports Java 8 and later.
## Verify the Toolchain
@@ -32,11 +32,19 @@ java -version
## Object Serialization
-Add the Fory Kotlin library to the application module:
+The repositories below resolve release coordinates from Maven Central and `-SNAPSHOT` coordinates
+from the Apache snapshot repository. Keep every Fory module on the same version:
```kotlin title="build.gradle.kts"
+repositories {
+ maven("https://repository.apache.org/snapshots/") {
+ mavenContent { snapshotsOnly() }
+ }
+ mavenCentral()
+}
+
dependencies {
- implementation("org.apache.fory:fory-kotlin:1.6.1")
+ implementation("org.apache.fory:fory-kotlin:1.7.0-SNAPSHOT")
}
```
@@ -73,6 +81,34 @@ within the JVM Fory implementation family. Continue with
[xlang](../object-serialization/kotlin/basic-serialization.md#cross-language-interoperability), or
[native mode](../object-serialization/kotlin/native.md).
+## Standard JSON
+
+Fory JSON is a separate text format from binary Object Serialization. Add its optional Kotlin
+module when interoperating with ordinary JSON APIs, browsers, logs, or other JSON libraries:
+
+```kotlin title="build.gradle.kts"
+dependencies {
+ implementation("org.apache.fory:fory-json-kotlin:1.7.0-SNAPSHOT")
+}
+```
+
+```kotlin
+import org.apache.fory.json.kotlin.ForyJsonKotlin
+import org.apache.fory.json.kotlin.jsonTypeRef
+
+data class User(val id: Long, val name: String)
+
+val json = ForyJsonKotlin.builder().build()
+val userType = jsonTypeRef<User>()
+val text = json.toJson(User(1, "Alice"), userType)
+val decoded = json.fromJson(text, userType)
+```
+
+The runtime reads Kotlin/JVM metadata directly. Add `fory-json-kotlin-ksp` only to Android builds
+that use R8 or ProGuard; it emits exact retention rules for Kotlin `@JsonType` models and
+source-owned exact Mixins. Native Image uses the normal `@ForyJsonProvider` workflow. Continue with
+[Kotlin JSON](../json/kotlin.md).
+
## Other Capabilities
- **Fory IDL and Compiler** generates Kotlin models and registration helpers through KSP. See [Compiler Getting Started](../compiler/getting-started.md) and the [Kotlin generated-code guide](../compiler/generated-code/kotlin.md).
diff --git a/integration_tests/android_tests/README.md b/integration_tests/android_tests/README.md
index f08ac807f..fcc06df8c 100644
--- a/integration_tests/android_tests/README.md
+++ b/integration_tests/android_tests/README.md
@@ -1,25 +1,32 @@
# Android Integration Tests
-This project runs Android API 26+ instrumented tests for Java `fory-core` and
-`fory-json`. API 26 runs both debug reflection coverage and the release-minified
-suite. API 36 runs the release-minified suite. Release coverage verifies static
-serializers, processor-generated Fory JSON execution for mutable classes,
-`JsonCreator` classes, object-mapped and `JsonValue` desugared Records, generated
-retention rules, generated validator invocation and failure propagation,
-generated operations for the exact target-Mixin pair, and equivalent
-application-authored exact rules for unannotated ordinary classes. Mixin
-coverage registers the source at runtime after R8 minification so broad
-application keep rules cannot hide missing processor output.
+This project runs Android API 26+ instrumented tests for Java `fory-core`, Java
+`fory-json`, and Kotlin `fory-json-kotlin`. API 26 runs both debug coverage and
+the release-minified suite. API 36 runs the release-minified suite. Release
+coverage verifies static serializers, processor-generated Fory JSON execution
+for mutable Java classes, `JsonCreator` classes, object-mapped and `JsonValue`
+desugared Records, runtime Kotlin metadata and exact KSP retention through R8
+minification, generated validator invocation and failure propagation, and
+exact target-Mixin behavior. Mixin coverage registers the source at runtime
+after R8 minification so broad application keep rules cannot hide missing
+processor output.
The tests consume `org.apache.fory:fory-core:1.7.0-SNAPSHOT`,
-`org.apache.fory:fory-json:1.7.0-SNAPSHOT`, and
-`org.apache.fory:fory-annotation-processor:1.7.0-SNAPSHOT` from the local Maven
-repository, so install the Java artifacts before running Gradle:
+`org.apache.fory:fory-json:1.7.0-SNAPSHOT`,
+`org.apache.fory:fory-annotation-processor:1.7.0-SNAPSHOT`,
+`org.apache.fory:fory-json-kotlin:1.7.0-SNAPSHOT`,
+`org.apache.fory:fory-json-kotlin-ksp:1.7.0-SNAPSHOT`, and the shared Kotlin JSON
+corpus from the local Maven repository. From the repository root, install the
+Java, Kotlin, KSP, and corpus artifacts through the single Kotlin CI owner before
+running Gradle. The fixture uses Gradle 8.13, Android Gradle Plugin 8.13.2,
+Android Build Tools 35.0.0, Kotlin Android plugin 2.3.20, and KSP 2.3.8. KSP
+packages exact consumer rules through its standard resource output; the fixture
+adds no application-specific transform or processor option.
```bash
-cd ../../java
-mvn -T16 --no-transfer-progress -pl fory-json,fory-annotation-processor -am install -DskipTests -Dmaven.javadoc.skip=true -Dmaven.source.skip=true
-cd ../integration_tests/android_tests
+python ./ci/run_ci.py kotlin --task install
+cd integration_tests/android_tests
+gradle --no-daemon verifyKotlinJsonRules
gradle --no-daemon -PforyTestBuildType=debug connectedCheck
gradle --no-daemon -PforyTestBuildType=release connectedCheck
```
diff --git a/integration_tests/android_tests/build.gradle b/integration_tests/android_tests/build.gradle
index be7e56891..12e5f93c8 100644
--- a/integration_tests/android_tests/build.gradle
+++ b/integration_tests/android_tests/build.gradle
@@ -40,10 +40,14 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:8.13.2'
+ classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:2.3.20'
+ classpath 'com.google.devtools.ksp:symbol-processing-gradle-plugin:2.3.8'
}
}
apply plugin: 'com.android.application'
+apply plugin: 'org.jetbrains.kotlin.android'
+apply plugin: 'com.google.devtools.ksp'
def foryTestBuildType = providers.gradleProperty('foryTestBuildType').getOrElse('release')
@@ -88,12 +92,21 @@ android {
}
}
+kotlin {
+ compilerOptions {
+ jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
+ }
+}
+
dependencies {
implementation('org.apache.fory:fory-core:1.7.0-SNAPSHOT') {
exclude group: 'com.google.guava', module: 'guava'
exclude group: 'org.codehaus.janino', module: 'janino'
}
implementation 'org.apache.fory:fory-json:1.7.0-SNAPSHOT'
+ implementation 'org.apache.fory:fory-json-kotlin:1.7.0-SNAPSHOT'
+ implementation 'org.apache.fory:kotlin-json-corpus:1.7.0-SNAPSHOT'
+ ksp 'org.apache.fory:fory-json-kotlin-ksp:1.7.0-SNAPSHOT'
annotationProcessor 'org.apache.fory:fory-annotation-processor:1.7.0-SNAPSHOT'
implementation 'com.google.guava:guava:32.1.2-android'
implementation 'org.slf4j:slf4j-api:2.0.12'
@@ -101,3 +114,37 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test:runner:1.6.2'
}
+
+def kotlinJsonRuleModels = [
+ 'org.apache.fory.android.AndroidKotlinAccount',
+ 'org.apache.fory.android.AndroidKotlinDefaults',
+ 'org.apache.fory.android.AndroidKotlinMarker',
+]
+
+tasks.register('verifyKotlinJsonRules') {
+ dependsOn 'kspDebugKotlin', 'kspReleaseKotlin'
+ doLast {
+ ['debug', 'release'].each { variant ->
+ def ruleDir = layout.buildDirectory
+ .dir("generated/ksp/${variant}/resources/META-INF/proguard")
+ .get().asFile
+ def expected = kotlinJsonRuleModels.collectEntries { model ->
+ [("fory-json-${model}.pro"): model]
+ }
+ def actual = fileTree(ruleDir).matching { include 'fory-json-*.pro' }.files
+ .collectEntries { rule -> [(rule.name): rule] }
+ if (actual.keySet() != expected.keySet()) {
+ throw new GradleException(
+ "Unexpected Kotlin JSON rules for ${variant}: " +
+ (actual.keySet() - expected.keySet()) +
+ "; missing: " + (expected.keySet() - actual.keySet()))
+ }
+ expected.each { name, model ->
+ def exactKeep = "-keep,allowoptimization class ${model}".toString()
+ if (!actual[name].readLines('UTF-8').contains(exactKeep)) {
+ throw new GradleException("${actual[name]} does not retain exact model ${model}")
+ }
+ }
+ }
+ }
+}
diff --git a/integration_tests/android_tests/proguard-rules.pro b/integration_tests/android_tests/proguard-rules.pro
index 94391724f..c3c4110c8 100644
--- a/integration_tests/android_tests/proguard-rules.pro
+++ b/integration_tests/android_tests/proguard-rules.pro
@@ -21,6 +21,17 @@
public static void generatedMixin();
}
+# Instrumentation invokes this entry point across the target/test APK boundary. KSP's exact
+# consumer rules alone retain the Kotlin metadata and members used at runtime.
+-keep,allowoptimization class org.apache.fory.android.AndroidKotlinJsonScenarios {
+ public static void metadataSurvivesMinification();
+}
+
+# The release androidTest APK omits the Kotlin runtime shared with the separately minified target
+# APK. Target R8 cannot see AndroidX Test's reachability, so this cross-APK runtime ABI must retain
+# both its classes and method descriptors. This harness rule is independent of model retention.
+-keep class kotlin.** { *; }
+
# Equivalent user-authored rules for models that deliberately omit @JsonType.
-keepattributes Signature,RuntimeVisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations,AnnotationDefault,MethodParameters
diff --git a/integration_tests/android_tests/src/androidTest/java/org/apache/fory/android/ForyAndroidInstrumentedTest.java b/integration_tests/android_tests/src/androidTest/java/org/apache/fory/android/ForyAndroidInstrumentedTest.java
index 7757f5c81..4d50298c1 100644
--- a/integration_tests/android_tests/src/androidTest/java/org/apache/fory/android/ForyAndroidInstrumentedTest.java
+++ b/integration_tests/android_tests/src/androidTest/java/org/apache/fory/android/ForyAndroidInstrumentedTest.java
@@ -85,6 +85,11 @@ public class ForyAndroidInstrumentedTest {
AndroidJsonScenarios.generatedMixin();
}
+ @Test
+ public void kotlinJsonMetadataSurvivesMinification() {
+ AndroidKotlinJsonScenarios.metadataSurvivesMinification();
+ }
+
@Test
public void androidRuntimeDisablesCodegenAndUnsafeCopies() {
AndroidForyRuntimeScenarios.androidRuntimeDisablesCodegenAndUnsafeCopies();
diff --git a/integration_tests/android_tests/src/main/kotlin/org/apache/fory/android/AndroidKotlinJsonScenarios.kt b/integration_tests/android_tests/src/main/kotlin/org/apache/fory/android/AndroidKotlinJsonScenarios.kt
new file mode 100644
index 000000000..a74814648
--- /dev/null
+++ b/integration_tests/android_tests/src/main/kotlin/org/apache/fory/android/AndroidKotlinJsonScenarios.kt
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fory.android
+
+import org.apache.fory.integration.kotlin.json.corpus.PlatformCorpusChecks
+import org.apache.fory.integration.kotlin.json.corpus.PlatformJavaProfileMixin
+import org.apache.fory.json.annotation.JsonType
+import org.apache.fory.json.kotlin.ForyJsonKotlin
+import org.apache.fory.json.kotlin.jsonTypeRef
+
+@JsonType
+internal data class AndroidKotlinAccount(
+ val id: Int,
+ val name: String,
+ val label: String? = "android-default",
+)
+
+@JsonType internal object AndroidKotlinMarker
+
+@JsonType
+internal data class AndroidKotlinDefaults(
+ val id: Int,
+ val v0: Int = 0,
+ val v1: Int = 1,
+ val v2: Int = 2,
+ val v3: Int = 3,
+ val v4: Int = 4,
+ val v5: Int = 5,
+ val v6: Int = 6,
+ val v7: Int = 7,
+ val v8: Int = 8,
+ val v9: Int = 9,
+ val v10: Int = 10,
+ val v11: Int = 11,
+ val v12: Int = 12,
+ val v13: Int = 13,
+ val v14: Int = 14,
+ val v15: Int = 15,
+ val v16: Int = 16,
+ val v17: Int = 17,
+ val v18: Int = 18,
+ val v19: Int = 19,
+ val v20: Int = 20,
+ val v21: Int = 21,
+ val v22: Int = 22,
+ val v23: Int = 23,
+ val v24: Int = 24,
+ val v25: Int = 25,
+ val v26: Int = 26,
+ val v27: Int = 27,
+ val v28: Int = 28,
+ val v29: Int = 29,
+ val v30: Int = 30,
+ val v31: Int = 31,
+ val v32: Int = 32,
+)
+
+internal object AndroidKotlinJsonScenarios {
+ @JvmStatic
+ fun metadataSurvivesMinification() {
+ val json =
+ ForyJsonKotlin.builder()
+ .registerMixin(PlatformJavaProfileMixin::class.java)
+ .withAsyncCompilation(false)
+ .build()
+ val accountType = jsonTypeRef<AndroidKotlinAccount>()
+ val value = AndroidKotlinAccount(26, "android", null)
+ check(json.fromJson(json.toJson(value, accountType), accountType) == value)
+ check(
+ json.fromJson("{\"id\":27,\"name\":\"default\"}", accountType) ==
+ AndroidKotlinAccount(27, "default")
+ )
+ val defaults =
+ json.fromJson(
+ "{\"id\":28,\"v32\":320}",
+ jsonTypeRef<AndroidKotlinDefaults>(),
+ )
+ check(defaults.v0 == 0)
+ check(defaults.v31 == 31)
+ check(defaults.v32 == 320)
+ check(json.fromJson("{}", jsonTypeRef<AndroidKotlinMarker>()) === AndroidKotlinMarker)
+
+ PlatformCorpusChecks.verifyRoundTrip(json)
+ }
+
+}
diff --git a/integration_tests/jpms_tests/pom.xml b/integration_tests/graalvm_kotlin_tests/pom.xml
similarity index 56%
copy from integration_tests/jpms_tests/pom.xml
copy to integration_tests/graalvm_kotlin_tests/pom.xml
index a3608c023..3da0a935b 100644
--- a/integration_tests/jpms_tests/pom.xml
+++ b/integration_tests/graalvm_kotlin_tests/pom.xml
@@ -24,40 +24,30 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.fory</groupId>
- <artifactId>fory-parent</artifactId>
+ <artifactId>fory-kotlin-parent</artifactId>
<version>1.7.0-SNAPSHOT</version>
- <relativePath>../../java</relativePath>
+ <relativePath>../../kotlin</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
- <artifactId>jpms_tests</artifactId>
+ <artifactId>graalvm-kotlin-tests</artifactId>
+ <name>Fory Kotlin JSON GraalVM Integration Tests</name>
<properties>
- <maven.compiler.source>11</maven.compiler.source>
- <maven.compiler.target>11</maven.compiler.target>
- <maven.compiler.release>11</maven.compiler.release>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <fory.final.field.policy.arg/>
+ <dokka.skip>true</dokka.skip>
+ <mainClass>org.apache.fory.graalvm.kotlin.Main</mainClass>
+ <maven.deploy.skip>true</maven.deploy.skip>
+ <native.maven.plugin.version>0.9.28</native.maven.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.fory</groupId>
- <artifactId>fory-core</artifactId>
+ <artifactId>fory-json-kotlin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.fory</groupId>
- <artifactId>fory-format</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.fory</groupId>
- <artifactId>fory-test-core</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.fory</groupId>
- <artifactId>fory-testsuite</artifactId>
+ <artifactId>kotlin-json-corpus</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
@@ -67,44 +57,50 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
- <version>3.11.0</version>
+ </plugin>
+ <plugin>
+ <groupId>org.graalvm.buildtools</groupId>
+ <artifactId>native-maven-plugin</artifactId>
+ <version>${native.maven.plugin.version}</version>
+ <extensions>true</extensions>
+ <executions>
+ <execution>
+ <id>build-native</id>
+ <phase>package</phase>
+ <goals>
+ <goal>compile-no-fork</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <fallback>false</fallback>
+ <imageName>main</imageName>
+ <mainClass>${mainClass}</mainClass>
+ </configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
- <id>jdk25-and-higher</id>
+ <id>native-jdk25</id>
<activation>
<jdk>[25,)</jdk>
</activation>
<build>
<plugins>
<plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
+ <groupId>org.graalvm.buildtools</groupId>
+ <artifactId>native-maven-plugin</artifactId>
<configuration>
- <argLine>
- --sun-misc-unsafe-memory-access=deny
- --add-opens=java.base/java.lang.invoke=org.apache.fory.core
- ${fory.final.field.policy.arg}
- </argLine>
+ <buildArgs combine.children="append">
+ <buildArg>-J--sun-misc-unsafe-memory-access=deny</buildArg>
+ <buildArg>-J--add-opens=java.base/java.lang.invoke=ALL-UNNAMED</buildArg>
+ </buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
- <profile>
- <id>jdk26-and-higher</id>
- <activation>
- <jdk>[26,)</jdk>
- </activation>
- <properties>
- <fory.final.field.policy.arg>
- --illegal-final-field-mutation=deny
- </fory.final.field.policy.arg>
- </properties>
- </profile>
</profiles>
-
</project>
diff --git a/integration_tests/jpms_tests/src/main/java/org/apache/fory/integration_tests/Test.java b/integration_tests/graalvm_kotlin_tests/src/main/java/org/apache/fory/graalvm/kotlin/KotlinJsonProvider.java
similarity index 56%
copy from integration_tests/jpms_tests/src/main/java/org/apache/fory/integration_tests/Test.java
copy to integration_tests/graalvm_kotlin_tests/src/main/java/org/apache/fory/graalvm/kotlin/KotlinJsonProvider.java
index cd57946ea..b981c0604 100644
--- a/integration_tests/jpms_tests/src/main/java/org/apache/fory/integration_tests/Test.java
+++ b/integration_tests/graalvm_kotlin_tests/src/main/java/org/apache/fory/graalvm/kotlin/KotlinJsonProvider.java
@@ -17,22 +17,22 @@
* under the License.
*/
-package org.apache.fory.integration_tests;
+package org.apache.fory.graalvm.kotlin;
-import org.apache.fory.Fory;
-import org.apache.fory.format.encoder.Encoders;
-import org.apache.fory.test.bean.Foo;
+import org.apache.fory.integration.kotlin.json.corpus.PlatformJavaProfileMixin;
+import org.apache.fory.json.ForyJson;
+import org.apache.fory.json.annotation.ForyJsonProvider;
+import org.apache.fory.json.kotlin.ForyJsonKotlin;
-/**
- * A test class that simply references classes from the various Fory artifacts to check whether they
- * specify the module names referenced in the module-info descriptor.
- */
-public class Test {
-
- public static void main(String[] args) {
- final Fory fory = Fory.builder().withCompatible(true).build();
- fory.serialize(Foo.create());
+/** Selects the Kotlin-enabled generated configuration during Native Image analysis. */
+@ForyJsonProvider
+public final class KotlinJsonProvider {
+ public KotlinJsonProvider() {}
- Encoders.bean(Foo.class, fory);
+ public ForyJson generatedConfiguration() {
+ return ForyJsonKotlin.builder()
+ .registerMixin(PlatformJavaProfileMixin.class)
+ .withAsyncCompilation(false)
+ .build();
}
}
diff --git a/integration_tests/graalvm_kotlin_tests/src/main/java/org/apache/fory/graalvm/kotlin/Main.java b/integration_tests/graalvm_kotlin_tests/src/main/java/org/apache/fory/graalvm/kotlin/Main.java
new file mode 100644
index 000000000..b27280ac9
--- /dev/null
+++ b/integration_tests/graalvm_kotlin_tests/src/main/java/org/apache/fory/graalvm/kotlin/Main.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fory.graalvm.kotlin;
+
+import org.apache.fory.integration.kotlin.json.corpus.PlatformCorpusChecks;
+import org.apache.fory.integration.kotlin.json.corpus.PlatformJavaProfileMixin;
+import org.apache.fory.json.ForyJson;
+import org.apache.fory.json.annotation.ForyJsonProvider;
+import org.apache.fory.json.kotlin.ForyJsonKotlin;
+
+/** Native Image acceptance application for provider-selected Kotlin JSON capabilities. */
+public final class Main {
+ private Main() {}
+
+ public static void main(String[] args) {
+ check(
+ KotlinJsonProvider.class.isAnnotationPresent(ForyJsonProvider.class),
+ "Native configuration provider is not reachable");
+
+ ForyJson json =
+ ForyJsonKotlin.builder()
+ .registerMixin(PlatformJavaProfileMixin.class)
+ .withAsyncCompilation(false)
+ .build();
+ PlatformCorpusChecks.verifyRoundTrip(json);
+ System.out.println("Fory Kotlin JSON Native Image succeed");
+ }
+
+ private static void check(boolean condition, String message) {
+ if (!condition) {
+ throw new AssertionError(message);
+ }
+ }
+}
diff --git a/integration_tests/graalvm_kotlin_tests/src/main/resources/META-INF/native-image/org.apache.fory/graalvm-kotlin-tests/native-image.properties b/integration_tests/graalvm_kotlin_tests/src/main/resources/META-INF/native-image/org.apache.fory/graalvm-kotlin-tests/native-image.properties
new file mode 100644
index 000000000..8f730d7de
--- /dev/null
+++ b/integration_tests/graalvm_kotlin_tests/src/main/resources/META-INF/native-image/org.apache.fory/graalvm-kotlin-tests/native-image.properties
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+Args=-H:+ReportExceptionStackTraces -H:IncludeResources=org/apache/fory/integration/kotlin/json/corpus/.*[.]json
diff --git a/integration_tests/grpc_tests/kotlin/pom.xml b/integration_tests/grpc_tests/kotlin/pom.xml
index 28f50a5d7..4b6d943e8 100644
--- a/integration_tests/grpc_tests/kotlin/pom.xml
+++ b/integration_tests/grpc_tests/kotlin/pom.xml
@@ -33,6 +33,7 @@
<artifactId>fory-kotlin-grpc-tests</artifactId>
<properties>
+ <dokka.skip>true</dokka.skip>
<grpc.version>1.75.0</grpc.version>
<grpc.kotlin.version>1.4.3</grpc.kotlin.version>
<kotlinx.coroutines.version>1.8.0</kotlinx.coroutines.version>
diff --git a/integration_tests/idl_tests/kotlin/pom.xml b/integration_tests/idl_tests/kotlin/pom.xml
index ebed79de4..f407390e2 100644
--- a/integration_tests/idl_tests/kotlin/pom.xml
+++ b/integration_tests/idl_tests/kotlin/pom.xml
@@ -32,6 +32,10 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>fory-kotlin-idl-tests</artifactId>
+ <properties>
+ <dokka.skip>true</dokka.skip>
+ </properties>
+
<build>
<resources>
<resource>
diff --git a/integration_tests/jpms_tests/pom.xml b/integration_tests/jpms_tests/pom.xml
index a3608c023..a40aedb02 100644
--- a/integration_tests/jpms_tests/pom.xml
+++ b/integration_tests/jpms_tests/pom.xml
@@ -50,6 +50,21 @@
<artifactId>fory-format</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.fory</groupId>
+ <artifactId>fory-json</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.fory</groupId>
+ <artifactId>fory-json-kotlin</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.fory</groupId>
+ <artifactId>kotlin-json-corpus</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>org.apache.fory</groupId>
<artifactId>fory-test-core</artifactId>
diff --git a/integration_tests/jpms_tests/src/main/java/module-info.java b/integration_tests/jpms_tests/src/main/java/module-info.java
index be2ec6f58..984457430 100644
--- a/integration_tests/jpms_tests/src/main/java/module-info.java
+++ b/integration_tests/jpms_tests/src/main/java/module-info.java
@@ -20,6 +20,9 @@
module org.apache.fory.integration_tests {
requires org.apache.fory.core;
requires org.apache.fory.format;
+ requires org.apache.fory.json;
+ requires org.apache.fory.json.kotlin;
+ requires org.apache.fory.integration.kotlin.json.corpus;
requires org.apache.fory.test.core;
// we can't really test any classes from this module because it only contains test-classes
diff --git a/integration_tests/jpms_tests/src/main/java/org/apache/fory/integration_tests/Test.java b/integration_tests/jpms_tests/src/main/java/org/apache/fory/integration_tests/Test.java
index cd57946ea..51e8e9bed 100644
--- a/integration_tests/jpms_tests/src/main/java/org/apache/fory/integration_tests/Test.java
+++ b/integration_tests/jpms_tests/src/main/java/org/apache/fory/integration_tests/Test.java
@@ -21,6 +21,11 @@ package org.apache.fory.integration_tests;
import org.apache.fory.Fory;
import org.apache.fory.format.encoder.Encoders;
+import org.apache.fory.integration.kotlin.json.corpus.KotlinJsonCorpus;
+import org.apache.fory.integration.kotlin.json.corpus.PlatformAccount;
+import org.apache.fory.json.ForyJson;
+import org.apache.fory.json.kotlin.ForyJsonKotlin;
+import org.apache.fory.reflect.TypeRef;
import org.apache.fory.test.bean.Foo;
/**
@@ -34,5 +39,23 @@ public class Test {
fory.serialize(Foo.create());
Encoders.bean(Foo.class, fory);
+
+ verifyKotlinJson();
+ }
+
+ static void verifyKotlinJson() {
+ ForyJson json = ForyJsonKotlin.builder().withAsyncCompilation(false).build();
+ TypeRef<PlatformAccount> accountType = KotlinJsonCorpus.accountType();
+ PlatformAccount value = new PlatformAccount(37, "jpms", "default-label");
+ PlatformAccount decoded = json.fromJson(json.toJson(value, accountType), accountType);
+ if (!value.equals(decoded)) {
+ throw new AssertionError("Kotlin JSON JPMS round trip failed: " + decoded);
+ }
+ PlatformAccount defaulted = json.fromJson("{\"id\":1,\"name\":\"default\"}", accountType);
+ if (defaulted.getId() != 1
+ || !"default".equals(defaulted.getName())
+ || !"corpus-default".equals(defaulted.getLabel())) {
+ throw new AssertionError("Kotlin JSON JPMS default constructor bridge was not used");
+ }
}
}
diff --git a/integration_tests/jpms_tests/src/test/java/org/apache/fory/integration_tests/JpmsFieldAccessorTest.java b/integration_tests/jpms_tests/src/test/java/org/apache/fory/integration_tests/JpmsFieldAccessorTest.java
index 3f81ea9c7..141f062c3 100644
--- a/integration_tests/jpms_tests/src/test/java/org/apache/fory/integration_tests/JpmsFieldAccessorTest.java
+++ b/integration_tests/jpms_tests/src/test/java/org/apache/fory/integration_tests/JpmsFieldAccessorTest.java
@@ -40,6 +40,11 @@ public class JpmsFieldAccessorTest {
private static final int JDK_MAJOR_VERSION = Runtime.version().feature();
private static final String VAR_HANDLE = "java.lang.invoke.VarHandle";
+ @Test
+ public void testModuleConsumer() {
+ org.apache.fory.integration_tests.Test.verifyKotlinJson();
+ }
+
@Test
public void testPrivateFieldAccess() throws Exception {
PrivateFieldBean bean = new PrivateFieldBean(7);
diff --git a/integration_tests/kotlin_json_corpus/pom.xml b/integration_tests/kotlin_json_corpus/pom.xml
new file mode 100644
index 000000000..8840598c2
--- /dev/null
+++ b/integration_tests/kotlin_json_corpus/pom.xml
@@ -0,0 +1,179 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <groupId>org.apache.fory</groupId>
+ <artifactId>fory-kotlin-parent</artifactId>
+ <version>1.7.0-SNAPSHOT</version>
+ <relativePath>../../kotlin</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>kotlin-json-corpus</artifactId>
+ <name>Fory Kotlin JSON Integration Corpus</name>
+
+ <properties>
+ <maven.deploy.skip>true</maven.deploy.skip>
+ <dokka.skip>true</dokka.skip>
+ </properties>
+
+ <build>
+ <resources>
+ <resource>
+ <directory>src/main/resources</directory>
+ </resource>
+ <resource>
+ <directory>${project.build.directory}/generated-resources/ksp</directory>
+ <includes>
+ <include>META-INF/proguard/**</include>
+ </includes>
+ </resource>
+ </resources>
+ <plugins>
+ <plugin>
+ <groupId>me.kpavlov.ksp.maven</groupId>
+ <artifactId>ksp-maven-plugin</artifactId>
+ <version>${ksp.maven.plugin.version}</version>
+ <executions>
+ <execution>
+ <id>ksp</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>process</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
+ <sourceDirs>
+ <sourceDir>${project.basedir}/src/main/java</sourceDir>
+ </sourceDirs>
+ <addGeneratedSourcesToCompile>false</addGeneratedSourcesToCompile>
+ <processors>
+ <processor>
+ <groupId>org.apache.fory</groupId>
+ <artifactId>fory-json-kotlin-ksp</artifactId>
+ <version>${project.version}</version>
+ </processor>
+ </processors>
+ </configuration>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.fory</groupId>
+ <artifactId>fory-json-kotlin-ksp</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.google.devtools.ksp</groupId>
+ <artifactId>symbol-processing-aa-embeddable</artifactId>
+ <version>${ksp.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.google.devtools.ksp</groupId>
+ <artifactId>symbol-processing-api</artifactId>
+ <version>${ksp.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.google.devtools.ksp</groupId>
+ <artifactId>symbol-processing-common-deps</artifactId>
+ <version>${ksp.version}</version>
+ </dependency>
+ </dependencies>
+ </plugin>
+ <plugin>
+ <groupId>org.jetbrains.kotlin</groupId>
+ <artifactId>kotlin-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>compile</id>
+ <phase>process-sources</phase>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ <configuration>
+ <sourceDirs>
+ <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
+ <sourceDir>${project.basedir}/src/main/java</sourceDir>
+ </sourceDirs>
+ <args>
+ <arg>-Xexplicit-api=strict</arg>
+ </args>
+ </configuration>
+ </execution>
+ <execution>
+ <id>test-compile</id>
+ <phase>test-compile</phase>
+ <goals>
+ <goal>test-compile</goal>
+ </goals>
+ <configuration>
+ <sourceDirs>
+ <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
+ </sourceDirs>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <version>3.3.0</version>
+ <configuration>
+ <archive>
+ <manifestEntries>
+ <Automatic-Module-Name>org.apache.fory.integration.kotlin.json.corpus</Automatic-Module-Name>
+ </manifestEntries>
+ </archive>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>3.5.4</version>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.fory</groupId>
+ <artifactId>fory-json-kotlin</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jetbrains.kotlin</groupId>
+ <artifactId>kotlin-stdlib</artifactId>
+ <version>${kotlin.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jetbrains.kotlin</groupId>
+ <artifactId>kotlin-test-testng</artifactId>
+ <version>${kotlin.version}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/integration_tests/jpms_tests/src/main/java/module-info.java b/integration_tests/kotlin_json_corpus/src/main/java/org/apache/fory/integration/kotlin/json/corpus/PlatformJavaProfile.java
similarity index 66%
copy from integration_tests/jpms_tests/src/main/java/module-info.java
copy to integration_tests/kotlin_json_corpus/src/main/java/org/apache/fory/integration/kotlin/json/corpus/PlatformJavaProfile.java
index be2ec6f58..9efae38b0 100644
--- a/integration_tests/jpms_tests/src/main/java/module-info.java
+++ b/integration_tests/kotlin_json_corpus/src/main/java/org/apache/fory/integration/kotlin/json/corpus/PlatformJavaProfile.java
@@ -17,14 +17,23 @@
* under the License.
*/
-module org.apache.fory.integration_tests {
- requires org.apache.fory.core;
- requires org.apache.fory.format;
- requires org.apache.fory.test.core;
+package org.apache.fory.integration.kotlin.json.corpus;
- // we can't really test any classes from this module because it only contains test-classes
- requires org.apache.fory.test.suite;
+/** Java model targeted by a Kotlin-authored JSON Mixin. */
+public final class PlatformJavaProfile {
+ private String label;
- exports org.apache.fory.integration_tests.model;
- exports org.apache.fory.integration_tests.publicserializer;
+ public PlatformJavaProfile() {}
+
+ public PlatformJavaProfile(String label) {
+ this.label = label;
+ }
+
+ public String getLabel() {
+ return label;
+ }
+
+ public void setLabel(String label) {
+ this.label = label;
+ }
}
diff --git a/integration_tests/kotlin_json_corpus/src/main/kotlin/org/apache/fory/integration/kotlin/json/corpus/KotlinJsonCorpus.kt b/integration_tests/kotlin_json_corpus/src/main/kotlin/org/apache/fory/integration/kotlin/json/corpus/KotlinJsonCorpus.kt
new file mode 100644
index 000000000..f44316776
--- /dev/null
+++ b/integration_tests/kotlin_json_corpus/src/main/kotlin/org/apache/fory/integration/kotlin/json/corpus/KotlinJsonCorpus.kt
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fory.integration.kotlin.json.corpus
+
+import java.nio.charset.StandardCharsets
+import org.apache.fory.json.kotlin.jsonTypeRef
+import org.apache.fory.reflect.TypeRef
+
+/** Java-friendly exact structural type tokens shared by every platform fixture. */
+public object KotlinJsonCorpus {
+ private const val RESOURCE_ROOT: String = "/org/apache/fory/integration/kotlin/json/corpus/"
+
+ @JvmStatic public fun accountType(): TypeRef<PlatformAccount> = jsonTypeRef()
+
+ @JvmStatic public fun rootType(): TypeRef<PlatformRoot> = jsonTypeRef()
+
+ @JvmStatic public fun rootValue(): PlatformRoot = platformRootValue()
+
+ @JvmStatic public fun rootJson(): String = resourceText("cases/root.json")
+
+ private fun resourceText(path: String): String {
+ val stream =
+ KotlinJsonCorpus::class.java.getResourceAsStream(RESOURCE_ROOT + path)
+ ?: error("Missing Kotlin JSON corpus resource: $path")
+ return stream.bufferedReader(StandardCharsets.UTF_8).use { it.readText() }
+ }
+}
diff --git a/integration_tests/kotlin_json_corpus/src/main/kotlin/org/apache/fory/integration/kotlin/json/corpus/PlatformCorpusChecks.kt b/integration_tests/kotlin_json_corpus/src/main/kotlin/org/apache/fory/integration/kotlin/json/corpus/PlatformCorpusChecks.kt
new file mode 100644
index 000000000..7e8b4cf2e
--- /dev/null
+++ b/integration_tests/kotlin_json_corpus/src/main/kotlin/org/apache/fory/integration/kotlin/json/corpus/PlatformCorpusChecks.kt
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fory.integration.kotlin.json.corpus
+
+import org.apache.fory.json.ForyJson
+
+/** Executes the same representative round trip on the JVM, Android, and Native Image. */
+public object PlatformCorpusChecks {
+ @JvmStatic
+ public fun verifyRoundTrip(json: ForyJson) {
+ val type = KotlinJsonCorpus.rootType()
+ val decoded = json.fromJson(KotlinJsonCorpus.rootJson(), type)
+ verifyRoot(decoded)
+ val text = json.toJson(decoded, type)
+ check(text.contains("\"display_label\":\"mixin\""))
+ verifyRoot(json.fromJson(text, type))
+ verifyRoot(json.fromJson(json.toJsonBytes(decoded, type), type))
+ }
+
+ private fun verifyRoot(actual: PlatformRoot) {
+ val expected = KotlinJsonCorpus.rootValue()
+ check(actual.account == expected.account)
+ check(actual.id == expected.id)
+ check(actual.unsigned == expected.unsigned)
+ check(actual.shape == expected.shape)
+ check(actual.profile.label == expected.profile.label)
+ check(actual.token == expected.token)
+ check(actual.box == expected.box)
+ }
+}
diff --git a/integration_tests/jpms_tests/src/main/java/module-info.java b/integration_tests/kotlin_json_corpus/src/main/kotlin/org/apache/fory/integration/kotlin/json/corpus/PlatformJavaProfileMixin.kt
similarity index 67%
copy from integration_tests/jpms_tests/src/main/java/module-info.java
copy to integration_tests/kotlin_json_corpus/src/main/kotlin/org/apache/fory/integration/kotlin/json/corpus/PlatformJavaProfileMixin.kt
index be2ec6f58..6739c8e02 100644
--- a/integration_tests/jpms_tests/src/main/java/module-info.java
+++ b/integration_tests/kotlin_json_corpus/src/main/kotlin/org/apache/fory/integration/kotlin/json/corpus/PlatformJavaProfileMixin.kt
@@ -17,14 +17,12 @@
* under the License.
*/
-module org.apache.fory.integration_tests {
- requires org.apache.fory.core;
- requires org.apache.fory.format;
- requires org.apache.fory.test.core;
+package org.apache.fory.integration.kotlin.json.corpus
- // we can't really test any classes from this module because it only contains test-classes
- requires org.apache.fory.test.suite;
+import org.apache.fory.json.annotation.JsonMixin
+import org.apache.fory.json.annotation.JsonProperty
- exports org.apache.fory.integration_tests.model;
- exports org.apache.fory.integration_tests.publicserializer;
+@JsonMixin(target = PlatformJavaProfile::class)
+public abstract class PlatformJavaProfileMixin {
+ @get:JsonProperty("display_label") public abstract val label: String
}
diff --git a/integration_tests/kotlin_json_corpus/src/main/kotlin/org/apache/fory/integration/kotlin/json/corpus/PlatformModels.kt b/integration_tests/kotlin_json_corpus/src/main/kotlin/org/apache/fory/integration/kotlin/json/corpus/PlatformModels.kt
new file mode 100644
index 000000000..d64254ce6
--- /dev/null
+++ b/integration_tests/kotlin_json_corpus/src/main/kotlin/org/apache/fory/integration/kotlin/json/corpus/PlatformModels.kt
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fory.integration.kotlin.json.corpus
+
+import kotlin.jvm.JvmInline
+import org.apache.fory.json.annotation.JsonCodec
+import org.apache.fory.json.annotation.JsonSubTypes
+import org.apache.fory.json.annotation.JsonType
+import org.apache.fory.json.codec.AbstractJsonValueCodec
+import org.apache.fory.json.reader.JsonReader
+import org.apache.fory.json.writer.JsonWriter
+
+@JsonType
+public data class PlatformAccount(
+ public val id: Int,
+ public val name: String,
+ public val label: String? = "corpus-default",
+)
+
+@JsonType public data class PlatformBox<T>(public val value: T)
+
+@JsonType @JvmInline public value class PlatformId(public val value: Long)
+
+@JsonType
+@JsonSubTypes(
+ value =
+ [
+ JsonSubTypes.Type(value = PlatformCircle::class, name = "circle"),
+ JsonSubTypes.Type(value = PlatformMarker::class, name = "marker"),
+ ],
+ property = "kind",
+)
+public sealed interface PlatformShape
+
+@JsonType public data class PlatformCircle(public val radius: Int) : PlatformShape
+
+@JsonType public data object PlatformMarker : PlatformShape
+
+public data class PlatformToken(public val value: String)
+
+public class PlatformTokenCodec : AbstractJsonValueCodec<PlatformToken>() {
+ override fun write(writer: JsonWriter, value: PlatformToken?) {
+ if (value == null) writer.writeNull() else writer.writeString(value.value)
+ }
+
+ override fun read(reader: JsonReader): PlatformToken? {
+ val value = reader.readString() ?: return null
+ return PlatformToken(value)
+ }
+}
+
+@JsonType
+public data class PlatformRoot(
+ public val account: PlatformAccount,
+ public val id: PlatformId,
+ public val unsigned: UInt,
+ public val shape: PlatformShape,
+ public val profile: PlatformJavaProfile,
+ @field:JsonCodec(PlatformTokenCodec::class) public val token: PlatformToken,
+ public val box: PlatformBox<String>,
+)
+
+internal fun platformRootValue(): PlatformRoot =
+ PlatformRoot(
+ account = PlatformAccount(1, "default"),
+ id = PlatformId(9),
+ unsigned = UInt.MAX_VALUE,
+ shape = PlatformCircle(3),
+ profile = PlatformJavaProfile("mixin"),
+ token = PlatformToken("custom"),
+ box = PlatformBox("generic"),
+ )
diff --git a/integration_tests/kotlin_json_corpus/src/main/resources/org/apache/fory/integration/kotlin/json/corpus/cases/root.json b/integration_tests/kotlin_json_corpus/src/main/resources/org/apache/fory/integration/kotlin/json/corpus/cases/root.json
new file mode 100644
index 000000000..38c051504
--- /dev/null
+++ b/integration_tests/kotlin_json_corpus/src/main/resources/org/apache/fory/integration/kotlin/json/corpus/cases/root.json
@@ -0,0 +1,9 @@
+{
+ "account": { "id": 1, "name": "default" },
+ "id": 9,
+ "unsigned": 4294967295,
+ "shape": { "kind": "circle", "radius": 3 },
+ "profile": { "display_label": "mixin" },
+ "token": "custom",
+ "box": { "value": "generic" }
+}
diff --git a/integration_tests/jpms_tests/src/main/java/module-info.java b/integration_tests/kotlin_json_corpus/src/test/kotlin/org/apache/fory/integration/kotlin/json/corpus/KotlinJsonCorpusTest.kt
similarity index 65%
copy from integration_tests/jpms_tests/src/main/java/module-info.java
copy to integration_tests/kotlin_json_corpus/src/test/kotlin/org/apache/fory/integration/kotlin/json/corpus/KotlinJsonCorpusTest.kt
index be2ec6f58..280afa067 100644
--- a/integration_tests/jpms_tests/src/main/java/module-info.java
+++ b/integration_tests/kotlin_json_corpus/src/test/kotlin/org/apache/fory/integration/kotlin/json/corpus/KotlinJsonCorpusTest.kt
@@ -17,14 +17,19 @@
* under the License.
*/
-module org.apache.fory.integration_tests {
- requires org.apache.fory.core;
- requires org.apache.fory.format;
- requires org.apache.fory.test.core;
+package org.apache.fory.integration.kotlin.json.corpus
- // we can't really test any classes from this module because it only contains test-classes
- requires org.apache.fory.test.suite;
+import org.apache.fory.json.kotlin.ForyJsonKotlin
+import org.testng.annotations.Test
- exports org.apache.fory.integration_tests.model;
- exports org.apache.fory.integration_tests.publicserializer;
+public class KotlinJsonCorpusTest {
+ @Test
+ public fun sharedRoundTrip(): Unit {
+ val json =
+ ForyJsonKotlin.builder()
+ .registerMixin(PlatformJavaProfileMixin::class.java)
+ .withAsyncCompilation(false)
+ .build()
+ PlatformCorpusChecks.verifyRoundTrip(json)
+ }
}
diff --git a/integration_tests/kotlin_json_corpus/src/test/kotlin/org/apache/fory/integration/kotlin/json/corpus/KspRetentionResourceTest.kt b/integration_tests/kotlin_json_corpus/src/test/kotlin/org/apache/fory/integration/kotlin/json/corpus/KspRetentionResourceTest.kt
new file mode 100644
index 000000000..251f8f431
--- /dev/null
+++ b/integration_tests/kotlin_json_corpus/src/test/kotlin/org/apache/fory/integration/kotlin/json/corpus/KspRetentionResourceTest.kt
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fory.integration.kotlin.json.corpus
+
+import java.nio.file.Files
+import java.nio.file.Path
+import org.testng.Assert.assertFalse
+import org.testng.Assert.assertTrue
+import org.testng.annotations.Test
+
+public class KspRetentionResourceTest {
+ @Test
+ public fun emitsOnlyExactResources(): Unit {
+ val generatedResources = Path.of("target/generated-resources/ksp")
+ val outputs = regularFiles(generatedResources)
+ val relative = outputs.map { generatedResources.relativize(it).toString() }.toSet()
+ assertTrue(relative == EXPECTED_RULES, relative.toString())
+ assertTrue(regularFiles(Path.of("target/generated-sources/ksp")).isEmpty())
+ assertTrue(regularFiles(Path.of("target/ksp-classes")).isEmpty())
+
+ val account = rules("PlatformAccount")
+ assertTrue(account.contains("kotlin.jvm.internal.DefaultConstructorMarker"), account)
+ val value = rules("PlatformId")
+ assertTrue(value.contains("constructor-impl"), value)
+ assertTrue(value.contains("box-impl"), value)
+ assertTrue(value.contains("unbox-impl"), value)
+ val sealed = rules("PlatformShape")
+ assertTrue(sealed.contains("class $PACKAGE.PlatformCircle"), sealed)
+ assertTrue(sealed.contains("class $PACKAGE.PlatformMarker"), sealed)
+ assertConstructor(rules("PlatformRoot"), "$PACKAGE.PlatformTokenCodec")
+ val mixin = mixinRules("PlatformJavaProfileMixin")
+ assertTrue(mixin.contains("class $PACKAGE.PlatformJavaProfile"), mixin)
+ assertTrue(mixin.contains("class $PACKAGE.PlatformJavaProfileMixin"), mixin)
+
+ outputs.forEach { output ->
+ val text = Files.readString(output)
+ assertFalse(text.contains('*'), output.toString())
+ assertFalse(text.contains("JsonCodec\$NoJsonValueCodec"), output.toString())
+ assertFalse(text.contains("JsonCodec\$NoMapKeyCodec"), output.toString())
+ }
+ }
+
+ private fun rules(model: String): String =
+ resourceText("META-INF/proguard/fory-json-$PACKAGE.$model.pro")
+
+ private fun mixinRules(model: String): String =
+ resourceText("META-INF/proguard/fory-json-mixin-$PACKAGE.$model.pro")
+
+ private fun resourceText(path: String): String =
+ checkNotNull(javaClass.classLoader.getResourceAsStream(path)) { "Missing KSP resource $path" }
+ .bufferedReader()
+ .use { it.readText() }
+
+ private fun assertConstructor(rules: String, type: String) {
+ assertTrue(
+ rules.contains("-keep,allowoptimization,allowobfuscation class $type"),
+ rules,
+ )
+ assertTrue(rules.contains("-keepclassmembers class $type {\n public <init>();\n}"), rules)
+ }
+
+ private fun regularFiles(root: Path): List<Path> {
+ if (!Files.exists(root)) return emptyList()
+ return Files.walk(root).use { paths -> paths.filter(Files::isRegularFile).toList() }
+ }
+
+ private companion object {
+ const val PACKAGE = "org.apache.fory.integration.kotlin.json.corpus"
+ val EXPECTED_RULES: Set<String> =
+ setOf(
+ "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.PlatformId.pro",
+ "META-INF/proguard/fory-json-$PACKAGE.PlatformMarker.pro",
+ "META-INF/proguard/fory-json-$PACKAGE.PlatformRoot.pro",
+ "META-INF/proguard/fory-json-$PACKAGE.PlatformShape.pro",
+ "META-INF/proguard/fory-json-mixin-$PACKAGE.PlatformJavaProfileMixin.pro",
+ )
+ }
+}
diff --git a/java/fory-core/src/main/java/org/apache/fory/codegen/CodeGenerator.java b/java/fory-core/src/main/java/org/apache/fory/codegen/CodeGenerator.java
index 826029a4c..b72064e85 100644
--- a/java/fory-core/src/main/java/org/apache/fory/codegen/CodeGenerator.java
+++ b/java/fory-core/src/main/java/org/apache/fory/codegen/CodeGenerator.java
@@ -32,6 +32,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
+import org.apache.fory.annotation.Internal;
import org.apache.fory.builder.AccessorHelper;
import org.apache.fory.builder.Generated;
import org.apache.fory.collection.Collections;
@@ -108,6 +109,13 @@ public class CodeGenerator {
classLoaderLock = new Object();
}
+ /** Compiles one unit and installs its verified direct invocation bridges before publication. */
+ @Internal
+ public ClassLoader compileDirect(CompileUnit unit, JaninoUtils.DirectInvocation... invocations) {
+ return compile(
+ Arrays.asList(unit), compileState -> compileState.lock.lock(), unit, invocations);
+ }
+
/**
* Compile code, return as a new classloader. If the class of a compilation unit already exists in
* previous classloader, skip the corresponding compilation unit.
@@ -115,10 +123,18 @@ public class CodeGenerator {
* @param units compile units
*/
public ClassLoader compile(CompileUnit... units) {
- return compile(Arrays.asList(units), compileState -> compileState.lock.lock());
+ return compile(Arrays.asList(units), compileState -> compileState.lock.lock(), null, null);
}
public ClassLoader compile(List<CompileUnit> units, CompileCallback callback) {
+ return compile(units, callback, null, null);
+ }
+
+ private ClassLoader compile(
+ List<CompileUnit> units,
+ CompileCallback callback,
+ CompileUnit directUnit,
+ JaninoUtils.DirectInvocation[] invocations) {
checkRuntimeCodegenSupported();
List<CompileUnit> compileUnits = new ArrayList<>();
ClassLoader parentClassLoader;
@@ -145,6 +161,14 @@ public class CodeGenerator {
try {
classes =
JaninoUtils.toBytecode(parentClassLoader, compileUnits.toArray(new CompileUnit[0]));
+ if (directUnit != null && invocations.length != 0) {
+ String classFile = directUnit.getQualifiedClassName().replace('.', '/') + ".class";
+ byte[] bytecode = classes.get(classFile);
+ if (bytecode == null) {
+ throw new CodegenException("Missing generated direct invocation class " + classFile);
+ }
+ classes.put(classFile, JaninoUtils.installDirectInvocations(bytecode, invocations));
+ }
compileState.result = classes;
compileState.finished = true;
} finally {
diff --git a/java/fory-core/src/main/java/org/apache/fory/codegen/JaninoUtils.java b/java/fory-core/src/main/java/org/apache/fory/codegen/JaninoUtils.java
index 98dfe57a2..e172496fa 100644
--- a/java/fory-core/src/main/java/org/apache/fory/codegen/JaninoUtils.java
+++ b/java/fory-core/src/main/java/org/apache/fory/codegen/JaninoUtils.java
@@ -21,12 +21,18 @@ package org.apache.fory.codegen;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Executable;
import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
@@ -34,6 +40,7 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
+import org.apache.fory.annotation.Internal;
import org.apache.fory.collection.Tuple2;
import org.apache.fory.logging.Logger;
import org.apache.fory.logging.LoggerFactory;
@@ -46,12 +53,87 @@ import org.codehaus.commons.compiler.util.resource.MapResourceFinder;
import org.codehaus.commons.compiler.util.resource.Resource;
import org.codehaus.janino.ClassLoaderIClassLoader;
import org.codehaus.janino.Compiler;
+import org.codehaus.janino.MethodDescriptor;
import org.codehaus.janino.util.ClassFile;
/** A util to compile code to bytecode and create classloader to load generated class. */
public class JaninoUtils {
private static final Logger LOG = LoggerFactory.getLogger(JaninoUtils.class);
+ /**
+ * One verified direct JVM invocation used to replace a source placeholder after Janino has
+ * compiled the containing class.
+ *
+ * <p>The generated-codec bridge is an instance method with a source-nameable signature; the
+ * target member need not be source-nameable. Parameter index {@code -1} supplies a JVM null
+ * constant. All other indexes select one bridge parameter.
+ */
+ @Internal
+ public static final class DirectInvocation {
+ private final String bridgeName;
+ private final Class<?> returnType;
+ private final Class<?>[] parameterTypes;
+ private final Executable target;
+ private final int receiverIndex;
+ private final int[] argumentIndexes;
+
+ private DirectInvocation(
+ String bridgeName,
+ Class<?> returnType,
+ Class<?>[] parameterTypes,
+ Executable target,
+ int receiverIndex,
+ int[] argumentIndexes) {
+ this.bridgeName = bridgeName;
+ this.returnType = returnType;
+ this.parameterTypes = parameterTypes.clone();
+ this.target = target;
+ this.receiverIndex = receiverIndex;
+ this.argumentIndexes = argumentIndexes.clone();
+ validateDirectInvocation(this);
+ }
+
+ /** Creates a bridge which invokes one exact constructor. */
+ public static DirectInvocation constructor(
+ String bridgeName,
+ Class<?>[] parameterTypes,
+ Constructor<?> target,
+ int... argumentIndexes) {
+ return new DirectInvocation(
+ bridgeName, target.getDeclaringClass(), parameterTypes, target, -1, argumentIndexes);
+ }
+
+ /** Creates a bridge which invokes one exact method. */
+ public static DirectInvocation method(
+ String bridgeName,
+ Class<?> returnType,
+ Class<?>[] parameterTypes,
+ Method target,
+ int receiverIndex,
+ int... argumentIndexes) {
+ return new DirectInvocation(
+ bridgeName, returnType, parameterTypes, target, receiverIndex, argumentIndexes);
+ }
+
+ public String bridgeName() {
+ return bridgeName;
+ }
+
+ public String descriptor() {
+ return methodDescriptor(returnType, parameterTypes);
+ }
+
+ /** Returns whether two bridge declarations invoke the same exact verified target shape. */
+ public boolean sameTarget(DirectInvocation other) {
+ return other != null
+ && returnType == other.returnType
+ && target.equals(other.target)
+ && receiverIndex == other.receiverIndex
+ && Arrays.equals(parameterTypes, other.parameterTypes)
+ && Arrays.equals(argumentIndexes, other.argumentIndexes);
+ }
+ }
+
public static Class<?> compileClass(
ClassLoader loader, String pkg, String className, String code) {
ByteArrayClassLoader classLoader = compile(loader, new CompileUnit(pkg, className, code));
@@ -144,6 +226,316 @@ public class JaninoUtils {
return classes;
}
+ /** Replaces verified source placeholders with straight-line direct JVM invocations. */
+ @Internal
+ public static byte[] installDirectInvocations(
+ byte[] classBytes, DirectInvocation... invocations) {
+ if (invocations.length == 0) {
+ return classBytes;
+ }
+ try {
+ ClassFile classFile = new ClassFile(new ByteArrayInputStream(classBytes));
+ ReflectionUtils.setObjectFieldValue(
+ classFile, "methodInfos", new ArrayList<>(classFile.methodInfos));
+ for (DirectInvocation invocation : invocations) {
+ installDirectInvocation(classFile, invocation);
+ }
+ return classFile.toByteArray();
+ } catch (IOException e) {
+ throw new CodegenException("Cannot rewrite generated direct invocation", e);
+ }
+ }
+
+ private static void installDirectInvocation(ClassFile classFile, DirectInvocation invocation) {
+ String descriptor = invocation.descriptor();
+ ClassFile.MethodInfo sourceMethod = null;
+ for (ClassFile.MethodInfo method : classFile.methodInfos) {
+ if (method.getName().equals(invocation.bridgeName())
+ && method.getDescriptor().equals(descriptor)) {
+ if (sourceMethod != null) {
+ throw new CodegenException(
+ "Duplicate generated direct invocation " + invocation.bridgeName());
+ }
+ sourceMethod = method;
+ }
+ }
+ if (sourceMethod == null) {
+ throw new CodegenException(
+ "Missing generated direct invocation "
+ + invocation.bridgeName()
+ + descriptor
+ + " in "
+ + classFile.getThisClassName());
+ }
+ if (Modifier.isStatic(sourceMethod.getAccessFlags())) {
+ throw new CodegenException(
+ "Generated direct invocation bridge must be an instance method "
+ + invocation.bridgeName()
+ + descriptor);
+ }
+ classFile.methodInfos.remove(sourceMethod);
+ ClassFile.MethodInfo method =
+ classFile.addMethodInfo(
+ sourceMethod.getAccessFlags(),
+ invocation.bridgeName(),
+ new MethodDescriptor(descriptor));
+ byte[] code = directInvocationCode(classFile, invocation);
+ int maxLocals = parameterSlots(invocation.parameterTypes) + 1;
+ int targetSlots = parameterSlots(invocation.target.getParameterTypes());
+ int invocationStack =
+ invocation.target instanceof Constructor
+ ? targetSlots + 2
+ : targetSlots + (Modifier.isStatic(invocation.target.getModifiers()) ? 0 : 1);
+ int maxStack = Math.max(invocationStack, slots(invocation.returnType));
+ short codeName = classFile.addConstantUtf8Info("Code");
+ method.addAttribute(
+ new ClassFile.CodeAttribute(
+ codeName,
+ (short) maxStack,
+ (short) maxLocals,
+ code,
+ new ClassFile.CodeAttribute.ExceptionTableEntry[0],
+ new ClassFile.AttributeInfo[0]));
+ }
+
+ private static byte[] directInvocationCode(ClassFile classFile, DirectInvocation invocation) {
+ ByteArrayOutputStream code = new ByteArrayOutputStream();
+ Executable target = invocation.target;
+ if (target instanceof Constructor) {
+ short owner = classFile.addConstantClassInfo(typeDescriptor(target.getDeclaringClass()));
+ writeOpcodeIndex(code, 0xbb, owner); // new
+ code.write(0x59); // dup
+ } else if (!Modifier.isStatic(target.getModifiers())) {
+ writeLoad(
+ code,
+ invocation.parameterTypes[invocation.receiverIndex],
+ localIndex(invocation, invocation.receiverIndex));
+ }
+ Class<?>[] targetParameters = target.getParameterTypes();
+ for (int i = 0; i < targetParameters.length; i++) {
+ int source = invocation.argumentIndexes[i];
+ if (source < 0) {
+ code.write(0x01); // aconst_null
+ } else {
+ writeLoad(code, invocation.parameterTypes[source], localIndex(invocation, source));
+ }
+ }
+ Class<?> ownerType = target.getDeclaringClass();
+ String ownerDescriptor = typeDescriptor(ownerType);
+ String targetName = target instanceof Constructor ? "<init>" : target.getName();
+ String targetDescriptor =
+ methodDescriptor(
+ target instanceof Method ? ((Method) target).getReturnType() : void.class,
+ targetParameters);
+ short reference;
+ int opcode;
+ if (target instanceof Constructor) {
+ reference = classFile.addConstantMethodrefInfo(ownerDescriptor, targetName, targetDescriptor);
+ opcode = 0xb7; // invokespecial
+ } else if (Modifier.isStatic(target.getModifiers())) {
+ reference =
+ ownerType.isInterface()
+ ? classFile.addConstantInterfaceMethodrefInfo(
+ ownerDescriptor, targetName, targetDescriptor)
+ : classFile.addConstantMethodrefInfo(ownerDescriptor, targetName, targetDescriptor);
+ opcode = 0xb8; // invokestatic
+ } else if (ownerType.isInterface()) {
+ reference =
+ classFile.addConstantInterfaceMethodrefInfo(
+ ownerDescriptor, targetName, targetDescriptor);
+ opcode = 0xb9; // invokeinterface
+ } else {
+ reference = classFile.addConstantMethodrefInfo(ownerDescriptor, targetName, targetDescriptor);
+ opcode = 0xb6; // invokevirtual
+ }
+ writeOpcodeIndex(code, opcode, reference);
+ if (opcode == 0xb9) {
+ code.write(parameterSlots(targetParameters) + 1);
+ code.write(0);
+ }
+ writeReturn(code, invocation.returnType);
+ return code.toByteArray();
+ }
+
+ private static void validateDirectInvocation(DirectInvocation invocation) {
+ if (invocation.bridgeName.isEmpty()) {
+ throw new IllegalArgumentException("Direct invocation bridge name is empty");
+ }
+ Class<?>[] targetParameters = invocation.target.getParameterTypes();
+ if (targetParameters.length != invocation.argumentIndexes.length) {
+ throw new IllegalArgumentException("Direct invocation argument shape does not match target");
+ }
+ if (invocation.target instanceof Constructor) {
+ if (invocation.returnType != invocation.target.getDeclaringClass()
+ || invocation.receiverIndex != -1) {
+ throw new IllegalArgumentException("Invalid direct constructor bridge shape");
+ }
+ } else {
+ Method method = (Method) invocation.target;
+ if (invocation.returnType != method.getReturnType()) {
+ throw new IllegalArgumentException(
+ "Direct method bridge return type does not match target");
+ }
+ if (Modifier.isStatic(method.getModifiers())) {
+ if (invocation.receiverIndex != -1) {
+ throw new IllegalArgumentException("Static direct method cannot have a receiver");
+ }
+ } else if (invocation.receiverIndex < 0
+ || invocation.receiverIndex >= invocation.parameterTypes.length
+ || !method
+ .getDeclaringClass()
+ .isAssignableFrom(invocation.parameterTypes[invocation.receiverIndex])) {
+ throw new IllegalArgumentException("Invalid direct method receiver");
+ }
+ }
+ for (int i = 0; i < targetParameters.length; i++) {
+ int source = invocation.argumentIndexes[i];
+ if (source < 0) {
+ if (targetParameters[i].isPrimitive()) {
+ throw new IllegalArgumentException("Null cannot supply a primitive direct argument");
+ }
+ } else if (source >= invocation.parameterTypes.length
+ || invocation.parameterTypes[source] != targetParameters[i]) {
+ throw new IllegalArgumentException("Direct invocation argument type does not match target");
+ }
+ }
+ }
+
+ private static int localIndex(DirectInvocation invocation, int parameterIndex) {
+ // Direct placeholders are generated-codec instance methods. Target staticness affects only the
+ // invocation opcode; local slot zero always remains the generated-codec receiver.
+ int index = 1;
+ for (int i = 0; i < parameterIndex; i++) {
+ index += slots(invocation.parameterTypes[i]);
+ }
+ return index;
+ }
+
+ private static int parameterSlots(Class<?>[] types) {
+ int slots = 0;
+ for (Class<?> type : types) {
+ slots += slots(type);
+ }
+ return slots;
+ }
+
+ private static int slots(Class<?> type) {
+ return type == long.class || type == double.class ? 2 : 1;
+ }
+
+ private static void writeLoad(ByteArrayOutputStream code, Class<?> type, int index) {
+ int opcode;
+ if (!type.isPrimitive()) {
+ opcode = 0x19; // aload
+ } else if (type == long.class) {
+ opcode = 0x16; // lload
+ } else if (type == float.class) {
+ opcode = 0x17; // fload
+ } else if (type == double.class) {
+ opcode = 0x18; // dload
+ } else {
+ opcode = 0x15; // iload
+ }
+ int compactBase;
+ switch (opcode) {
+ case 0x15:
+ compactBase = 0x1a;
+ break;
+ case 0x16:
+ compactBase = 0x1e;
+ break;
+ case 0x17:
+ compactBase = 0x22;
+ break;
+ case 0x18:
+ compactBase = 0x26;
+ break;
+ default:
+ compactBase = 0x2a;
+ }
+ if (index <= 3) {
+ code.write(compactBase + index);
+ } else if (index <= 255) {
+ code.write(opcode);
+ code.write(index);
+ } else {
+ code.write(0xc4); // wide
+ code.write(opcode);
+ writeShort(code, index);
+ }
+ }
+
+ private static void writeOpcodeIndex(ByteArrayOutputStream code, int opcode, short index) {
+ code.write(opcode);
+ writeShort(code, index & 0xffff);
+ }
+
+ private static void writeShort(ByteArrayOutputStream code, int value) {
+ code.write(value >>> 8);
+ code.write(value);
+ }
+
+ private static void writeReturn(ByteArrayOutputStream code, Class<?> type) {
+ if (type == void.class) {
+ code.write(0xb1);
+ } else if (!type.isPrimitive()) {
+ code.write(0xb0);
+ } else if (type == long.class) {
+ code.write(0xad);
+ } else if (type == float.class) {
+ code.write(0xae);
+ } else if (type == double.class) {
+ code.write(0xaf);
+ } else {
+ code.write(0xac);
+ }
+ }
+
+ private static String methodDescriptor(Class<?> returnType, Class<?>[] parameterTypes) {
+ StringBuilder descriptor = new StringBuilder("(");
+ for (Class<?> parameterType : parameterTypes) {
+ descriptor.append(typeDescriptor(parameterType));
+ }
+ return descriptor.append(')').append(typeDescriptor(returnType)).toString();
+ }
+
+ private static String typeDescriptor(Class<?> type) {
+ if (type.isPrimitive()) {
+ if (type == void.class) {
+ return "V";
+ }
+ if (type == boolean.class) {
+ return "Z";
+ }
+ if (type == byte.class) {
+ return "B";
+ }
+ if (type == char.class) {
+ return "C";
+ }
+ if (type == short.class) {
+ return "S";
+ }
+ if (type == int.class) {
+ return "I";
+ }
+ if (type == long.class) {
+ return "J";
+ }
+ if (type == float.class) {
+ return "F";
+ }
+ if (type == double.class) {
+ return "D";
+ }
+ throw new AssertionError(type);
+ }
+ if (type.isArray()) {
+ return type.getName().replace('.', '/');
+ }
+ return 'L' + type.getName().replace('.', '/') + ';';
+ }
+
public static class CodeStats {
public final Map<String, Integer> methodsSize;
public final int constPoolSize;
diff --git a/java/fory-core/src/main/java/org/apache/fory/meta/FieldTypes.java b/java/fory-core/src/main/java/org/apache/fory/meta/FieldTypes.java
index d3fba549d..e19cf1ab5 100644
--- a/java/fory-core/src/main/java/org/apache/fory/meta/FieldTypes.java
+++ b/java/fory-core/src/main/java/org/apache/fory/meta/FieldTypes.java
@@ -1390,7 +1390,11 @@ public class FieldTypes {
int typeId, boolean nullable, boolean trackingRef, TypeRef<?> declared) {
TypeExtMeta declaredMeta = declared == null ? null : declared.getTypeExtMeta();
return TypeExtMeta.of(
- typeId, nullable, trackingRef, declaredMeta != null && declaredMeta.nullableWrapper());
+ typeId,
+ nullable,
+ trackingRef,
+ declaredMeta != null && declaredMeta.nullableWrapper(),
+ declaredMeta != null && declaredMeta.covariant());
}
/** Class for Union field type. Union types use declared type. */
diff --git a/java/fory-core/src/main/java/org/apache/fory/meta/TypeExtMeta.java b/java/fory-core/src/main/java/org/apache/fory/meta/TypeExtMeta.java
index d0be96865..689bff3d0 100644
--- a/java/fory-core/src/main/java/org/apache/fory/meta/TypeExtMeta.java
+++ b/java/fory-core/src/main/java/org/apache/fory/meta/TypeExtMeta.java
@@ -26,6 +26,7 @@ public class TypeExtMeta {
private final boolean nullable;
private final boolean trackingRef;
private final boolean nullableWrapper;
+ private final boolean covariant;
public static TypeExtMeta of(int typeId, boolean nullable, boolean trackingRef) {
return new TypeExtMeta(typeId, nullable, trackingRef);
@@ -36,15 +37,34 @@ public class TypeExtMeta {
return new TypeExtMeta(typeId, nullable, trackingRef, nullableWrapper);
}
+ public static TypeExtMeta of(
+ int typeId,
+ boolean nullable,
+ boolean trackingRef,
+ boolean nullableWrapper,
+ boolean covariant) {
+ return new TypeExtMeta(typeId, nullable, trackingRef, nullableWrapper, covariant);
+ }
+
TypeExtMeta(int typeId, boolean nullable, boolean trackingRef) {
this(typeId, nullable, trackingRef, false);
}
TypeExtMeta(int typeId, boolean nullable, boolean trackingRef, boolean nullableWrapper) {
+ this(typeId, nullable, trackingRef, nullableWrapper, false);
+ }
+
+ TypeExtMeta(
+ int typeId,
+ boolean nullable,
+ boolean trackingRef,
+ boolean nullableWrapper,
+ boolean covariant) {
this.typeId = typeId;
this.nullable = nullable;
this.trackingRef = trackingRef;
this.nullableWrapper = nullableWrapper;
+ this.covariant = covariant;
}
public int typeId() {
@@ -64,6 +84,11 @@ public class TypeExtMeta {
return nullableWrapper;
}
+ /** Whether this declared occurrence is the producer of a covariant type projection. */
+ public boolean covariant() {
+ return covariant;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
@@ -76,12 +101,13 @@ public class TypeExtMeta {
return typeId == that.typeId
&& nullable == that.nullable
&& trackingRef == that.trackingRef
- && nullableWrapper == that.nullableWrapper;
+ && nullableWrapper == that.nullableWrapper
+ && covariant == that.covariant;
}
@Override
public int hashCode() {
- return Objects.hash(typeId, nullable, trackingRef, nullableWrapper);
+ return Objects.hash(typeId, nullable, trackingRef, nullableWrapper, covariant);
}
@Override
@@ -95,6 +121,8 @@ public class TypeExtMeta {
+ trackingRef
+ ", nullableWrapper="
+ nullableWrapper
+ + ", covariant="
+ + covariant
+ '}';
}
}
diff --git a/java/fory-core/src/main/java/org/apache/fory/reflect/TypeRef.java b/java/fory-core/src/main/java/org/apache/fory/reflect/TypeRef.java
index a12f49675..6a178828a 100644
--- a/java/fory-core/src/main/java/org/apache/fory/reflect/TypeRef.java
+++ b/java/fory-core/src/main/java/org/apache/fory/reflect/TypeRef.java
@@ -542,6 +542,8 @@ public class TypeRef<T> {
builder.append('#').append(typeExtMeta.typeId());
builder.append(':').append(typeExtMeta.nullable() ? '1' : '0');
builder.append(':').append(typeExtMeta.trackingRef() ? '1' : '0');
+ builder.append(':').append(typeExtMeta.nullableWrapper() ? '1' : '0');
+ builder.append(':').append(typeExtMeta.covariant() ? '1' : '0');
}
if (typeArguments != null && !typeArguments.isEmpty()) {
builder.append('<');
diff --git a/java/fory-core/src/test/java/org/apache/fory/reflect/TypeRefTest.java b/java/fory-core/src/test/java/org/apache/fory/reflect/TypeRefTest.java
index 46d70f46f..870f0304a 100644
--- a/java/fory-core/src/test/java/org/apache/fory/reflect/TypeRefTest.java
+++ b/java/fory-core/src/test/java/org/apache/fory/reflect/TypeRefTest.java
@@ -393,6 +393,24 @@ public class TypeRefTest extends ForyTestBase {
Assert.assertFalse(fixedElementType.hasTypeExtMeta());
}
+ @Test
+ public void testTypeKeyMetadata() {
+ TypeRef<?> plain =
+ TypeRef.of(String.class, TypeExtMeta.of(Types.UNKNOWN, false, false, false, false));
+ TypeRef<?> wrapped =
+ TypeRef.of(String.class, TypeExtMeta.of(Types.UNKNOWN, false, false, true, false));
+ TypeRef<?> covariant =
+ TypeRef.of(String.class, TypeExtMeta.of(Types.UNKNOWN, false, false, false, true));
+ TypeRef<?> both =
+ TypeRef.of(String.class, TypeExtMeta.of(Types.UNKNOWN, false, false, true, true));
+ assertNotEquals(plain.getTypeKey(), wrapped.getTypeKey());
+ assertNotEquals(plain.getTypeKey(), covariant.getTypeKey());
+ assertNotEquals(plain.getTypeKey(), both.getTypeKey());
+ assertNotEquals(wrapped.getTypeKey(), covariant.getTypeKey());
+ assertNotEquals(wrapped.getTypeKey(), both.getTypeKey());
+ assertNotEquals(covariant.getTypeKey(), both.getTypeKey());
+ }
+
@Test
public void testScalaContainerTypeRefNormalization() throws Exception {
if (!ScalaTypes.SCALA_AVAILABLE) {
@@ -427,7 +445,7 @@ public class TypeRefTest extends ForyTestBase {
}
@Test
- public void testTypeUseMetadataKeepsNullableOwnership() throws Exception {
+ public void typeUseMetadataKeepsNullableOwner() throws Exception {
Field nicknameField = TypeUseMetadataStruct.class.getDeclaredField("nickname");
TypeRef<?> nicknameType = TypeRef.ofTypeUse(nicknameField.getAnnotatedType());
TypeExtMeta nicknameMeta = nicknameType.getTypeExtMeta();
diff --git a/java/fory-json/src/main/java/org/apache/fory/json/ForyJson.java b/java/fory-json/src/main/java/org/apache/fory/json/ForyJson.java
index baecb0cb8..bf356a38f 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/ForyJson.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/ForyJson.java
@@ -28,6 +28,7 @@ import java.lang.reflect.WildcardType;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;
+import org.apache.fory.json.reader.JsonReader;
import org.apache.fory.json.reader.Latin1JsonReader;
import org.apache.fory.json.reader.Utf16JsonReader;
import org.apache.fory.json.reader.Utf8JsonReader;
@@ -36,6 +37,7 @@ import org.apache.fory.json.resolver.JsonTypeInfo;
import org.apache.fory.json.resolver.JsonTypeResolver;
import org.apache.fory.json.writer.StringJsonWriter;
import org.apache.fory.json.writer.Utf8JsonWriter;
+import org.apache.fory.meta.TypeExtMeta;
import org.apache.fory.reflect.TypeRef;
import org.apache.fory.serializer.StringSerializer;
@@ -132,7 +134,7 @@ public final class ForyJson {
if (value == null) {
writer.writeNull();
} else {
- JsonTypeInfo typeInfo = state.rootTypeInfo(value.getClass());
+ JsonTypeInfo typeInfo = state.runtimeRootTypeInfo(value.getClass());
typeInfo.stringWriter().writeString(writer, value);
}
} finally {
@@ -162,7 +164,7 @@ public final class ForyJson {
public <T> String toJson(T value, Class<T> declaredType) {
requireDeclaredType(declaredType);
validateWriteValue(value, declaredType);
- return toJsonDeclared(value, declaredType, declaredType);
+ return toJsonDeclared(value, declaredType);
}
/**
@@ -178,9 +180,7 @@ public final class ForyJson {
public <T> String toJson(T value, TypeRef<T> declaredType) {
requireDeclaredType(declaredType);
validateDeclaredType(declaredType.getType());
- Class<?> rawType = declaredType.getRawType();
- validateWriteValue(value, rawType);
- return toJsonDeclared(value, declaredType.getType(), rawType);
+ return toJsonDeclared(value, declaredType);
}
/**
@@ -200,7 +200,7 @@ public final class ForyJson {
if (value == null) {
writer.writeNull();
} else {
- JsonTypeInfo typeInfo = state.rootTypeInfo(value.getClass());
+ JsonTypeInfo typeInfo = state.runtimeRootTypeInfo(value.getClass());
typeInfo.utf8Writer().writeUtf8(writer, value);
}
} finally {
@@ -226,7 +226,7 @@ public final class ForyJson {
public <T> byte[] toJsonBytes(T value, Class<T> declaredType) {
requireDeclaredType(declaredType);
validateWriteValue(value, declaredType);
- return toJsonBytesDeclared(value, declaredType, declaredType);
+ return toJsonBytesDeclared(value, declaredType);
}
/**
@@ -239,9 +239,7 @@ public final class ForyJson {
public <T> byte[] toJsonBytes(T value, TypeRef<T> declaredType) {
requireDeclaredType(declaredType);
validateDeclaredType(declaredType.getType());
- Class<?> rawType = declaredType.getRawType();
- validateWriteValue(value, rawType);
- return toJsonBytesDeclared(value, declaredType.getType(), rawType);
+ return toJsonBytesDeclared(value, declaredType);
}
/**
@@ -265,7 +263,7 @@ public final class ForyJson {
if (value == null) {
writer.writeNull();
} else {
- JsonTypeInfo typeInfo = state.rootTypeInfo(value.getClass());
+ JsonTypeInfo typeInfo = state.runtimeRootTypeInfo(value.getClass());
// Keep root dispatch direct so generated codecs own their own compilation boundaries.
typeInfo.utf8Writer().writeUtf8(writer, value);
}
@@ -293,7 +291,7 @@ public final class ForyJson {
public <T> void writeJsonTo(T value, Class<T> declaredType, OutputStream output) {
requireDeclaredType(declaredType);
validateWriteValue(value, declaredType);
- writeJsonDeclared(value, declaredType, declaredType, output);
+ writeJsonDeclared(value, declaredType, output);
}
/**
@@ -307,19 +305,38 @@ public final class ForyJson {
public <T> void writeJsonTo(T value, TypeRef<T> declaredType, OutputStream output) {
requireDeclaredType(declaredType);
validateDeclaredType(declaredType.getType());
- Class<?> rawType = declaredType.getRawType();
- validateWriteValue(value, rawType);
- writeJsonDeclared(value, declaredType.getType(), rawType, output);
+ writeJsonDeclared(value, declaredType, output);
+ }
+
+ private String toJsonDeclared(Object value, Class<?> type) {
+ PooledState entry = acquire();
+ JsonState state = entry.state;
+ StringJsonWriter writer = state.stringWriter;
+ try {
+ state.typeResolver.lockJIT();
+ try {
+ state.declaredRootTypeInfo(type).stringWriter().writeString(writer, value);
+ } finally {
+ state.typeResolver.unlockJIT();
+ }
+ return writer.toJson();
+ } finally {
+ try {
+ writer.reset();
+ } finally {
+ release(entry);
+ }
+ }
}
- private String toJsonDeclared(Object value, Type type, Class<?> fallback) {
+ private String toJsonDeclared(Object value, TypeRef<?> type) {
PooledState entry = acquire();
JsonState state = entry.state;
StringJsonWriter writer = state.stringWriter;
try {
state.typeResolver.lockJIT();
try {
- state.rootTypeInfo(type, fallback).stringWriter().writeString(writer, value);
+ state.declaredWriteTypeInfo(value, type).stringWriter().writeString(writer, value);
} finally {
state.typeResolver.unlockJIT();
}
@@ -333,7 +350,7 @@ public final class ForyJson {
}
}
- private byte[] toJsonBytesDeclared(Object value, Type type, Class<?> fallback) {
+ private byte[] toJsonBytesDeclared(Object value, Class<?> type) {
PooledState entry = acquire();
JsonState state = entry.state;
Utf8JsonWriter writer = state.utf8Writer;
@@ -341,7 +358,28 @@ public final class ForyJson {
state.typeResolver.lockJIT();
try {
// Declared root dispatch stays direct; generated codecs own their own boundaries.
- state.rootTypeInfo(type, fallback).utf8Writer().writeUtf8(writer, value);
+ state.declaredRootTypeInfo(type).utf8Writer().writeUtf8(writer, value);
+ } finally {
+ state.typeResolver.unlockJIT();
+ }
+ return writer.toJsonBytes();
+ } finally {
+ try {
+ writer.reset();
+ } finally {
+ release(entry);
+ }
+ }
+ }
+
+ private byte[] toJsonBytesDeclared(Object value, TypeRef<?> type) {
+ PooledState entry = acquire();
+ JsonState state = entry.state;
+ Utf8JsonWriter writer = state.utf8Writer;
+ try {
+ state.typeResolver.lockJIT();
+ try {
+ state.declaredWriteTypeInfo(value, type).utf8Writer().writeUtf8(writer, value);
} finally {
state.typeResolver.unlockJIT();
}
@@ -355,7 +393,7 @@ public final class ForyJson {
}
}
- private void writeJsonDeclared(Object value, Type type, Class<?> fallback, OutputStream output) {
+ private void writeJsonDeclared(Object value, Class<?> type, OutputStream output) {
Objects.requireNonNull(output, "output");
PooledState entry = acquire();
JsonState state = entry.state;
@@ -363,7 +401,29 @@ public final class ForyJson {
try {
state.typeResolver.lockJIT();
try {
- state.rootTypeInfo(type, fallback).utf8Writer().writeUtf8(writer, value);
+ state.declaredRootTypeInfo(type).utf8Writer().writeUtf8(writer, value);
+ } finally {
+ state.typeResolver.unlockJIT();
+ }
+ writer.writeTo(output);
+ } finally {
+ try {
+ writer.reset();
+ } finally {
+ release(entry);
+ }
+ }
+ }
+
+ private void writeJsonDeclared(Object value, TypeRef<?> type, OutputStream output) {
+ Objects.requireNonNull(output, "output");
+ PooledState entry = acquire();
+ JsonState state = entry.state;
+ Utf8JsonWriter writer = state.utf8Writer;
+ try {
+ state.typeResolver.lockJIT();
+ try {
+ state.declaredWriteTypeInfo(value, type).utf8Writer().writeUtf8(writer, value);
} finally {
state.typeResolver.unlockJIT();
}
@@ -396,6 +456,24 @@ public final class ForyJson {
}
}
+ private static void validateWriteValue(Object value, TypeRef<?> declaredType) {
+ validateWriteValue(value, declaredType, declaredType.getRawType());
+ }
+
+ private static void validateWriteValue(Object value, TypeRef<?> declaredType, Class<?> rawType) {
+ TypeExtMeta typeExtMeta = declaredType.getTypeExtMeta();
+ if (rawType == Void.class && typeExtMeta != null && typeExtMeta.nullable()) {
+ if (value != null) {
+ throw new IllegalArgumentException("Nothing? accepts only null");
+ }
+ return;
+ }
+ validateWriteValue(value, rawType);
+ if (value == null && typeExtMeta != null && !typeExtMeta.nullable()) {
+ throw new IllegalArgumentException("Cannot write null as non-null " + declaredType);
+ }
+ }
+
private static void requireDeclaredType(Object declaredType) {
if (declaredType == null) {
throw new IllegalArgumentException("declaredType must not be null");
@@ -462,7 +540,7 @@ public final class ForyJson {
try {
state.typeResolver.lockJIT();
try {
- return castValue(readJavaStringValue(json, type, type, state), type);
+ return castValue(readJavaStringValue(json, type, state), type);
} finally {
state.typeResolver.unlockJIT();
}
@@ -490,7 +568,7 @@ public final class ForyJson {
try {
state.typeResolver.lockJIT();
try {
- Object value = readJavaStringValue(json, typeRef.getType(), typeRef.getRawType(), state);
+ Object value = readJavaStringValue(json, typeRef, state);
return castValue(value, typeRef);
} finally {
state.typeResolver.unlockJIT();
@@ -518,7 +596,7 @@ public final class ForyJson {
try {
state.typeResolver.lockJIT();
try {
- return castValue(readUtf8Value(state.utf8Reader(bytes), type, type, state), type);
+ return castValue(readUtf8Value(state.utf8Reader(bytes), type, state), type);
} finally {
state.typeResolver.unlockJIT();
}
@@ -545,8 +623,7 @@ public final class ForyJson {
try {
state.typeResolver.lockJIT();
try {
- Object value =
- readUtf8Value(state.utf8Reader(bytes), typeRef.getType(), typeRef.getRawType(), state);
+ Object value = readUtf8Value(state.utf8Reader(bytes), typeRef, state);
return castValue(value, typeRef);
} finally {
state.typeResolver.unlockJIT();
@@ -625,45 +702,101 @@ public final class ForyJson {
return hash ^ (hash >>> 16);
}
- private Object readJavaStringValue(String json, Type type, Class<?> fallback, JsonState state) {
+ private Object readJavaStringValue(String json, Class<?> type, JsonState state) {
if (StringSerializer.isBytesBackedString()) {
byte coder = StringSerializer.getStringCoder(json);
if (StringSerializer.isLatin1Coder(coder)) {
// Keep String input on its reader owner even when ASCII Latin1 bytes match UTF-8;
// custom JsonValueCodec implementations can observe readLatin1/readUtf16 dispatch.
- return readLatin1Value(state.latin1Reader(json), type, fallback, state);
+ return readLatin1Value(state.latin1Reader(json), type, state);
}
if (StringSerializer.isUtf16Coder(coder)) {
- return readUtf16Value(state.utf16Reader(json), type, fallback, state);
+ return readUtf16Value(state.utf16Reader(json), type, state);
}
}
- return readUtf16Value(state.charBackedUtf16Reader(json), type, fallback, state);
+ return readUtf16Value(state.charBackedUtf16Reader(json), type, state);
}
- private Object readLatin1Value(
- Latin1JsonReader reader, Type type, Class<?> fallback, JsonState state) {
- JsonTypeInfo typeInfo = state.rootTypeInfo(type, fallback);
+ private Object readJavaStringValue(String json, TypeRef<?> type, JsonState state) {
+ if (StringSerializer.isBytesBackedString()) {
+ byte coder = StringSerializer.getStringCoder(json);
+ if (StringSerializer.isLatin1Coder(coder)) {
+ return readLatin1Value(state.latin1Reader(json), type, state);
+ }
+ if (StringSerializer.isUtf16Coder(coder)) {
+ return readUtf16Value(state.utf16Reader(json), type, state);
+ }
+ }
+ return readUtf16Value(state.charBackedUtf16Reader(json), type, state);
+ }
+
+ private Object readLatin1Value(Latin1JsonReader reader, Class<?> type, JsonState state) {
+ JsonTypeInfo typeInfo = state.declaredRootTypeInfo(type);
Object value = typeInfo.latin1Reader().readLatin1(reader);
reader.finish();
return value;
}
- private Object readUtf16Value(
- Utf16JsonReader reader, Type type, Class<?> fallback, JsonState state) {
- JsonTypeInfo typeInfo = state.rootTypeInfo(type, fallback);
+ private Object readLatin1Value(Latin1JsonReader reader, TypeRef<?> type, JsonState state) {
+ JsonTypeInfo typeInfo = state.declaredRootTypeInfo(type);
+ if (readOuterNull(reader, type, typeInfo)) {
+ reader.finish();
+ return null;
+ }
+ Object value = typeInfo.latin1Reader().readLatin1(reader);
+ reader.finish();
+ return value;
+ }
+
+ private Object readUtf16Value(Utf16JsonReader reader, Class<?> type, JsonState state) {
+ JsonTypeInfo typeInfo = state.declaredRootTypeInfo(type);
+ Object value = typeInfo.utf16Reader().readUtf16(reader);
+ reader.finish();
+ return value;
+ }
+
+ private Object readUtf16Value(Utf16JsonReader reader, TypeRef<?> type, JsonState state) {
+ JsonTypeInfo typeInfo = state.declaredRootTypeInfo(type);
+ if (readOuterNull(reader, type, typeInfo)) {
+ reader.finish();
+ return null;
+ }
Object value = typeInfo.utf16Reader().readUtf16(reader);
reader.finish();
return value;
}
- private Object readUtf8Value(
- Utf8JsonReader reader, Type type, Class<?> fallback, JsonState state) {
- JsonTypeInfo typeInfo = state.rootTypeInfo(type, fallback);
+ private Object readUtf8Value(Utf8JsonReader reader, Class<?> type, JsonState state) {
+ JsonTypeInfo typeInfo = state.declaredRootTypeInfo(type);
Object value = typeInfo.utf8Reader().readUtf8(reader);
reader.finish();
return value;
}
+ private Object readUtf8Value(Utf8JsonReader reader, TypeRef<?> type, JsonState state) {
+ JsonTypeInfo typeInfo = state.declaredRootTypeInfo(type);
+ if (readOuterNull(reader, type, typeInfo)) {
+ reader.finish();
+ return null;
+ }
+ Object value = typeInfo.utf8Reader().readUtf8(reader);
+ reader.finish();
+ return value;
+ }
+
+ private static boolean readOuterNull(JsonReader reader, TypeRef<?> type, JsonTypeInfo typeInfo) {
+ if (!typeInfo.nullable() && !typeInfo.rejectsNull()) {
+ return false;
+ }
+ if (!reader.tryReadNull()) {
+ return false;
+ }
+ if (typeInfo.rejectsNull()) {
+ throw new ForyJsonException("Cannot read null as non-null " + type);
+ }
+ return true;
+ }
+
@SuppressWarnings("unchecked")
private static <T> T castValue(Object value, Class<T> type) {
if (!type.isPrimitive()) {
@@ -717,8 +850,9 @@ public final class ForyJson {
*
* <p>The resolver is constructed first and retained by all five readers and writers. Codecs
* obtain dynamic child bindings from the active reader or writer instead of receiving a resolver
- * through every capability call. The last-root cache is state-local and only avoids repeated
- * resolver lookup for an identical declared type and fallback pair.
+ * through every capability call. The runtime-class cache and declared-token cache each avoid
+ * resolver lookup only on an identity hit; the declared cache also retains the resolved raw type
+ * used by typed-root write validation.
*/
private static final class JsonState {
private final JsonTypeResolver typeResolver;
@@ -730,9 +864,12 @@ public final class ForyJson {
private byte[] charBackedUtf16Bytes;
private Class<?> lastRuntimeRootType;
private JsonTypeInfo lastRuntimeRootInfo;
- private Type lastRootType;
- private Class<?> lastRootFallback;
- private JsonTypeInfo lastRootInfo;
+ // Keep Class and TypeRef roots in one declared-token cache. Separate caches give C2 competing
+ // guards that can be folded into primitive-array loops and destabilize otherwise direct signed
+ // array code; the retained raw type also avoids structural TypeRef traversal on typed writes.
+ private Object lastDeclaredRootType;
+ private Class<?> lastDeclaredRootRawType;
+ private JsonTypeInfo lastDeclaredRootInfo;
private JsonState(JsonConfig config, JsonSharedRegistry sharedRegistry) {
typeResolver = new JsonTypeResolver(sharedRegistry);
@@ -792,7 +929,7 @@ public final class ForyJson {
utf8Reader.clear();
}
- private JsonTypeInfo rootTypeInfo(Class<?> type) {
+ private JsonTypeInfo runtimeRootTypeInfo(Class<?> type) {
JsonTypeInfo typeInfo = lastRuntimeRootInfo;
if (lastRuntimeRootType == type && typeInfo != null) {
return typeInfo;
@@ -803,16 +940,42 @@ public final class ForyJson {
return typeInfo;
}
- private JsonTypeInfo rootTypeInfo(Type type, Class<?> fallback) {
- JsonTypeInfo typeInfo = lastRootInfo;
- if (lastRootType == type && lastRootFallback == fallback && typeInfo != null) {
+ private JsonTypeInfo declaredRootTypeInfo(Class<?> type) {
+ JsonTypeInfo typeInfo = lastDeclaredRootInfo;
+ if (lastDeclaredRootType == type && lastDeclaredRootRawType == type && typeInfo != null) {
return typeInfo;
}
- typeInfo = typeResolver.getTypeInfo(type, fallback);
- lastRootType = type;
- lastRootFallback = fallback;
- lastRootInfo = typeInfo;
+ // Keep Class roots on the resolver's identity-key path. Converting here to TypeRef would
+ // allocate on every alternating-root state-cache miss even when the schema is already bound.
+ typeInfo = typeResolver.getTypeInfo(type, type);
+ lastDeclaredRootType = type;
+ lastDeclaredRootRawType = type;
+ lastDeclaredRootInfo = typeInfo;
return typeInfo;
}
+
+ private JsonTypeInfo declaredRootTypeInfo(TypeRef<?> type) {
+ JsonTypeInfo typeInfo = lastDeclaredRootInfo;
+ if (lastDeclaredRootType == type && typeInfo != null) {
+ return typeInfo;
+ }
+ typeInfo = typeResolver.getTypeInfo(type);
+ lastDeclaredRootType = type;
+ lastDeclaredRootRawType = typeInfo.rawType();
+ lastDeclaredRootInfo = typeInfo;
+ return typeInfo;
+ }
+
+ private JsonTypeInfo declaredWriteTypeInfo(Object value, TypeRef<?> type) {
+ JsonTypeInfo typeInfo = lastDeclaredRootInfo;
+ if (lastDeclaredRootType == type && typeInfo != null) {
+ // The exact-root cache already owns the canonical raw type. Recomputing it from TypeRef on
+ // every typed write repeats structural type traversal in the root hot path.
+ validateWriteValue(value, type, lastDeclaredRootRawType);
+ return typeInfo;
+ }
+ validateWriteValue(value, type);
+ return declaredRootTypeInfo(type);
+ }
}
}
diff --git a/java/fory-json/src/main/java/org/apache/fory/json/JsonCodecFactory.java b/java/fory-json/src/main/java/org/apache/fory/json/JsonCodecFactory.java
index cbc24cd90..f4af72022 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/JsonCodecFactory.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/JsonCodecFactory.java
@@ -29,8 +29,13 @@ import org.apache.fory.reflect.TypeRef;
/** Creates a complete JSON codec during a resolver-owned cold type lookup. */
@FunctionalInterface
public interface JsonCodecFactory {
- /** Returns a codec for {@code type}, or {@code null} when this factory does not own it. */
- JsonValueCodec<?> create(TypeRef<?> type, JsonTypeResolver resolver);
+ /**
+ * Returns a codec for {@code type}, or {@code null} when this factory does not own it.
+ *
+ * @param runtimeType true only when {@code type} was selected from an actual value during a
+ * dynamic write; factories must not infer this from resolver state
+ */
+ JsonValueCodec<?> create(TypeRef<?> type, JsonTypeResolver resolver, boolean runtimeType);
/** Returns the deterministic semantic identity of this factory configuration. */
default String factoryKey() {
diff --git a/java/fory-json/src/main/java/org/apache/fory/json/JsonConfig.java b/java/fory-json/src/main/java/org/apache/fory/json/JsonConfig.java
index 1bd980d08..ca5010259 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/JsonConfig.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/JsonConfig.java
@@ -26,9 +26,6 @@ import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.atomic.AtomicInteger;
import org.apache.fory.annotation.Internal;
import org.apache.fory.json.codegen.JsonCodegenKey;
import org.apache.fory.json.resolver.CodecRegistry;
@@ -37,10 +34,10 @@ import org.apache.fory.json.resolver.CodecRegistry;
* Build configuration used to create all pooled states of one {@link ForyJson} instance.
*
* <p>Scalar settings and the codec registry are snapshotted at construction; the JSON runtime never
- * observes later builder mutation. {@link #getCodegenHash()} identifies only settings that can
- * change generated source; runtime-only settings such as depth, graph memory, and asynchronous
- * scheduling do not fragment generated class names. Concurrency, per-reader field-name cache, and
- * retained writer-buffer limits are also runtime-only and do not fragment generated class names.
+ * observes later builder mutation. {@link JsonCodegenKey} identifies only settings that can change
+ * generated source; runtime-only settings such as depth, graph memory, and asynchronous scheduling
+ * do not fragment generated class names. Concurrency, per-reader field-name cache, and retained
+ * writer-buffer limits are also runtime-only and do not fragment generated class names.
*/
public final class JsonConfig {
private static final int MAX_CACHED_FIELD_NAMES = 1 << 29;
@@ -63,7 +60,6 @@ public final class JsonConfig {
private final JsonTypeChecker typeChecker;
private final JsonTypeCheckContext typeCheckContext;
private final JsonCodegenKey codegenKey;
- private transient int codegenHash;
JsonConfig(
boolean writeNullFields,
@@ -247,21 +243,6 @@ public final class JsonConfig {
builder.append(value.length()).append(':').append(value);
}
- private static final AtomicInteger COUNTER = new AtomicInteger(0);
-
- // Equal generated source inputs share one map entry, following core generated-code naming model.
- // This process-wide map retains only immutable configuration text and integers, never user
- // classes, codec instances, class loaders, or generated classes.
- private static final ConcurrentMap<JsonCodegenKey, Integer> CODEGEN_ID_MAP =
- new ConcurrentHashMap<>();
-
- public int getCodegenHash() {
- if (codegenHash == 0) {
- codegenHash = CODEGEN_ID_MAP.computeIfAbsent(codegenKey, key -> COUNTER.incrementAndGet());
- }
- return codegenHash;
- }
-
/** Returns the immutable generated-source identity for this configuration. */
@Internal
public JsonCodegenKey codegenKey() {
diff --git a/java/fory-json/src/main/java/org/apache/fory/json/annotation/ForyJsonProvider.java b/java/fory-json/src/main/java/org/apache/fory/json/annotation/ForyJsonProvider.java
index 5384d872c..9d6536da4 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/annotation/ForyJsonProvider.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/annotation/ForyJsonProvider.java
@@ -33,8 +33,10 @@ import org.apache.fory.json.ForyJson;
* effective public, non-static, zero-argument instance method whose exact return type is {@link
* ForyJson} is invoked once while the native image is built. This includes inherited superclass
* methods and public interface default methods. The returned configurations select the generated
- * object codecs included in the image; configurations not returned by a provider continue to use
- * interpreted codecs. The provider package does not need to be exported or opened to Fory.
+ * object codecs included in the image. Configurations not returned by a provider continue to use
+ * interpreted codecs for ordinary Java models and complete value codecs; language-module object
+ * models require a returned generated configuration. The provider package does not need to be
+ * exported or opened to Fory.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
diff --git a/java/fory-json/src/main/java/org/apache/fory/json/annotation/JsonType.java b/java/fory-json/src/main/java/org/apache/fory/json/annotation/JsonType.java
index 7ac3f672b..8a3de0de9 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/annotation/JsonType.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/annotation/JsonType.java
@@ -26,17 +26,23 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
- * Marks a JSON model for build-time generated execution and retention metadata.
+ * Marks a JSON model for build-time language processing and retention metadata.
*
- * <p>The Fory annotation processor generates a type-owned JSON companion for an eligible concrete
- * object model or a Record with an effective {@link JsonValue}, together with exact R8 rules for
- * the model, companion, and codec classes selected by its {@link JsonCodec} declarations. The
- * companion provides direct member access and creator invocation on the JVM and Android. GraalVM
- * Native Image discovers this annotation directly and does not use annotation-processor output. A
- * concrete subtype listed only by a class-literal {@link JsonSubTypes} entry receives retention
- * metadata but needs its own direct {@code JsonType} annotation to receive a companion on the JVM
- * or Android. Outside Native Image, a directly annotated model that reaches the default object
- * codec fails during codec creation when its generated companion is missing.
+ * <p>The Java Fory annotation processor generates a type-owned JSON companion for an eligible
+ * concrete object model or a Record with an effective {@link JsonValue}, together with exact R8
+ * rules for the model, companion, and codec classes selected by its {@link JsonCodec} declarations.
+ * The companion provides direct member access and creator invocation on the JVM and Android. The
+ * Kotlin Symbol Processing (KSP) processor emits exact retention rules only; the installed Kotlin
+ * module supplies the metadata-backed object model used by interpreted and generated execution.
+ * GraalVM Native Image discovers this annotation directly and does not require annotation-processor
+ * output. Hosted analysis snapshots a Java companion when one is available, but reflection
+ * reachability and hosted-generated capabilities remain sufficient without it.
+ *
+ * <p>A concrete Java subtype listed only by a class-literal {@link JsonSubTypes} entry receives
+ * retention metadata but needs its own direct {@code JsonType} annotation to receive a Java
+ * companion on the JVM or Android. Outside Native Image, a directly annotated Java model that
+ * reaches the default object codec fails during codec creation when its generated companion is
+ * missing.
*
* <p>This annotation does not change the JSON schema and is intentionally not inherited. An
* ordinary mutable class may omit it and use reflection, with application-authored exact R8 rules
@@ -45,8 +51,9 @@ import java.lang.annotation.Target;
* exact {@link JsonMixin} pair because Android does not expose the Java Record reflection APIs.
*
* <p>{@link JsonMixin} cannot contribute or remove this build-time marker. A non-empty Mixin is its
- * own processor entry point and produces the platform configuration and generated operations
- * required by its exact target/source mapping.
+ * own processor entry point. The Java annotation processor generates the operations and platform
+ * configuration for its exact target/source mapping; Kotlin KSP emits only the corresponding
+ * retention rules.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
diff --git a/java/fory-json/src/main/java/org/apache/fory/json/codec/ArrayCodec.java b/java/fory-json/src/main/java/org/apache/fory/json/codec/ArrayCodec.java
index a84fe034f..cf3499eec 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/codec/ArrayCodec.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/codec/ArrayCodec.java
@@ -29,11 +29,13 @@ import org.apache.fory.json.reader.Utf16JsonReader;
import org.apache.fory.json.reader.Utf8JsonReader;
import org.apache.fory.json.resolver.JsonTypeInfo;
import org.apache.fory.json.resolver.JsonTypeResolver;
+import org.apache.fory.json.writer.JsonWriter;
import org.apache.fory.json.writer.StringJsonWriter;
import org.apache.fory.json.writer.Utf8JsonWriter;
import org.apache.fory.reflect.TypeRef;
import org.apache.fory.serializer.GraphMemoryEstimates;
import org.apache.fory.type.TypeUtils;
+import org.apache.fory.type.Types;
/**
* Codec family for Java primitive, boxed, String, and object arrays.
@@ -63,8 +65,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
Class<?> componentType = arrayType.getComponentType();
TypeRef<?> componentTypeRef = arrayTypeRef.getComponentType();
- JsonTypeInfo componentTypeInfo =
- resolver.getTypeInfo(componentTypeRef.getType(), componentType);
+ JsonTypeInfo componentTypeInfo = resolver.getTypeInfo(componentTypeRef);
return create(arrayType, componentTypeInfo);
}
@@ -96,30 +97,76 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
&& componentCodec == ScalarCodecs.DoubleCodec.PRIMITIVE) {
return bind(DoubleArrayCodec.INSTANCE);
} else if (componentType == Integer.class && componentCodec == ScalarCodecs.IntCodec.BOXED) {
- return bind(BoxedIntArrayCodec.INSTANCE);
+ return bind(
+ componentTypeInfo.rejectsNull()
+ ? BoxedIntArrayCodec.NON_NULL
+ : BoxedIntArrayCodec.INSTANCE);
} else if (componentType == Long.class && componentCodec == ScalarCodecs.LongCodec.BOXED) {
- return bind(BoxedLongArrayCodec.INSTANCE);
+ return bind(
+ componentTypeInfo.rejectsNull()
+ ? BoxedLongArrayCodec.NON_NULL
+ : BoxedLongArrayCodec.INSTANCE);
} else if (componentType == Boolean.class
&& componentCodec == ScalarCodecs.BooleanCodec.BOXED) {
- return bind(BoxedBooleanArrayCodec.INSTANCE);
+ return bind(
+ componentTypeInfo.rejectsNull()
+ ? BoxedBooleanArrayCodec.NON_NULL
+ : BoxedBooleanArrayCodec.INSTANCE);
} else if (componentType == Short.class && componentCodec == ScalarCodecs.ShortCodec.BOXED) {
- return bind(BoxedShortArrayCodec.INSTANCE);
+ return bind(
+ componentTypeInfo.rejectsNull()
+ ? BoxedShortArrayCodec.NON_NULL
+ : BoxedShortArrayCodec.INSTANCE);
} else if (componentType == Byte.class && componentCodec == ScalarCodecs.ByteCodec.BOXED) {
- return bind(BoxedByteArrayCodec.INSTANCE);
+ return bind(
+ componentTypeInfo.rejectsNull()
+ ? BoxedByteArrayCodec.NON_NULL
+ : BoxedByteArrayCodec.INSTANCE);
} else if (componentType == Character.class && componentCodec == ScalarCodecs.CharCodec.BOXED) {
- return bind(BoxedCharArrayCodec.INSTANCE);
+ return bind(
+ componentTypeInfo.rejectsNull()
+ ? BoxedCharArrayCodec.NON_NULL
+ : BoxedCharArrayCodec.INSTANCE);
} else if (componentType == Float.class && componentCodec == ScalarCodecs.FloatCodec.BOXED) {
- return bind(BoxedFloatArrayCodec.INSTANCE);
+ return bind(
+ componentTypeInfo.rejectsNull()
+ ? BoxedFloatArrayCodec.NON_NULL
+ : BoxedFloatArrayCodec.INSTANCE);
} else if (componentType == Double.class && componentCodec == ScalarCodecs.DoubleCodec.BOXED) {
- return bind(BoxedDoubleArrayCodec.INSTANCE);
+ return bind(
+ componentTypeInfo.rejectsNull()
+ ? BoxedDoubleArrayCodec.NON_NULL
+ : BoxedDoubleArrayCodec.INSTANCE);
} else if (componentType == String.class
&& componentCodec == ScalarCodecs.StringCodec.INSTANCE) {
- return bind(StringArrayCodec.INSTANCE);
+ return bind(
+ componentTypeInfo.rejectsNull() ? StringArrayCodec.NON_NULL : StringArrayCodec.INSTANCE);
}
if (componentType.isPrimitive()) {
return new CustomPrimitiveArrayCodec<>(componentType, componentTypeInfo);
}
- return new ObjectArrayCodec<>(componentType, componentTypeInfo);
+ return componentTypeInfo.rejectsNull()
+ ? new NonNullObjectArrayCodec<>(componentType, componentTypeInfo)
+ : new ObjectArrayCodec<>(componentType, componentTypeInfo);
+ }
+
+ /** Returns the exact unsigned primitive-array specialization for one semantic array id. */
+ @Internal
+ public static <T> ArrayCodec<T> createUnsignedPrimitive(Class<T> arrayType, int typeId) {
+ if (arrayType == byte[].class && typeId == Types.UINT8_ARRAY) {
+ return bind(ByteArrayCodec.UNSIGNED);
+ }
+ if (arrayType == short[].class && typeId == Types.UINT16_ARRAY) {
+ return bind(ShortArrayCodec.UNSIGNED);
+ }
+ if (arrayType == int[].class && typeId == Types.UINT32_ARRAY) {
+ return bind(IntArrayCodec.UNSIGNED);
+ }
+ if (arrayType == long[].class && typeId == Types.UINT64_ARRAY) {
+ return bind(LongArrayCodec.UNSIGNED);
+ }
+ throw new ForyJsonException(
+ "Unsigned JSON array semantic id " + typeId + " does not match " + arrayType.getName());
}
@SuppressWarnings("unchecked")
@@ -149,13 +196,27 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
reader.exitDepth();
}
- public static final class IntArrayCodec extends ArrayCodec<int[]> {
- private static final IntArrayCodec INSTANCE = new IntArrayCodec();
+ // Byte, short, and int signed/unsigned codecs intentionally share these exact element hooks.
+ // Their representation-specific reader call sites remain within the measured performance gate;
+ // duplicate complete loops only when a concrete C2 regression proves that ownership necessary.
+ public abstract static class IntArrayCodec extends ArrayCodec<int[]> {
+ private static final IntArrayCodec INSTANCE = new SignedIntArrayCodec();
+ private static final IntArrayCodec UNSIGNED = new UnsignedIntArrayCodec();
private IntArrayCodec() {
super(int.class);
}
+ abstract void writeElement(StringJsonWriter writer, int value);
+
+ abstract void writeElement(Utf8JsonWriter writer, int value);
+
+ abstract int readElement(Latin1JsonReader reader);
+
+ abstract int readElement(Utf16JsonReader reader);
+
+ abstract int readElement(Utf8JsonReader reader);
+
@Override
public void writeString(StringJsonWriter writer, int[] value) {
if (value == null) {
@@ -166,7 +227,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeArrayStart();
for (int i = 0; i < array.length; i++) {
writer.writeComma(i);
- writer.writeInt(array[i]);
+ writeElement(writer, array[i]);
}
writer.writeArrayEnd();
}
@@ -181,7 +242,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeArrayStart();
for (int i = 0; i < array.length; i++) {
writer.writeComma(i);
- writer.writeInt(array[i]);
+ writeElement(writer, array[i]);
}
writer.writeArrayEnd();
}
@@ -207,7 +268,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = reader.readIntValue();
+ values[size++] = readElement(reader);
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishPrimitiveArray(reader, size, Integer.BYTES);
@@ -235,7 +296,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = reader.readIntValue();
+ values[size++] = readElement(reader);
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishPrimitiveArray(reader, size, Integer.BYTES);
@@ -263,7 +324,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = reader.readIntValue();
+ values[size++] = readElement(reader);
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishPrimitiveArray(reader, size, Integer.BYTES);
@@ -271,8 +332,66 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
}
+ private static final class SignedIntArrayCodec extends IntArrayCodec {
+ @Override
+ void writeElement(StringJsonWriter writer, int value) {
+ writer.writeInt(value);
+ }
+
+ @Override
+ void writeElement(Utf8JsonWriter writer, int value) {
+ writer.writeInt(value);
+ }
+
+ @Override
+ int readElement(Latin1JsonReader reader) {
+ return reader.readIntValue();
+ }
+
+ @Override
+ int readElement(Utf16JsonReader reader) {
+ return reader.readIntValue();
+ }
+
+ @Override
+ int readElement(Utf8JsonReader reader) {
+ return reader.readIntValue();
+ }
+ }
+
+ private static final class UnsignedIntArrayCodec extends IntArrayCodec {
+ @Override
+ void writeElement(StringJsonWriter writer, int value) {
+ writer.writeUnsignedInt(value);
+ }
+
+ @Override
+ void writeElement(Utf8JsonWriter writer, int value) {
+ writer.writeUnsignedInt(value);
+ }
+
+ @Override
+ int readElement(Latin1JsonReader reader) {
+ return reader.readUnsignedInt();
+ }
+
+ @Override
+ int readElement(Utf16JsonReader reader) {
+ return reader.readUnsignedInt();
+ }
+
+ @Override
+ int readElement(Utf8JsonReader reader) {
+ return reader.readUnsignedInt();
+ }
+ }
+
+ // Keep the signed codec as the exact final loop owner. Routing signed long arrays through a
+ // semantic element hook makes C2 inline the root and array loop into one unstable graph; the
+ // unsigned codec below owns separate primitive operations so neither warmed path branches.
public static final class LongArrayCodec extends ArrayCodec<long[]> {
private static final LongArrayCodec INSTANCE = new LongArrayCodec();
+ private static final ArrayCodec<long[]> UNSIGNED = new UnsignedLongArrayCodec();
private LongArrayCodec() {
super(long.class);
@@ -615,6 +734,139 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
}
+ private static final class UnsignedLongArrayCodec extends ArrayCodec<long[]> {
+ private UnsignedLongArrayCodec() {
+ super(long.class);
+ }
+
+ @Override
+ public void writeString(StringJsonWriter writer, long[] value) {
+ if (value == null) {
+ writer.writeNull();
+ return;
+ }
+ long[] array = value;
+ writer.writeArrayStart();
+ for (int i = 0; i < array.length; i++) {
+ writer.writeComma(i);
+ writer.writeUnsignedLong(array[i]);
+ }
+ writer.writeArrayEnd();
+ }
+
+ @Override
+ public void writeUtf8(Utf8JsonWriter writer, long[] value) {
+ if (value == null) {
+ writer.writeNull();
+ return;
+ }
+ long[] array = value;
+ writer.writeArrayStart();
+ for (int i = 0; i < array.length; i++) {
+ writer.writeComma(i);
+ writer.writeUnsignedLong(array[i]);
+ }
+ writer.writeArrayEnd();
+ }
+
+ @Override
+ public long[] readLatin1(Latin1JsonReader reader) {
+ return readUnsignedArray(reader);
+ }
+
+ @Override
+ public long[] readUtf16(Utf16JsonReader reader) {
+ return readUnsignedArray(reader);
+ }
+
+ @Override
+ public long[] readUtf8(Utf8JsonReader reader) {
+ return readUnsignedArray(reader);
+ }
+
+ private long[] readUnsignedArray(JsonReader reader) {
+ if (reader.tryReadNullToken()) {
+ return null;
+ }
+ reader.enterDepth();
+ reader.expectNextToken('[');
+ if (reader.consumeNextToken(']')) {
+ finishPrimitiveArray(reader, 0, Long.BYTES);
+ return new long[0];
+ }
+ rejectNull(reader);
+ long v0 = reader.readUnsignedLong();
+ if (!reader.consumeNextCommaOrEndArray()) {
+ finishPrimitiveArray(reader, 1, Long.BYTES);
+ return new long[] {v0};
+ }
+ rejectNull(reader);
+ long v1 = reader.readUnsignedLong();
+ if (!reader.consumeNextCommaOrEndArray()) {
+ finishPrimitiveArray(reader, 2, Long.BYTES);
+ return new long[] {v0, v1};
+ }
+ rejectNull(reader);
+ long v2 = reader.readUnsignedLong();
+ if (!reader.consumeNextCommaOrEndArray()) {
+ finishPrimitiveArray(reader, 3, Long.BYTES);
+ return new long[] {v0, v1, v2};
+ }
+ rejectNull(reader);
+ long v3 = reader.readUnsignedLong();
+ if (!reader.consumeNextCommaOrEndArray()) {
+ finishPrimitiveArray(reader, 4, Long.BYTES);
+ return new long[] {v0, v1, v2, v3};
+ }
+ rejectNull(reader);
+ long v4 = reader.readUnsignedLong();
+ if (!reader.consumeNextCommaOrEndArray()) {
+ finishPrimitiveArray(reader, 5, Long.BYTES);
+ return new long[] {v0, v1, v2, v3, v4};
+ }
+ rejectNull(reader);
+ long v5 = reader.readUnsignedLong();
+ if (!reader.consumeNextCommaOrEndArray()) {
+ finishPrimitiveArray(reader, 6, Long.BYTES);
+ return new long[] {v0, v1, v2, v3, v4, v5};
+ }
+ rejectNull(reader);
+ long v6 = reader.readUnsignedLong();
+ if (!reader.consumeNextCommaOrEndArray()) {
+ finishPrimitiveArray(reader, 7, Long.BYTES);
+ return new long[] {v0, v1, v2, v3, v4, v5, v6};
+ }
+ rejectNull(reader);
+ long v7 = reader.readUnsignedLong();
+ if (!reader.consumeNextCommaOrEndArray()) {
+ finishPrimitiveArray(reader, 8, Long.BYTES);
+ return new long[] {v0, v1, v2, v3, v4, v5, v6, v7};
+ }
+ long[] values = new long[16];
+ values[0] = v0;
+ values[1] = v1;
+ values[2] = v2;
+ values[3] = v3;
+ values[4] = v4;
+ values[5] = v5;
+ values[6] = v6;
+ values[7] = v7;
+ int size = 8;
+ do {
+ rejectNull(reader);
+ if ((size & ARRAY_BATCH_MASK) == ARRAY_BATCH_MASK) {
+ reader.reserveGraphMemory(ARRAY_BATCH_SIZE * Long.BYTES);
+ }
+ if (size == values.length) {
+ values = Arrays.copyOf(values, values.length << 1);
+ }
+ values[size++] = reader.readUnsignedLong();
+ } while (reader.consumeNextCommaOrEndArray());
+ finishPrimitiveArray(reader, size, Long.BYTES);
+ return Arrays.copyOf(values, size);
+ }
+ }
+
public static final class BooleanArrayCodec extends ArrayCodec<boolean[]> {
private static final BooleanArrayCodec INSTANCE = new BooleanArrayCodec();
private static final int ELEMENT_BYTES = 1;
@@ -738,13 +990,24 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
}
- public static final class ShortArrayCodec extends ArrayCodec<short[]> {
- private static final ShortArrayCodec INSTANCE = new ShortArrayCodec();
+ public abstract static class ShortArrayCodec extends ArrayCodec<short[]> {
+ private static final ShortArrayCodec INSTANCE = new SignedShortArrayCodec();
+ private static final ShortArrayCodec UNSIGNED = new UnsignedShortArrayCodec();
private ShortArrayCodec() {
super(short.class);
}
+ abstract void writeElement(StringJsonWriter writer, short value);
+
+ abstract void writeElement(Utf8JsonWriter writer, short value);
+
+ abstract short readElement(Latin1JsonReader reader);
+
+ abstract short readElement(Utf16JsonReader reader);
+
+ abstract short readElement(Utf8JsonReader reader);
+
@Override
public void writeString(StringJsonWriter writer, short[] value) {
if (value == null) {
@@ -755,7 +1018,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeArrayStart();
for (int i = 0; i < array.length; i++) {
writer.writeComma(i);
- writer.writeInt(array[i]);
+ writeElement(writer, array[i]);
}
writer.writeArrayEnd();
}
@@ -770,7 +1033,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeArrayStart();
for (int i = 0; i < array.length; i++) {
writer.writeComma(i);
- writer.writeInt(array[i]);
+ writeElement(writer, array[i]);
}
writer.writeArrayEnd();
}
@@ -796,7 +1059,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = readShort(reader.readIntValue());
+ values[size++] = readElement(reader);
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishPrimitiveArray(reader, size, Short.BYTES);
@@ -824,7 +1087,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = readShort(reader.readIntValue());
+ values[size++] = readElement(reader);
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishPrimitiveArray(reader, size, Short.BYTES);
@@ -852,7 +1115,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = readShort(reader.readIntValue());
+ values[size++] = readElement(reader);
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishPrimitiveArray(reader, size, Short.BYTES);
@@ -860,13 +1123,78 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
}
- public static final class ByteArrayCodec extends ArrayCodec<byte[]> {
- private static final ByteArrayCodec INSTANCE = new ByteArrayCodec();
+ private static final class SignedShortArrayCodec extends ShortArrayCodec {
+ @Override
+ void writeElement(StringJsonWriter writer, short value) {
+ writer.writeInt(value);
+ }
+
+ @Override
+ void writeElement(Utf8JsonWriter writer, short value) {
+ writer.writeInt(value);
+ }
+
+ @Override
+ short readElement(Latin1JsonReader reader) {
+ return readShort(reader.readIntValue());
+ }
+
+ @Override
+ short readElement(Utf16JsonReader reader) {
+ return readShort(reader.readIntValue());
+ }
+
+ @Override
+ short readElement(Utf8JsonReader reader) {
+ return readShort(reader.readIntValue());
+ }
+ }
+
+ private static final class UnsignedShortArrayCodec extends ShortArrayCodec {
+ @Override
+ void writeElement(StringJsonWriter writer, short value) {
+ writer.writeInt(Short.toUnsignedInt(value));
+ }
+
+ @Override
+ void writeElement(Utf8JsonWriter writer, short value) {
+ writer.writeInt(Short.toUnsignedInt(value));
+ }
+
+ @Override
+ short readElement(Latin1JsonReader reader) {
+ return readUnsignedShort(reader.readUnsignedInt());
+ }
+
+ @Override
+ short readElement(Utf16JsonReader reader) {
+ return readUnsignedShort(reader.readUnsignedInt());
+ }
+
+ @Override
+ short readElement(Utf8JsonReader reader) {
+ return readUnsignedShort(reader.readUnsignedInt());
+ }
+ }
+
+ public abstract static class ByteArrayCodec extends ArrayCodec<byte[]> {
+ private static final ByteArrayCodec INSTANCE = new SignedByteArrayCodec();
+ private static final ByteArrayCodec UNSIGNED = new UnsignedByteArrayCodec();
private ByteArrayCodec() {
super(byte.class);
}
+ abstract void writeElement(StringJsonWriter writer, byte value);
+
+ abstract void writeElement(Utf8JsonWriter writer, byte value);
+
+ abstract byte readElement(Latin1JsonReader reader);
+
+ abstract byte readElement(Utf16JsonReader reader);
+
+ abstract byte readElement(Utf8JsonReader reader);
+
@Override
public void writeString(StringJsonWriter writer, byte[] value) {
if (value == null) {
@@ -877,7 +1205,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeArrayStart();
for (int i = 0; i < array.length; i++) {
writer.writeComma(i);
- writer.writeInt(array[i]);
+ writeElement(writer, array[i]);
}
writer.writeArrayEnd();
}
@@ -892,7 +1220,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeArrayStart();
for (int i = 0; i < array.length; i++) {
writer.writeComma(i);
- writer.writeInt(array[i]);
+ writeElement(writer, array[i]);
}
writer.writeArrayEnd();
}
@@ -918,7 +1246,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = readByte(reader.readIntValue());
+ values[size++] = readElement(reader);
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishPrimitiveArray(reader, size, Byte.BYTES);
@@ -946,7 +1274,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = readByte(reader.readIntValue());
+ values[size++] = readElement(reader);
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishPrimitiveArray(reader, size, Byte.BYTES);
@@ -974,7 +1302,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = readByte(reader.readIntValue());
+ values[size++] = readElement(reader);
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishPrimitiveArray(reader, size, Byte.BYTES);
@@ -982,6 +1310,60 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
}
+ private static final class SignedByteArrayCodec extends ByteArrayCodec {
+ @Override
+ void writeElement(StringJsonWriter writer, byte value) {
+ writer.writeInt(value);
+ }
+
+ @Override
+ void writeElement(Utf8JsonWriter writer, byte value) {
+ writer.writeInt(value);
+ }
+
+ @Override
+ byte readElement(Latin1JsonReader reader) {
+ return readByte(reader.readIntValue());
+ }
+
+ @Override
+ byte readElement(Utf16JsonReader reader) {
+ return readByte(reader.readIntValue());
+ }
+
+ @Override
+ byte readElement(Utf8JsonReader reader) {
+ return readByte(reader.readIntValue());
+ }
+ }
+
+ private static final class UnsignedByteArrayCodec extends ByteArrayCodec {
+ @Override
+ void writeElement(StringJsonWriter writer, byte value) {
+ writer.writeInt(Byte.toUnsignedInt(value));
+ }
+
+ @Override
+ void writeElement(Utf8JsonWriter writer, byte value) {
+ writer.writeInt(Byte.toUnsignedInt(value));
+ }
+
+ @Override
+ byte readElement(Latin1JsonReader reader) {
+ return readUnsignedByte(reader.readUnsignedInt());
+ }
+
+ @Override
+ byte readElement(Utf16JsonReader reader) {
+ return readUnsignedByte(reader.readUnsignedInt());
+ }
+
+ @Override
+ byte readElement(Utf8JsonReader reader) {
+ return readUnsignedByte(reader.readUnsignedInt());
+ }
+ }
+
public static final class CharArrayCodec extends ArrayCodec<char[]> {
private static final CharArrayCodec INSTANCE = new CharArrayCodec();
@@ -1343,10 +1725,13 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
public static final class StringArrayCodec extends ArrayCodec<String[]> {
- private static final StringArrayCodec INSTANCE = new StringArrayCodec();
+ private static final StringArrayCodec INSTANCE = new StringArrayCodec(false);
+ private static final StringArrayCodec NON_NULL = new StringArrayCodec(true);
+ private final boolean rejectsNull;
- private StringArrayCodec() {
+ private StringArrayCodec(boolean rejectsNull) {
super(String.class);
+ this.rejectsNull = rejectsNull;
}
@Override
@@ -1358,7 +1743,11 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
String[] array = value;
writer.writeArrayStart();
for (int i = 0; i < array.length; i++) {
- writer.writeStringElement(i, array[i]);
+ String element = array[i];
+ if (element == null && rejectsNull) {
+ rejectNullElement();
+ }
+ writer.writeStringElement(i, element);
}
writer.writeArrayEnd();
}
@@ -1372,11 +1761,38 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
String[] array = value;
writer.writeArrayStart();
for (int i = 0; i < array.length; i++) {
- writer.writeStringElement(i, array[i]);
+ String element = array[i];
+ if (element == null && rejectsNull) {
+ rejectNullElement();
+ }
+ writer.writeStringElement(i, element);
}
writer.writeArrayEnd();
}
+ private String readElement(Latin1JsonReader reader) {
+ return requireElement(reader.readNextNullableString());
+ }
+
+ private String readElement(Utf16JsonReader reader) {
+ return requireElement(reader.readNextNullableString());
+ }
+
+ private String readElement(Utf8JsonReader reader) {
+ return requireElement(reader.readNextNullableString());
+ }
+
+ private String requireElement(String element) {
+ if (element == null && rejectsNull) {
+ return rejectNullElement();
+ }
+ return element;
+ }
+
+ private static String rejectNullElement() {
+ throw new ForyJsonException("JSON array element is not nullable");
+ }
+
@Override
public String[] readLatin1(Latin1JsonReader reader) {
if (reader.tryReadNullToken()) {
@@ -1388,22 +1804,22 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
finishReferenceArray(reader, 0);
return new String[0];
}
- String v0 = reader.readNextNullableString();
+ String v0 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 1);
return new String[] {v0};
}
- String v1 = reader.readNextNullableString();
+ String v1 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 2);
return new String[] {v0, v1};
}
- String v2 = reader.readNextNullableString();
+ String v2 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 3);
return new String[] {v0, v1, v2};
}
- String v3 = reader.readNextNullableString();
+ String v3 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 4);
return new String[] {v0, v1, v2, v3};
@@ -1413,27 +1829,27 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
private String[] readLatin1Tail(
Latin1JsonReader reader, String v0, String v1, String v2, String v3) {
- String v4 = reader.readNextNullableString();
+ String v4 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 5);
return new String[] {v0, v1, v2, v3, v4};
}
- String v5 = reader.readNextNullableString();
+ String v5 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 6);
return new String[] {v0, v1, v2, v3, v4, v5};
}
- String v6 = reader.readNextNullableString();
+ String v6 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 7);
return new String[] {v0, v1, v2, v3, v4, v5, v6};
}
- String v7 = reader.readNextNullableString();
+ String v7 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 8);
return new String[] {v0, v1, v2, v3, v4, v5, v6, v7};
}
- String v8 = reader.readNextNullableString();
+ String v8 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 9);
return new String[] {v0, v1, v2, v3, v4, v5, v6, v7, v8};
@@ -1468,7 +1884,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = reader.readNextNullableString();
+ values[size++] = readElement(reader);
} while (reader.consumeNextCommaOrEndArray());
finishReferenceArray(reader, size);
return Arrays.copyOf(values, size);
@@ -1485,22 +1901,22 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
finishReferenceArray(reader, 0);
return new String[0];
}
- String v0 = reader.readNextNullableString();
+ String v0 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 1);
return new String[] {v0};
}
- String v1 = reader.readNextNullableString();
+ String v1 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 2);
return new String[] {v0, v1};
}
- String v2 = reader.readNextNullableString();
+ String v2 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 3);
return new String[] {v0, v1, v2};
}
- String v3 = reader.readNextNullableString();
+ String v3 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 4);
return new String[] {v0, v1, v2, v3};
@@ -1510,27 +1926,27 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
private String[] readUtf16Tail(
Utf16JsonReader reader, String v0, String v1, String v2, String v3) {
- String v4 = reader.readNextNullableString();
+ String v4 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 5);
return new String[] {v0, v1, v2, v3, v4};
}
- String v5 = reader.readNextNullableString();
+ String v5 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 6);
return new String[] {v0, v1, v2, v3, v4, v5};
}
- String v6 = reader.readNextNullableString();
+ String v6 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 7);
return new String[] {v0, v1, v2, v3, v4, v5, v6};
}
- String v7 = reader.readNextNullableString();
+ String v7 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 8);
return new String[] {v0, v1, v2, v3, v4, v5, v6, v7};
}
- String v8 = reader.readNextNullableString();
+ String v8 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 9);
return new String[] {v0, v1, v2, v3, v4, v5, v6, v7, v8};
@@ -1565,7 +1981,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = reader.readNextNullableString();
+ values[size++] = readElement(reader);
} while (reader.consumeNextCommaOrEndArray());
finishReferenceArray(reader, size);
return Arrays.copyOf(values, size);
@@ -1582,22 +1998,22 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
finishReferenceArray(reader, 0);
return new String[0];
}
- String v0 = reader.readNextNullableString();
+ String v0 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 1);
return new String[] {v0};
}
- String v1 = reader.readNextNullableString();
+ String v1 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 2);
return new String[] {v0, v1};
}
- String v2 = reader.readNextNullableString();
+ String v2 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 3);
return new String[] {v0, v1, v2};
}
- String v3 = reader.readNextNullableString();
+ String v3 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 4);
return new String[] {v0, v1, v2, v3};
@@ -1607,27 +2023,27 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
private String[] readUtf8Tail(
Utf8JsonReader reader, String v0, String v1, String v2, String v3) {
- String v4 = reader.readNextNullableString();
+ String v4 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 5);
return new String[] {v0, v1, v2, v3, v4};
}
- String v5 = reader.readNextNullableString();
+ String v5 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 6);
return new String[] {v0, v1, v2, v3, v4, v5};
}
- String v6 = reader.readNextNullableString();
+ String v6 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 7);
return new String[] {v0, v1, v2, v3, v4, v5, v6};
}
- String v7 = reader.readNextNullableString();
+ String v7 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 8);
return new String[] {v0, v1, v2, v3, v4, v5, v6, v7};
}
- String v8 = reader.readNextNullableString();
+ String v8 = readElement(reader);
if (!reader.consumeNextCommaOrEndArray()) {
finishReferenceArray(reader, 9);
return new String[] {v0, v1, v2, v3, v4, v5, v6, v7, v8};
@@ -1664,14 +2080,14 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = reader.readNextNullableString();
+ values[size++] = readElement(reader);
} while (reader.consumeNextCommaOrEndArray());
finishReferenceArray(reader, size);
return Arrays.copyOf(values, size);
}
}
- private static final class ObjectArrayCodec<T> extends ArrayCodec<T> {
+ private static class ObjectArrayCodec<T> extends ArrayCodec<T> {
private static final int VALUES_CACHE_DEPTH = 8;
private static final int INITIAL_VALUES_SIZE = 8;
private static final int MAX_CACHED_VALUES_SIZE = 1024;
@@ -1697,7 +2113,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeArrayStart();
for (int i = 0; i < array.length; i++) {
writer.writeComma(i);
- codec.writeString(writer, array[i]);
+ writeElement(writer, codec, array[i]);
}
writer.writeArrayEnd();
}
@@ -1713,7 +2129,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeArrayStart();
for (int i = 0; i < array.length; i++) {
writer.writeComma(i);
- codec.writeUtf8(writer, array[i]);
+ writeElement(writer, codec, array[i]);
}
writer.writeArrayEnd();
}
@@ -1746,7 +2162,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = codec.readLatin1(reader);
+ values[size++] = readElement(reader, codec);
} while (reader.consumeNextCommaOrEndArray());
}
reader.reserveGraphMemory(ARRAY_HEADER_BYTES + (size & ARRAY_BATCH_MASK) * REFERENCE_BYTES);
@@ -1788,7 +2204,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = codec.readUtf16(reader);
+ values[size++] = readElement(reader, codec);
} while (reader.consumeNextCommaOrEndArray());
}
reader.reserveGraphMemory(ARRAY_HEADER_BYTES + (size & ARRAY_BATCH_MASK) * REFERENCE_BYTES);
@@ -1830,7 +2246,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
if (size == values.length) {
values = Arrays.copyOf(values, values.length << 1);
}
- values[size++] = codec.readUtf8(reader);
+ values[size++] = readElement(reader, codec);
} while (reader.consumeNextCommaOrEndArray());
}
reader.reserveGraphMemory(ARRAY_HEADER_BYTES + (size & ARRAY_BATCH_MASK) * REFERENCE_BYTES);
@@ -1860,6 +2276,26 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
valuesDepth = depth;
}
+ void writeElement(StringJsonWriter writer, StringWriterCodec<Object> codec, Object element) {
+ codec.writeString(writer, element);
+ }
+
+ void writeElement(Utf8JsonWriter writer, Utf8WriterCodec<Object> codec, Object element) {
+ codec.writeUtf8(writer, element);
+ }
+
+ Object readElement(Latin1JsonReader reader, Latin1ReaderCodec<Object> codec) {
+ return codec.readLatin1(reader);
+ }
+
+ Object readElement(Utf16JsonReader reader, Utf16ReaderCodec<Object> codec) {
+ return codec.readUtf16(reader);
+ }
+
+ Object readElement(Utf8JsonReader reader, Utf8ReaderCodec<Object> codec) {
+ return codec.readUtf8(reader);
+ }
+
@SuppressWarnings("unchecked")
private T newArray(int size) {
// The factory constructs this codec only for reference-array components.
@@ -1867,6 +2303,56 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
}
+ private static final class NonNullObjectArrayCodec<T> extends ObjectArrayCodec<T> {
+ private NonNullObjectArrayCodec(Class<?> componentType, JsonTypeInfo elementTypeInfo) {
+ super(componentType, elementTypeInfo);
+ }
+
+ @Override
+ void writeElement(StringJsonWriter writer, StringWriterCodec<Object> codec, Object element) {
+ if (element == null) {
+ rejectNullElement();
+ }
+ codec.writeString(writer, element);
+ }
+
+ @Override
+ void writeElement(Utf8JsonWriter writer, Utf8WriterCodec<Object> codec, Object element) {
+ if (element == null) {
+ rejectNullElement();
+ }
+ codec.writeUtf8(writer, element);
+ }
+
+ @Override
+ Object readElement(Latin1JsonReader reader, Latin1ReaderCodec<Object> codec) {
+ if (reader.tryReadNullToken()) {
+ return rejectNullElement();
+ }
+ return codec.readLatin1(reader);
+ }
+
+ @Override
+ Object readElement(Utf16JsonReader reader, Utf16ReaderCodec<Object> codec) {
+ if (reader.tryReadNullToken()) {
+ return rejectNullElement();
+ }
+ return codec.readUtf16(reader);
+ }
+
+ @Override
+ Object readElement(Utf8JsonReader reader, Utf8ReaderCodec<Object> codec) {
+ if (reader.tryReadNullToken()) {
+ return rejectNullElement();
+ }
+ return codec.readUtf8(reader);
+ }
+ }
+
+ private static Object rejectNullElement() {
+ throw new ForyJsonException("JSON array element is not nullable");
+ }
+
private static final class CustomPrimitiveArrayCodec<T> extends ArrayCodec<T> {
private static final int INITIAL_SIZE = 8;
@@ -2042,10 +2528,13 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
public static final class BoxedIntArrayCodec extends ArrayCodec<Integer[]> {
- private static final BoxedIntArrayCodec INSTANCE = new BoxedIntArrayCodec();
+ private static final BoxedIntArrayCodec INSTANCE = new BoxedIntArrayCodec(false);
+ private static final BoxedIntArrayCodec NON_NULL = new BoxedIntArrayCodec(true);
+ private final boolean rejectsNull;
- private BoxedIntArrayCodec() {
+ private BoxedIntArrayCodec(boolean rejectsNull) {
super(Integer.class);
+ this.rejectsNull = rejectsNull;
}
@Override
@@ -2060,7 +2549,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Integer element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeInt(element);
}
@@ -2080,7 +2569,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Integer element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeInt(element);
}
@@ -2107,7 +2596,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Integer.valueOf(reader.readIntValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Integer.valueOf(reader.readIntValue());
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2133,7 +2624,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Integer.valueOf(reader.readIntValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Integer.valueOf(reader.readIntValue());
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2159,7 +2652,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Integer.valueOf(reader.readIntValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Integer.valueOf(reader.readIntValue());
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2168,10 +2663,13 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
public static final class BoxedLongArrayCodec extends ArrayCodec<Long[]> {
- private static final BoxedLongArrayCodec INSTANCE = new BoxedLongArrayCodec();
+ private static final BoxedLongArrayCodec INSTANCE = new BoxedLongArrayCodec(false);
+ private static final BoxedLongArrayCodec NON_NULL = new BoxedLongArrayCodec(true);
+ private final boolean rejectsNull;
- private BoxedLongArrayCodec() {
+ private BoxedLongArrayCodec(boolean rejectsNull) {
super(Long.class);
+ this.rejectsNull = rejectsNull;
}
@Override
@@ -2186,7 +2684,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Long element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeLong(element);
}
@@ -2206,7 +2704,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Long element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeLong(element);
}
@@ -2233,7 +2731,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Long.valueOf(reader.readLongValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Long.valueOf(reader.readLongValue());
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2259,7 +2759,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Long.valueOf(reader.readLongValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Long.valueOf(reader.readLongValue());
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2285,7 +2787,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Long.valueOf(reader.readLongValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Long.valueOf(reader.readLongValue());
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2294,10 +2798,13 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
public static final class BoxedBooleanArrayCodec extends ArrayCodec<Boolean[]> {
- private static final BoxedBooleanArrayCodec INSTANCE = new BoxedBooleanArrayCodec();
+ private static final BoxedBooleanArrayCodec INSTANCE = new BoxedBooleanArrayCodec(false);
+ private static final BoxedBooleanArrayCodec NON_NULL = new BoxedBooleanArrayCodec(true);
+ private final boolean rejectsNull;
- private BoxedBooleanArrayCodec() {
+ private BoxedBooleanArrayCodec(boolean rejectsNull) {
super(Boolean.class);
+ this.rejectsNull = rejectsNull;
}
@Override
@@ -2312,7 +2819,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Boolean element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeBoolean(element);
}
@@ -2332,7 +2839,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Boolean element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeBoolean(element);
}
@@ -2359,7 +2866,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Boolean.valueOf(reader.readBooleanValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Boolean.valueOf(reader.readBooleanValue());
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2385,7 +2894,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Boolean.valueOf(reader.readBooleanValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Boolean.valueOf(reader.readBooleanValue());
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2411,7 +2922,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Boolean.valueOf(reader.readBooleanValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Boolean.valueOf(reader.readBooleanValue());
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2420,10 +2933,13 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
public static final class BoxedShortArrayCodec extends ArrayCodec<Short[]> {
- private static final BoxedShortArrayCodec INSTANCE = new BoxedShortArrayCodec();
+ private static final BoxedShortArrayCodec INSTANCE = new BoxedShortArrayCodec(false);
+ private static final BoxedShortArrayCodec NON_NULL = new BoxedShortArrayCodec(true);
+ private final boolean rejectsNull;
- private BoxedShortArrayCodec() {
+ private BoxedShortArrayCodec(boolean rejectsNull) {
super(Short.class);
+ this.rejectsNull = rejectsNull;
}
@Override
@@ -2438,7 +2954,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Short element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeInt(element);
}
@@ -2458,7 +2974,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Short element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeInt(element);
}
@@ -2485,7 +3001,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Short.valueOf(readShort(reader.readIntValue()));
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Short.valueOf(readShort(reader.readIntValue()));
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2511,7 +3029,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Short.valueOf(readShort(reader.readIntValue()));
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Short.valueOf(readShort(reader.readIntValue()));
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2537,7 +3057,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Short.valueOf(readShort(reader.readIntValue()));
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Short.valueOf(readShort(reader.readIntValue()));
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2546,10 +3068,13 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
public static final class BoxedByteArrayCodec extends ArrayCodec<Byte[]> {
- private static final BoxedByteArrayCodec INSTANCE = new BoxedByteArrayCodec();
+ private static final BoxedByteArrayCodec INSTANCE = new BoxedByteArrayCodec(false);
+ private static final BoxedByteArrayCodec NON_NULL = new BoxedByteArrayCodec(true);
+ private final boolean rejectsNull;
- private BoxedByteArrayCodec() {
+ private BoxedByteArrayCodec(boolean rejectsNull) {
super(Byte.class);
+ this.rejectsNull = rejectsNull;
}
@Override
@@ -2564,7 +3089,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Byte element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeInt(element);
}
@@ -2584,7 +3109,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Byte element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeInt(element);
}
@@ -2611,7 +3136,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Byte.valueOf(readByte(reader.readIntValue()));
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Byte.valueOf(readByte(reader.readIntValue()));
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2637,7 +3164,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Byte.valueOf(readByte(reader.readIntValue()));
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Byte.valueOf(readByte(reader.readIntValue()));
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2663,7 +3192,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Byte.valueOf(readByte(reader.readIntValue()));
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Byte.valueOf(readByte(reader.readIntValue()));
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2672,10 +3203,13 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
public static final class BoxedCharArrayCodec extends ArrayCodec<Character[]> {
- private static final BoxedCharArrayCodec INSTANCE = new BoxedCharArrayCodec();
+ private static final BoxedCharArrayCodec INSTANCE = new BoxedCharArrayCodec(false);
+ private static final BoxedCharArrayCodec NON_NULL = new BoxedCharArrayCodec(true);
+ private final boolean rejectsNull;
- private BoxedCharArrayCodec() {
+ private BoxedCharArrayCodec(boolean rejectsNull) {
super(Character.class);
+ this.rejectsNull = rejectsNull;
}
@Override
@@ -2690,7 +3224,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Character element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeChar(element);
}
@@ -2710,7 +3244,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Character element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeChar(element);
}
@@ -2737,7 +3271,8 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
String element = reader.readNextNullableString();
- values[size++] = element == null ? null : Character.valueOf(readChar(element));
+ values[size++] =
+ element == null ? referenceNull(rejectsNull) : Character.valueOf(readChar(element));
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2763,7 +3298,8 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
String element = reader.readNextNullableString();
- values[size++] = element == null ? null : Character.valueOf(readChar(element));
+ values[size++] =
+ element == null ? referenceNull(rejectsNull) : Character.valueOf(readChar(element));
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2789,7 +3325,8 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
String element = reader.readNextNullableString();
- values[size++] = element == null ? null : Character.valueOf(readChar(element));
+ values[size++] =
+ element == null ? referenceNull(rejectsNull) : Character.valueOf(readChar(element));
} while (reader.consumeNextToken(','));
reader.expectNextToken(']');
finishReferenceArray(reader, size);
@@ -2798,10 +3335,13 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
public static final class BoxedFloatArrayCodec extends ArrayCodec<Float[]> {
- private static final BoxedFloatArrayCodec INSTANCE = new BoxedFloatArrayCodec();
+ private static final BoxedFloatArrayCodec INSTANCE = new BoxedFloatArrayCodec(false);
+ private static final BoxedFloatArrayCodec NON_NULL = new BoxedFloatArrayCodec(true);
+ private final boolean rejectsNull;
- private BoxedFloatArrayCodec() {
+ private BoxedFloatArrayCodec(boolean rejectsNull) {
super(Float.class);
+ this.rejectsNull = rejectsNull;
}
@Override
@@ -2816,7 +3356,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Float element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeFloat(element);
}
@@ -2836,7 +3376,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Float element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeFloat(element);
}
@@ -2863,7 +3403,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Float.valueOf(reader.readNextFloatValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Float.valueOf(reader.readNextFloatValue());
} while (reader.consumeNextCommaOrEndArray());
finishReferenceArray(reader, size);
return Arrays.copyOf(values, size);
@@ -2888,7 +3430,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Float.valueOf(reader.readNextFloatValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Float.valueOf(reader.readNextFloatValue());
} while (reader.consumeNextCommaOrEndArray());
finishReferenceArray(reader, size);
return Arrays.copyOf(values, size);
@@ -2913,7 +3457,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Float.valueOf(reader.readNextFloatValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Float.valueOf(reader.readNextFloatValue());
} while (reader.consumeNextCommaOrEndArray());
finishReferenceArray(reader, size);
return Arrays.copyOf(values, size);
@@ -2921,10 +3467,13 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
}
public static final class BoxedDoubleArrayCodec extends ArrayCodec<Double[]> {
- private static final BoxedDoubleArrayCodec INSTANCE = new BoxedDoubleArrayCodec();
+ private static final BoxedDoubleArrayCodec INSTANCE = new BoxedDoubleArrayCodec(false);
+ private static final BoxedDoubleArrayCodec NON_NULL = new BoxedDoubleArrayCodec(true);
+ private final boolean rejectsNull;
- private BoxedDoubleArrayCodec() {
+ private BoxedDoubleArrayCodec(boolean rejectsNull) {
super(Double.class);
+ this.rejectsNull = rejectsNull;
}
@Override
@@ -2939,7 +3488,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Double element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeDouble(element);
}
@@ -2959,7 +3508,7 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
writer.writeComma(i);
Double element = array[i];
if (element == null) {
- writer.writeNull();
+ writeReferenceNull(writer, rejectsNull);
} else {
writer.writeDouble(element);
}
@@ -2986,7 +3535,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Double.valueOf(reader.readNextDoubleValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Double.valueOf(reader.readNextDoubleValue());
} while (reader.consumeNextCommaOrEndArray());
finishReferenceArray(reader, size);
return Arrays.copyOf(values, size);
@@ -3011,7 +3562,9 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Double.valueOf(reader.readNextDoubleValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Double.valueOf(reader.readNextDoubleValue());
} while (reader.consumeNextCommaOrEndArray());
finishReferenceArray(reader, size);
return Arrays.copyOf(values, size);
@@ -3036,13 +3589,29 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
values = Arrays.copyOf(values, values.length << 1);
}
values[size++] =
- reader.tryReadNextNullToken() ? null : Double.valueOf(reader.readNextDoubleValue());
+ reader.tryReadNextNullToken()
+ ? referenceNull(rejectsNull)
+ : Double.valueOf(reader.readNextDoubleValue());
} while (reader.consumeNextCommaOrEndArray());
finishReferenceArray(reader, size);
return Arrays.copyOf(values, size);
}
}
+ private static <T> T referenceNull(boolean rejectsNull) {
+ if (rejectsNull) {
+ throw new ForyJsonException("JSON array element is not nullable");
+ }
+ return null;
+ }
+
+ private static void writeReferenceNull(JsonWriter writer, boolean rejectsNull) {
+ if (rejectsNull) {
+ throw new ForyJsonException("JSON array element is not nullable");
+ }
+ writer.writeNull();
+ }
+
private static void rejectNull(JsonReader reader) {
if (reader.tryReadNull()) {
throw new ForyJsonException("Cannot read null into primitive array element");
@@ -3081,6 +3650,21 @@ public abstract class ArrayCodec<T> implements JsonValueCodec<T> {
return (byte) value;
}
+ // JsonReader.readUnsignedInt returns raw int bits, so narrowing gates must compare them unsigned.
+ private static short readUnsignedShort(int value) {
+ if (Integer.compareUnsigned(value, 0xffff) > 0) {
+ throw new ForyJsonException("Unsigned short overflow");
+ }
+ return (short) value;
+ }
+
+ private static byte readUnsignedByte(int value) {
+ if (Integer.compareUnsigned(value, 0xff) > 0) {
+ throw new ForyJsonException("Unsigned byte overflow");
+ }
+ return (byte) value;
+ }
+
private static char readChar(JsonReader reader) {
return readChar(reader.readString());
}
diff --git a/java/fory-json/src/main/java/org/apache/fory/json/codec/ClosedSubtypeCodec.java b/java/fory-json/src/main/java/org/apache/fory/json/codec/ClosedSubtypeCodec.java
index 4d16d974d..107a31fc7 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/codec/ClosedSubtypeCodec.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/codec/ClosedSubtypeCodec.java
@@ -21,6 +21,7 @@ package org.apache.fory.json.codec;
import org.apache.fory.annotation.Internal;
import org.apache.fory.json.ForyJsonException;
+import org.apache.fory.json.JsonCodecFactory;
import org.apache.fory.json.annotation.JsonSubTypes.Inclusion;
import org.apache.fory.json.meta.JsonCreatorFieldInfo;
import org.apache.fory.json.meta.JsonCreatorInfo;
@@ -54,18 +55,20 @@ public final class ClosedSubtypeCodec implements CompositeJsonCodec<Object> {
private final Class<?> baseType;
private final JsonSubTypesInfo definition;
private final TypeRef<?> declaredType;
- private final Object[] singletonValues;
+ private final JsonCodecFactory childFactory;
+ private final Object[] fixedInstances;
private final JsonTypeInfo[] children;
private final ObjectCodec<Object>[] objectCodecs;
private JsonFieldTable[] inlineReadTables;
- private volatile Latin1ReaderCodec<Object>[] inlineLatin1Readers;
- private volatile Utf16ReaderCodec<Object>[] inlineUtf16Readers;
- private volatile Utf8ReaderCodec<Object>[] inlineUtf8Readers;
+ private InlineReader[] fixedInlineReaders;
+ private Latin1ReaderCodec<Object>[] inlineLatin1Readers;
+ private Utf16ReaderCodec<Object>[] inlineUtf16Readers;
+ private Utf8ReaderCodec<Object>[] inlineUtf8Readers;
/** Creates an unresolved resolver-local dispatcher shell for a validated subtype definition. */
@Internal
public ClosedSubtypeCodec(Class<?> baseType, JsonSubTypesInfo definition) {
- this(baseType, definition, null);
+ this(baseType, definition, null, null);
}
/** Creates an unresolved dispatcher with an exact factory-owned declared root type. */
@@ -75,25 +78,37 @@ public final class ClosedSubtypeCodec implements CompositeJsonCodec<Object> {
this(baseType, definition, declaredType, null);
}
- /** Creates an unresolved dispatcher with parent-owned singleton subtype values. */
+ /** Creates an unresolved dispatcher with a cold default factory for its children. */
@Internal
public ClosedSubtypeCodec(
Class<?> baseType,
JsonSubTypesInfo definition,
TypeRef<?> declaredType,
- Object[] singletonValues) {
+ JsonCodecFactory childFactory) {
+ this(baseType, definition, declaredType, childFactory, null);
+ }
+
+ /** Creates an unresolved dispatcher with parent-owned fixed branch values. */
+ @Internal
+ public ClosedSubtypeCodec(
+ Class<?> baseType,
+ JsonSubTypesInfo definition,
+ TypeRef<?> declaredType,
+ JsonCodecFactory childFactory,
+ Object[] fixedInstances) {
+ if (fixedInstances != null && fixedInstances.length != definition.classes.length) {
+ throw new IllegalArgumentException("Subtype branch metadata does not match the definition");
+ }
this.baseType = baseType;
this.definition = definition;
this.declaredType = declaredType;
- if (singletonValues != null && singletonValues.length != definition.classes.length) {
- throw new IllegalArgumentException("Singleton values do not match subtype branches");
- }
- this.singletonValues = singletonValues == null ? null : singletonValues.clone();
- if (singletonValues != null) {
- for (int i = 0; i < singletonValues.length; i++) {
- Object value = singletonValues[i];
- if (value != null && value.getClass() != definition.classes[i]) {
- throw new IllegalArgumentException("Singleton value does not match subtype branch " + i);
+ this.childFactory = childFactory;
+ this.fixedInstances = fixedInstances == null ? null : fixedInstances.clone();
+ if (fixedInstances != null) {
+ for (int i = 0; i < fixedInstances.length; i++) {
+ Object fixed = fixedInstances[i];
+ if (fixed != null && fixed.getClass() != definition.classes[i]) {
+ throw new IllegalArgumentException("Fixed value does not match subtype branch " + i);
}
}
}
@@ -124,12 +139,18 @@ public final class ClosedSubtypeCodec implements CompositeJsonCodec<Object> {
for (int i = 0; i < children.length; i++) {
Class<?> subtype = definition.classes[i];
TypeRef<?> childType = rootType.getSubtype(subtype);
- Object singleton = singletonValues == null ? null : singletonValues[i];
- JsonTypeInfo child =
- singleton == null
- ? resolver.getSubtypeTypeInfo(
- baseType, childType.getType(), subtype, declaredType != null)
- : resolver.createLeafTypeInfo(childType, new SingletonSubtypeCodec(singleton));
+ int prior = priorBranch(i, subtype);
+ JsonTypeInfo child;
+ if (prior >= 0 && fixedInstances[i] != null) {
+ ObjectCodec<?> priorCodec = resolver.canonicalObjectCodec(children[prior]);
+ child =
+ priorCodec != null && priorCodec.fixedInstance()
+ ? resolver.createSubtypeLeaf(childType, new FixedBranchCodec(fixedInstances[i]))
+ : children[prior];
+ } else {
+ child =
+ resolver.getSubtypeTypeInfo(baseType, childType, declaredType != null, childFactory);
+ }
if (definition.inclusion == Inclusion.PROPERTY) {
ObjectCodec<?> objectCodec = resolver.canonicalObjectCodec(child);
if (objectCodec == null) {
@@ -139,18 +160,24 @@ public final class ClosedSubtypeCodec implements CompositeJsonCodec<Object> {
rejectDiscriminatorCollision(objectCodec, definition.scanInfo.property());
objectCodecs[i] = (ObjectCodec<Object>) objectCodec;
ObjectCodec.AnyInfo any = objectCodec.anyInfo();
- if (any != null && (any.readField() != null || any.readSetter() != null)) {
+ boolean fixed = objectCodec.fixedInstance();
+ if (fixed || any != null && (any.readField() != null || any.readSetter() != null)) {
if (inlineReadTables == null) {
inlineReadTables = new JsonFieldTable[children.length];
}
JsonFieldTable table =
objectCodec.readTable().withSkippedName(definition.scanInfo.property());
inlineReadTables[i] = table;
+ if (fixed) {
+ if (fixedInlineReaders == null) {
+ fixedInlineReaders = new InlineReader[children.length];
+ }
+ fixedInlineReaders[i] = new FixedInlineReader((ObjectCodec<Object>) objectCodec, table);
+ }
// The subtype scan restores the cursor, so the outer child rereads the discriminator and
// needs this parent-local skip table. The resolver constructs a complete immutable array
- // of generated readers for each representation and publishes that array in one volatile
- // write; never publish its elements independently. Nested child values keep the canonical
- // table and canonical capability.
+ // under its JIT lock and installs the array as one unit; never publish its elements
+ // independently. Nested child values keep the canonical table and capability.
}
}
children[i] = child;
@@ -168,7 +195,7 @@ public final class ClosedSubtypeCodec implements CompositeJsonCodec<Object> {
writer.writeObjectStart();
writer.writeRawValue(
definition.stringSubtypePrefixes[index], definition.stringUtf16SubtypePrefixes[index]);
- objectCodecs[index].writeMembers(writer, value, 1);
+ objectCodecs[index].writeSubtypeMembers(writer, value, 1);
writer.writeObjectEnd();
return;
}
@@ -197,7 +224,7 @@ public final class ClosedSubtypeCodec implements CompositeJsonCodec<Object> {
if (definition.inclusion == Inclusion.PROPERTY) {
writer.writeObjectStart();
writer.writeRawValue(definition.utf8SubtypePrefixes[index]);
- objectCodecs[index].writeMembers(writer, value, 1);
+ objectCodecs[index].writeSubtypeMembers(writer, value, 1);
writer.writeObjectEnd();
return;
}
@@ -324,24 +351,19 @@ public final class ClosedSubtypeCodec implements CompositeJsonCodec<Object> {
private int requireSubtype(Object value) {
Class<?> runtimeType = value.getClass();
- Object[] singletons = singletonValues;
- if (singletons != null) {
- int classMatch = -1;
- for (int i = 0; i < singletons.length; i++) {
- Object singleton = singletons[i];
- if (singleton != null) {
- if (value == singleton) {
- return i;
- }
- } else if (definition.classes[i] == runtimeType) {
- classMatch = i;
- }
+ int index = -1;
+ for (int i = 0; i < definition.classes.length; i++) {
+ if (definition.classes[i] != runtimeType) {
+ continue;
+ }
+ Object fixed = fixedInstances == null ? null : fixedInstances[i];
+ if (fixed == value) {
+ return i;
}
- if (classMatch >= 0) {
- return classMatch;
+ if (fixed == null && index < 0) {
+ index = i;
}
}
- int index = singletons == null ? definition.classIndex(runtimeType) : -1;
if (index < 0) {
throw new ForyJsonException(
"Runtime type " + runtimeType.getName() + " is not a declared subtype of " + baseType);
@@ -349,23 +371,36 @@ public final class ClosedSubtypeCodec implements CompositeJsonCodec<Object> {
return index;
}
- private static final class SingletonSubtypeCodec implements JsonValueCodec<Object> {
- private final Object singleton;
+ private int priorBranch(int index, Class<?> type) {
+ if (fixedInstances == null) {
+ return -1;
+ }
+ for (int i = 0; i < index; i++) {
+ if (definition.classes[i] == type) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /** Parent-local fixed branch used when multiple discriminators share one runtime class. */
+ private static final class FixedBranchCodec implements JsonValueCodec<Object> {
+ private final Object instance;
- private SingletonSubtypeCodec(Object singleton) {
- this.singleton = singleton;
+ private FixedBranchCodec(Object instance) {
+ this.instance = instance;
}
@Override
public void writeString(StringJsonWriter writer, Object value) {
- requireSingleton(value);
+ requireInstance(value);
writer.writeObjectStart();
writer.writeObjectEnd();
}
@Override
public void writeUtf8(Utf8JsonWriter writer, Object value) {
- requireSingleton(value);
+ requireInstance(value);
writer.writeObjectStart();
writer.writeObjectEnd();
}
@@ -373,24 +408,24 @@ public final class ClosedSubtypeCodec implements CompositeJsonCodec<Object> {
@Override
public Object readLatin1(Latin1JsonReader reader) {
readEmptyObject(reader);
- return singleton;
+ return instance;
}
@Override
public Object readUtf16(Utf16JsonReader reader) {
readEmptyObject(reader);
- return singleton;
+ return instance;
}
@Override
public Object readUtf8(Utf8JsonReader reader) {
readEmptyObject(reader);
- return singleton;
+ return instance;
}
- private void requireSingleton(Object value) {
- if (value != singleton) {
- throw new ForyJsonException("Expected closed subtype singleton " + singleton);
+ private void requireInstance(Object value) {
+ if (value != instance) {
+ throw new ForyJsonException("Expected closed subtype fixed instance " + instance);
}
}
@@ -398,7 +433,7 @@ public final class ClosedSubtypeCodec implements CompositeJsonCodec<Object> {
reader.enterDepth();
reader.expectNextToken('{');
if (!reader.consumeNextToken('}')) {
- throw new ForyJsonException("Closed subtype singleton requires an empty JSON object");
+ throw new ForyJsonException("Closed subtype fixed instance requires an empty JSON object");
}
reader.exitDepth();
}
@@ -419,6 +454,17 @@ public final class ClosedSubtypeCodec implements CompositeJsonCodec<Object> {
return inlineReadTables == null ? null : inlineReadTables[index];
}
+ /** Returns the table-bound complete reader for a fixed inline branch, if this branch is fixed. */
+ @Internal
+ public InlineReader fixedInlineReader(int index) {
+ return fixedInlineReaders == null ? null : fixedInlineReaders[index];
+ }
+
+ /** Complete parent-table-bound reader capability for one fixed inline branch. */
+ @Internal
+ public interface InlineReader
+ extends Latin1ReaderCodec<Object>, Utf16ReaderCodec<Object>, Utf8ReaderCodec<Object> {}
+
@Internal
public Latin1ReaderCodec<Object>[] inlineLatin1Readers() {
return inlineLatin1Readers;
@@ -472,6 +518,32 @@ public final class ClosedSubtypeCodec implements CompositeJsonCodec<Object> {
}
}
+ /** One immutable fixed-body capability shared by all three parent-local reader arrays. */
+ private static final class FixedInlineReader implements InlineReader {
+ private final ObjectCodec<Object> codec;
+ private final JsonFieldTable table;
+
+ private FixedInlineReader(ObjectCodec<Object> codec, JsonFieldTable table) {
+ this.codec = codec;
+ this.table = table;
+ }
+
+ @Override
+ public Object readLatin1(Latin1JsonReader reader) {
+ return codec.readLatin1Object(reader, table);
+ }
+
+ @Override
+ public Object readUtf16(Utf16JsonReader reader) {
+ return codec.readUtf16Object(reader, table);
+ }
+
+ @Override
+ public Object readUtf8(Utf8JsonReader reader) {
+ return codec.readUtf8Object(reader, table);
+ }
+ }
+
private static void rejectDiscriminatorCollision(ObjectCodec<?> codec, String property) {
// Only the statically known child schema is validated here. Do not probe Any output: dynamic
// discriminator conflicts are application-owned, and invoking its getter here would duplicate
diff --git a/java/fory-json/src/main/java/org/apache/fory/json/codec/CollectionCodec.java b/java/fory-json/src/main/java/org/apache/fory/json/codec/CollectionCodec.java
index 90b23da64..6c077b28c 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/codec/CollectionCodec.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/codec/CollectionCodec.java
@@ -103,13 +103,26 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
this.createsArrayList = factory.createsArrayList();
}
+ private static Object requireElement(JsonTypeInfo type, Object element) {
+ if (element == null && type.rejectsNull()) {
+ type.rejectNullValue();
+ }
+ return element;
+ }
+
+ private static void requireWriteElement(JsonTypeInfo type, Object element) {
+ if (element == null && type.rejectsNull()) {
+ type.rejectNullValue();
+ }
+ }
+
public static CollectionCodec<?> create(
Class<?> rawType, TypeRef<?> typeRef, JsonTypeResolver resolver) {
TypeRef<?> elementTypeRef = CodecUtils.elementTypeRef(typeRef);
Type elementType = elementTypeRef.getType();
Class<?> elementRawType = CodecUtils.rawType(elementType, Object.class);
CollectionFactory factory = collectionFactory(rawType, elementRawType);
- JsonTypeInfo elementTypeInfo = resolver.getTypeInfo(elementType, elementRawType);
+ JsonTypeInfo elementTypeInfo = resolver.getTypeInfo(elementTypeRef);
return create(factory, elementTypeInfo, resolver.canonicalObjectCodec(elementTypeInfo) != null);
}
@@ -129,34 +142,34 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
CollectionFactory factory, JsonTypeInfo elementTypeInfo, boolean objectElement) {
Object elementCodec = elementTypeInfo.stringWriter();
if (elementCodec == ScalarCodecs.StringCodec.INSTANCE) {
- return new StringCollectionCodec(factory);
+ return new StringCollectionCodec(factory, elementTypeInfo);
}
if (elementCodec == ScalarCodecs.BooleanCodec.BOXED) {
- return new BooleanCollectionCodec(factory);
+ return new BooleanCollectionCodec(factory, elementTypeInfo);
}
if (elementCodec == ScalarCodecs.IntCodec.BOXED) {
- return new IntCollectionCodec(factory);
+ return new IntCollectionCodec(factory, elementTypeInfo);
}
if (elementCodec == ScalarCodecs.LongCodec.BOXED) {
- return new LongCollectionCodec(factory);
+ return new LongCollectionCodec(factory, elementTypeInfo);
}
if (elementCodec == ScalarCodecs.ShortCodec.BOXED) {
- return new ShortCollectionCodec(factory);
+ return new ShortCollectionCodec(factory, elementTypeInfo);
}
if (elementCodec == ScalarCodecs.ByteCodec.BOXED) {
- return new ByteCollectionCodec(factory);
+ return new ByteCollectionCodec(factory, elementTypeInfo);
}
if (elementCodec == ScalarCodecs.FloatCodec.BOXED) {
- return new FloatCollectionCodec(factory);
+ return new FloatCollectionCodec(factory, elementTypeInfo);
}
if (elementCodec == ScalarCodecs.DoubleCodec.BOXED) {
- return new DoubleCollectionCodec(factory);
+ return new DoubleCollectionCodec(factory, elementTypeInfo);
}
if (elementCodec == ScalarCodecs.BigIntegerCodec.INSTANCE) {
- return new BigIntegerCollectionCodec(factory);
+ return new BigIntegerCollectionCodec(factory, elementTypeInfo);
}
if (elementCodec == ScalarCodecs.BigDecimalCodec.INSTANCE) {
- return new BigDecimalCollectionCodec(factory);
+ return new BigDecimalCollectionCodec(factory, elementTypeInfo);
}
if (objectElement) {
return new ObjectCollectionCodec(factory, elementTypeInfo);
@@ -493,8 +506,19 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
}
public abstract static class DirectCollectionCodec extends CollectionCodec<Collection<?>> {
- DirectCollectionCodec(CollectionFactory factory) {
+ private final JsonTypeInfo elementTypeInfo;
+
+ DirectCollectionCodec(CollectionFactory factory, JsonTypeInfo elementTypeInfo) {
super(factory);
+ this.elementTypeInfo = elementTypeInfo;
+ }
+
+ final Object requireElement(Object element) {
+ return CollectionCodec.requireElement(elementTypeInfo, element);
+ }
+
+ final void requireWriteElement(Object element) {
+ CollectionCodec.requireWriteElement(elementTypeInfo, element);
}
@Override
@@ -514,7 +538,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- collection.add(readLatin1Element(reader));
+ collection.add(requireElement(readLatin1Element(reader)));
size++;
} while (reader.consumeNextCommaOrEndArray());
}
@@ -543,7 +567,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- collection.add(readUtf16Element(reader));
+ collection.add(requireElement(readUtf16Element(reader)));
size++;
} while (reader.consumeNextCommaOrEndArray());
}
@@ -572,7 +596,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- collection.add(readUtf8Element(reader));
+ collection.add(requireElement(readUtf8Element(reader)));
size++;
} while (reader.consumeNextCommaOrEndArray());
}
@@ -592,7 +616,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES);
return new ArrayList<>(0);
}
- Object e0 = readLatin1Element(reader);
+ Object e0 = requireElement(readLatin1Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + REFERENCE_BYTES);
@@ -600,7 +624,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e0);
return list;
}
- Object e1 = readLatin1Element(reader);
+ Object e1 = requireElement(readLatin1Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 2 * REFERENCE_BYTES);
@@ -609,7 +633,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e1);
return list;
}
- Object e2 = readLatin1Element(reader);
+ Object e2 = requireElement(readLatin1Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 3 * REFERENCE_BYTES);
@@ -619,7 +643,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e2);
return list;
}
- Object e3 = readLatin1Element(reader);
+ Object e3 = requireElement(readLatin1Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 4 * REFERENCE_BYTES);
@@ -634,7 +658,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
// method smaller than C2's hot-inline limit, so a generated caller can absorb the collection
// and element closure solely according to compilation order. The uncommon longer tail stays
// separate below.
- Object e4 = readLatin1Element(reader);
+ Object e4 = requireElement(readLatin1Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 5 * REFERENCE_BYTES);
@@ -646,7 +670,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e4);
return list;
}
- Object e5 = readLatin1Element(reader);
+ Object e5 = requireElement(readLatin1Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 6 * REFERENCE_BYTES);
@@ -664,7 +688,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
private ArrayList<Object> readLatin1ArrayListLongTail(
Latin1JsonReader reader, Object e0, Object e1, Object e2, Object e3, Object e4, Object e5) {
- Object e6 = readLatin1Element(reader);
+ Object e6 = requireElement(readLatin1Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 7 * REFERENCE_BYTES);
@@ -678,7 +702,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e6);
return list;
}
- Object e7 = readLatin1Element(reader);
+ Object e7 = requireElement(readLatin1Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 8 * REFERENCE_BYTES);
@@ -703,13 +727,13 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e5);
list.add(e6);
list.add(e7);
- list.add(readLatin1Element(reader));
+ list.add(requireElement(readLatin1Element(reader)));
int pendingSize = 0;
while (reader.consumeNextCommaOrEndArray()) {
if ((pendingSize & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- list.add(readLatin1Element(reader));
+ list.add(requireElement(readLatin1Element(reader)));
pendingSize++;
}
int tailSize = pendingSize & REFERENCE_BATCH_MASK;
@@ -728,7 +752,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES);
return new ArrayList<>(0);
}
- Object e0 = readUtf16Element(reader);
+ Object e0 = requireElement(readUtf16Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + REFERENCE_BYTES);
@@ -736,7 +760,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e0);
return list;
}
- Object e1 = readUtf16Element(reader);
+ Object e1 = requireElement(readUtf16Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 2 * REFERENCE_BYTES);
@@ -745,7 +769,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e1);
return list;
}
- Object e2 = readUtf16Element(reader);
+ Object e2 = requireElement(readUtf16Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 3 * REFERENCE_BYTES);
@@ -755,7 +779,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e2);
return list;
}
- Object e3 = readUtf16Element(reader);
+ Object e3 = requireElement(readUtf16Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 4 * REFERENCE_BYTES);
@@ -771,7 +795,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
private ArrayList<Object> readUtf16ArrayListTail(
Utf16JsonReader reader, Object e0, Object e1, Object e2, Object e3) {
- Object e4 = readUtf16Element(reader);
+ Object e4 = requireElement(readUtf16Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 5 * REFERENCE_BYTES);
@@ -783,7 +807,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e4);
return list;
}
- Object e5 = readUtf16Element(reader);
+ Object e5 = requireElement(readUtf16Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 6 * REFERENCE_BYTES);
@@ -801,7 +825,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
private ArrayList<Object> readUtf16ArrayListLongTail(
Utf16JsonReader reader, Object e0, Object e1, Object e2, Object e3, Object e4, Object e5) {
- Object e6 = readUtf16Element(reader);
+ Object e6 = requireElement(readUtf16Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 7 * REFERENCE_BYTES);
@@ -815,7 +839,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e6);
return list;
}
- Object e7 = readUtf16Element(reader);
+ Object e7 = requireElement(readUtf16Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 8 * REFERENCE_BYTES);
@@ -840,13 +864,13 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e5);
list.add(e6);
list.add(e7);
- list.add(readUtf16Element(reader));
+ list.add(requireElement(readUtf16Element(reader)));
int pendingSize = 0;
while (reader.consumeNextCommaOrEndArray()) {
if ((pendingSize & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- list.add(readUtf16Element(reader));
+ list.add(requireElement(readUtf16Element(reader)));
pendingSize++;
}
int tailSize = pendingSize & REFERENCE_BATCH_MASK;
@@ -865,7 +889,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES);
return new ArrayList<>(0);
}
- Object e0 = readUtf8Element(reader);
+ Object e0 = requireElement(readUtf8Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + REFERENCE_BYTES);
@@ -873,7 +897,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e0);
return list;
}
- Object e1 = readUtf8Element(reader);
+ Object e1 = requireElement(readUtf8Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 2 * REFERENCE_BYTES);
@@ -882,7 +906,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e1);
return list;
}
- Object e2 = readUtf8Element(reader);
+ Object e2 = requireElement(readUtf8Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 3 * REFERENCE_BYTES);
@@ -892,7 +916,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e2);
return list;
}
- Object e3 = readUtf8Element(reader);
+ Object e3 = requireElement(readUtf8Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 4 * REFERENCE_BYTES);
@@ -908,7 +932,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
private ArrayList<Object> readUtf8ArrayListTail(
Utf8JsonReader reader, Object e0, Object e1, Object e2, Object e3) {
- Object e4 = readUtf8Element(reader);
+ Object e4 = requireElement(readUtf8Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 5 * REFERENCE_BYTES);
@@ -920,7 +944,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e4);
return list;
}
- Object e5 = readUtf8Element(reader);
+ Object e5 = requireElement(readUtf8Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 6 * REFERENCE_BYTES);
@@ -938,7 +962,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
private ArrayList<Object> readUtf8ArrayListLongTail(
Utf8JsonReader reader, Object e0, Object e1, Object e2, Object e3, Object e4, Object e5) {
- Object e6 = readUtf8Element(reader);
+ Object e6 = requireElement(readUtf8Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 7 * REFERENCE_BYTES);
@@ -952,7 +976,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e6);
return list;
}
- Object e7 = readUtf8Element(reader);
+ Object e7 = requireElement(readUtf8Element(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 8 * REFERENCE_BYTES);
@@ -977,13 +1001,13 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e5);
list.add(e6);
list.add(e7);
- list.add(readUtf8Element(reader));
+ list.add(requireElement(readUtf8Element(reader)));
int pendingSize = 0;
while (reader.consumeNextCommaOrEndArray()) {
if ((pendingSize & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- list.add(readUtf8Element(reader));
+ list.add(requireElement(readUtf8Element(reader)));
pendingSize++;
}
int tailSize = pendingSize & REFERENCE_BATCH_MASK;
@@ -1020,6 +1044,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
int index = 0;
for (Object element : value) {
writer.writeComma(index++);
+ requireWriteElement(elementTypeInfo, element);
codec.writeString(writer, element);
}
writer.writeArrayEnd();
@@ -1036,6 +1061,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
int index = 0;
for (Object element : value) {
writer.writeComma(index++);
+ requireWriteElement(elementTypeInfo, element);
codec.writeUtf8(writer, element);
}
writer.writeArrayEnd();
@@ -1056,7 +1082,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- collection.add(codec.readLatin1(reader));
+ collection.add(requireElement(elementTypeInfo, codec.readLatin1(reader)));
size++;
} while (reader.consumeNextCommaOrEndArray());
}
@@ -1083,7 +1109,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- collection.add(codec.readUtf16(reader));
+ collection.add(requireElement(elementTypeInfo, codec.readUtf16(reader)));
size++;
} while (reader.consumeNextCommaOrEndArray());
}
@@ -1110,7 +1136,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- collection.add(codec.readUtf8(reader));
+ collection.add(requireElement(elementTypeInfo, codec.readUtf8(reader)));
size++;
} while (reader.consumeNextCommaOrEndArray());
}
@@ -1144,12 +1170,14 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
for (int index = 0, size = list.size(); index < size; index++) {
Object element = list.get(index);
writer.writeComma(index);
+ requireWriteElement(elementTypeInfo, element);
codec.writeString(writer, element);
}
} else {
int index = 0;
for (Object element : value) {
writer.writeComma(index++);
+ requireWriteElement(elementTypeInfo, element);
codec.writeString(writer, element);
}
}
@@ -1169,12 +1197,14 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
for (int index = 0, size = list.size(); index < size; index++) {
Object element = list.get(index);
writer.writeComma(index);
+ requireWriteElement(elementTypeInfo, element);
codec.writeUtf8(writer, element);
}
} else {
int index = 0;
for (Object element : value) {
writer.writeComma(index++);
+ requireWriteElement(elementTypeInfo, element);
codec.writeUtf8(writer, element);
}
}
@@ -1199,7 +1229,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- collection.add(codec.readLatin1(reader));
+ collection.add(requireElement(elementTypeInfo, codec.readLatin1(reader)));
size++;
} while (reader.consumeNextCommaOrEndArray());
}
@@ -1229,7 +1259,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- collection.add(codec.readUtf16(reader));
+ collection.add(requireElement(elementTypeInfo, codec.readUtf16(reader)));
size++;
} while (reader.consumeNextCommaOrEndArray());
}
@@ -1259,7 +1289,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- collection.add(codec.readUtf8(reader));
+ collection.add(requireElement(elementTypeInfo, codec.readUtf8(reader)));
size++;
} while (reader.consumeNextCommaOrEndArray());
}
@@ -1280,7 +1310,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES);
return new ArrayList<>(0);
}
- Object e0 = codec.readLatin1(reader);
+ Object e0 = requireElement(elementTypeInfo, codec.readLatin1(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + REFERENCE_BYTES);
@@ -1288,7 +1318,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e0);
return list;
}
- Object e1 = codec.readLatin1(reader);
+ Object e1 = requireElement(elementTypeInfo, codec.readLatin1(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 2 * REFERENCE_BYTES);
@@ -1297,7 +1327,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e1);
return list;
}
- Object e2 = codec.readLatin1(reader);
+ Object e2 = requireElement(elementTypeInfo, codec.readLatin1(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 3 * REFERENCE_BYTES);
@@ -1307,7 +1337,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e2);
return list;
}
- Object e3 = codec.readLatin1(reader);
+ Object e3 = requireElement(elementTypeInfo, codec.readLatin1(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 4 * REFERENCE_BYTES);
@@ -1322,7 +1352,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
// method smaller than C2's hot-inline limit, so a generated caller can absorb the collection
// and element closure solely according to compilation order. The uncommon longer tail stays
// separate below.
- Object e4 = codec.readLatin1(reader);
+ Object e4 = requireElement(elementTypeInfo, codec.readLatin1(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 5 * REFERENCE_BYTES);
@@ -1334,7 +1364,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e4);
return list;
}
- Object e5 = codec.readLatin1(reader);
+ Object e5 = requireElement(elementTypeInfo, codec.readLatin1(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 6 * REFERENCE_BYTES);
@@ -1359,7 +1389,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
Object e3,
Object e4,
Object e5) {
- Object e6 = codec.readLatin1(reader);
+ Object e6 = requireElement(elementTypeInfo, codec.readLatin1(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 7 * REFERENCE_BYTES);
@@ -1373,7 +1403,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e6);
return list;
}
- Object e7 = codec.readLatin1(reader);
+ Object e7 = requireElement(elementTypeInfo, codec.readLatin1(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 8 * REFERENCE_BYTES);
@@ -1400,13 +1430,13 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e5);
list.add(e6);
list.add(e7);
- list.add(codec.readLatin1(reader));
+ list.add(requireElement(elementTypeInfo, codec.readLatin1(reader)));
int pendingSize = 0;
while (reader.consumeNextCommaOrEndArray()) {
if ((pendingSize & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- list.add(codec.readLatin1(reader));
+ list.add(requireElement(elementTypeInfo, codec.readLatin1(reader)));
pendingSize++;
}
int tailSize = pendingSize & REFERENCE_BATCH_MASK;
@@ -1426,7 +1456,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES);
return new ArrayList<>(0);
}
- Object e0 = codec.readUtf16(reader);
+ Object e0 = requireElement(elementTypeInfo, codec.readUtf16(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + REFERENCE_BYTES);
@@ -1434,7 +1464,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e0);
return list;
}
- Object e1 = codec.readUtf16(reader);
+ Object e1 = requireElement(elementTypeInfo, codec.readUtf16(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 2 * REFERENCE_BYTES);
@@ -1443,7 +1473,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e1);
return list;
}
- Object e2 = codec.readUtf16(reader);
+ Object e2 = requireElement(elementTypeInfo, codec.readUtf16(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 3 * REFERENCE_BYTES);
@@ -1453,7 +1483,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e2);
return list;
}
- Object e3 = codec.readUtf16(reader);
+ Object e3 = requireElement(elementTypeInfo, codec.readUtf16(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 4 * REFERENCE_BYTES);
@@ -1474,7 +1504,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
Object e1,
Object e2,
Object e3) {
- Object e4 = codec.readUtf16(reader);
+ Object e4 = requireElement(elementTypeInfo, codec.readUtf16(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 5 * REFERENCE_BYTES);
@@ -1486,7 +1516,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e4);
return list;
}
- Object e5 = codec.readUtf16(reader);
+ Object e5 = requireElement(elementTypeInfo, codec.readUtf16(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 6 * REFERENCE_BYTES);
@@ -1511,7 +1541,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
Object e3,
Object e4,
Object e5) {
- Object e6 = codec.readUtf16(reader);
+ Object e6 = requireElement(elementTypeInfo, codec.readUtf16(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 7 * REFERENCE_BYTES);
@@ -1525,7 +1555,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e6);
return list;
}
- Object e7 = codec.readUtf16(reader);
+ Object e7 = requireElement(elementTypeInfo, codec.readUtf16(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 8 * REFERENCE_BYTES);
@@ -1552,13 +1582,13 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e5);
list.add(e6);
list.add(e7);
- list.add(codec.readUtf16(reader));
+ list.add(requireElement(elementTypeInfo, codec.readUtf16(reader)));
int pendingSize = 0;
while (reader.consumeNextCommaOrEndArray()) {
if ((pendingSize & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- list.add(codec.readUtf16(reader));
+ list.add(requireElement(elementTypeInfo, codec.readUtf16(reader)));
pendingSize++;
}
int tailSize = pendingSize & REFERENCE_BATCH_MASK;
@@ -1578,7 +1608,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES);
return new ArrayList<>(0);
}
- Object e0 = codec.readUtf8(reader);
+ Object e0 = requireElement(elementTypeInfo, codec.readUtf8(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + REFERENCE_BYTES);
@@ -1586,7 +1616,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e0);
return list;
}
- Object e1 = codec.readUtf8(reader);
+ Object e1 = requireElement(elementTypeInfo, codec.readUtf8(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 2 * REFERENCE_BYTES);
@@ -1595,7 +1625,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e1);
return list;
}
- Object e2 = codec.readUtf8(reader);
+ Object e2 = requireElement(elementTypeInfo, codec.readUtf8(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 3 * REFERENCE_BYTES);
@@ -1605,7 +1635,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e2);
return list;
}
- Object e3 = codec.readUtf8(reader);
+ Object e3 = requireElement(elementTypeInfo, codec.readUtf8(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 4 * REFERENCE_BYTES);
@@ -1619,7 +1649,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
// Keep the fifth exact-allocation lane in the collection owner. If this lane is split after
// four elements, both resulting methods fall below C2's hot-inline limit and let an outer
// fallback caller absorb the object-element closure according to compilation order.
- Object e4 = codec.readUtf8(reader);
+ Object e4 = requireElement(elementTypeInfo, codec.readUtf8(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 5 * REFERENCE_BYTES);
@@ -1642,7 +1672,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
Object e2,
Object e3,
Object e4) {
- Object e5 = codec.readUtf8(reader);
+ Object e5 = requireElement(elementTypeInfo, codec.readUtf8(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 6 * REFERENCE_BYTES);
@@ -1667,7 +1697,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
Object e3,
Object e4,
Object e5) {
- Object e6 = codec.readUtf8(reader);
+ Object e6 = requireElement(elementTypeInfo, codec.readUtf8(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 7 * REFERENCE_BYTES);
@@ -1681,7 +1711,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e6);
return list;
}
- Object e7 = codec.readUtf8(reader);
+ Object e7 = requireElement(elementTypeInfo, codec.readUtf8(reader));
if (!reader.consumeNextCommaOrEndArray()) {
reader.exitDepth();
reader.reserveGraphMemory(ARRAY_LIST_OWNER_BYTES + 8 * REFERENCE_BYTES);
@@ -1708,13 +1738,13 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
list.add(e5);
list.add(e6);
list.add(e7);
- list.add(codec.readUtf8(reader));
+ list.add(requireElement(elementTypeInfo, codec.readUtf8(reader)));
int pendingSize = 0;
while (reader.consumeNextCommaOrEndArray()) {
if ((pendingSize & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
- list.add(codec.readUtf8(reader));
+ list.add(requireElement(elementTypeInfo, codec.readUtf8(reader)));
pendingSize++;
}
int tailSize = pendingSize & REFERENCE_BATCH_MASK;
@@ -1727,8 +1757,8 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
}
public static final class StringCollectionCodec extends DirectCollectionCodec {
- private StringCollectionCodec(CollectionFactory factory) {
- super(factory);
+ private StringCollectionCodec(CollectionFactory factory, JsonTypeInfo elementTypeInfo) {
+ super(factory, elementTypeInfo);
}
@Override
@@ -1740,6 +1770,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
writer.writeArrayStart();
int index = 0;
for (Object element : value) {
+ requireWriteElement(element);
writer.writeStringElement(index++, (String) element);
}
writer.writeArrayEnd();
@@ -1754,6 +1785,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
writer.writeArrayStart();
int index = 0;
for (Object element : value) {
+ requireWriteElement(element);
writer.writeStringElement(index++, (String) element);
}
writer.writeArrayEnd();
@@ -1776,8 +1808,8 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
}
public static final class BooleanCollectionCodec extends DirectCollectionCodec {
- private BooleanCollectionCodec(CollectionFactory factory) {
- super(factory);
+ private BooleanCollectionCodec(CollectionFactory factory, JsonTypeInfo elementTypeInfo) {
+ super(factory, elementTypeInfo);
}
@Override
@@ -1791,6 +1823,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
for (Object element : value) {
writer.writeComma(index++);
if (element == null) {
+ requireWriteElement(null);
writer.writeNull();
} else {
writer.writeBoolean((boolean) element);
@@ -1810,6 +1843,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
for (Object element : value) {
writer.writeComma(index++);
if (element == null) {
+ requireWriteElement(null);
writer.writeNull();
} else {
writer.writeBoolean((boolean) element);
@@ -1835,8 +1869,8 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
}
public abstract static class NumberCollectionCodec extends DirectCollectionCodec {
- NumberCollectionCodec(CollectionFactory factory) {
- super(factory);
+ NumberCollectionCodec(CollectionFactory factory, JsonTypeInfo elementTypeInfo) {
+ super(factory, elementTypeInfo);
}
@Override
@@ -1850,6 +1884,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
for (Object element : value) {
writer.writeComma(index++);
if (element == null) {
+ requireWriteElement(null);
writer.writeNull();
} else {
writeNumber(writer, element);
@@ -1869,6 +1904,7 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
for (Object element : value) {
writer.writeComma(index++);
if (element == null) {
+ requireWriteElement(null);
writer.writeNull();
} else {
writeNumber(writer, element);
@@ -1881,8 +1917,8 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
}
public static final class IntCollectionCodec extends NumberCollectionCodec {
- private IntCollectionCodec(CollectionFactory factory) {
- super(factory);
+ private IntCollectionCodec(CollectionFactory factory, JsonTypeInfo elementTypeInfo) {
+ super(factory, elementTypeInfo);
}
@Override
@@ -1907,8 +1943,8 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
}
public static final class LongCollectionCodec extends NumberCollectionCodec {
- private LongCollectionCodec(CollectionFactory factory) {
- super(factory);
+ private LongCollectionCodec(CollectionFactory factory, JsonTypeInfo elementTypeInfo) {
+ super(factory, elementTypeInfo);
}
@Override
@@ -1933,8 +1969,8 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
}
public static final class ShortCollectionCodec extends NumberCollectionCodec {
- private ShortCollectionCodec(CollectionFactory factory) {
- super(factory);
+ private ShortCollectionCodec(CollectionFactory factory, JsonTypeInfo elementTypeInfo) {
+ super(factory, elementTypeInfo);
}
@Override
@@ -1959,8 +1995,8 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
}
public static final class ByteCollectionCodec extends NumberCollectionCodec {
- private ByteCollectionCodec(CollectionFactory factory) {
- super(factory);
+ private ByteCollectionCodec(CollectionFactory factory, JsonTypeInfo elementTypeInfo) {
+ super(factory, elementTypeInfo);
}
@Override
@@ -1985,8 +2021,8 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
}
public static final class FloatCollectionCodec extends NumberCollectionCodec {
- private FloatCollectionCodec(CollectionFactory factory) {
- super(factory);
+ private FloatCollectionCodec(CollectionFactory factory, JsonTypeInfo elementTypeInfo) {
+ super(factory, elementTypeInfo);
}
@Override
@@ -2011,8 +2047,8 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
}
public static final class DoubleCollectionCodec extends NumberCollectionCodec {
- private DoubleCollectionCodec(CollectionFactory factory) {
- super(factory);
+ private DoubleCollectionCodec(CollectionFactory factory, JsonTypeInfo elementTypeInfo) {
+ super(factory, elementTypeInfo);
}
@Override
@@ -2037,8 +2073,8 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
}
public static final class BigIntegerCollectionCodec extends NumberCollectionCodec {
- private BigIntegerCollectionCodec(CollectionFactory factory) {
- super(factory);
+ private BigIntegerCollectionCodec(CollectionFactory factory, JsonTypeInfo elementTypeInfo) {
+ super(factory, elementTypeInfo);
}
@Override
@@ -2063,8 +2099,8 @@ public abstract class CollectionCodec<T extends Collection<?>> implements JsonVa
}
public static final class BigDecimalCollectionCodec extends NumberCollectionCodec {
- private BigDecimalCollectionCodec(CollectionFactory factory) {
- super(factory);
+ private BigDecimalCollectionCodec(CollectionFactory factory, JsonTypeInfo elementTypeInfo) {
+ super(factory, elementTypeInfo);
}
@Override
diff --git a/java/fory-json/src/main/java/org/apache/fory/json/codec/DateTimeFormatCodec.java b/java/fory-json/src/main/java/org/apache/fory/json/codec/DateTimeFormatCodec.java
index 0e118451b..7c41d1cde 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/codec/DateTimeFormatCodec.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/codec/DateTimeFormatCodec.java
@@ -140,19 +140,19 @@ final class DateTimeFormatCodec implements JsonValueCodec<Object> {
@Override
public Object readLatin1(Latin1JsonReader reader) {
- CharSequence value = reader.readDateTimeText();
+ CharSequence value = reader.readQuotedText();
return value == null ? null : parse(value);
}
@Override
public Object readUtf16(Utf16JsonReader reader) {
- CharSequence value = reader.readDateTimeText();
+ CharSequence value = reader.readQuotedText();
return value == null ? null : parse(value);
}
@Override
public Object readUtf8(Utf8JsonReader reader) {
- CharSequence value = reader.readDateTimeText();
+ CharSequence value = reader.readQuotedText();
return value == null ? null : parse(value);
}
diff --git a/integration_tests/jpms_tests/src/main/java/module-info.java b/java/fory-json/src/main/java/org/apache/fory/json/codec/DirectUnboxedValueCodec.java
similarity index 60%
copy from integration_tests/jpms_tests/src/main/java/module-info.java
copy to java/fory-json/src/main/java/org/apache/fory/json/codec/DirectUnboxedValueCodec.java
index be2ec6f58..a2a4056b7 100644
--- a/integration_tests/jpms_tests/src/main/java/module-info.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/codec/DirectUnboxedValueCodec.java
@@ -17,14 +17,17 @@
* under the License.
*/
-module org.apache.fory.integration_tests {
- requires org.apache.fory.core;
- requires org.apache.fory.format;
- requires org.apache.fory.test.core;
+package org.apache.fory.json.codec;
- // we can't really test any classes from this module because it only contains test-classes
- requires org.apache.fory.test.suite;
+import java.lang.reflect.Method;
+import org.apache.fory.annotation.Internal;
- exports org.apache.fory.integration_tests.model;
- exports org.apache.fory.integration_tests.publicserializer;
+/** Exact parent-carrier operations for a semantic leaf which is not transparent to its carrier. */
+@Internal
+public interface DirectUnboxedValueCodec extends UnboxedValueCodec {
+ /** Returns the exact static {@code (JsonReader) -> carrier} generated invocation. */
+ Method readCarrierMethod();
+
+ /** Returns the exact static {@code (JsonWriter, carrier) -> void} generated invocation. */
+ Method writeCarrierMethod();
}
diff --git a/java/fory-json/src/main/java/org/apache/fory/json/codec/JsonObjectModel.java b/java/fory-json/src/main/java/org/apache/fory/json/codec/JsonObjectModel.java
index 0f7fc9a50..64c2422b8 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/codec/JsonObjectModel.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/codec/JsonObjectModel.java
@@ -20,109 +20,362 @@
package org.apache.fory.json.codec;
import java.lang.reflect.Constructor;
+import java.lang.reflect.Executable;
+import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
+import java.lang.reflect.WildcardType;
import java.util.HashSet;
+import java.util.List;
import java.util.Objects;
import org.apache.fory.annotation.Internal;
+import org.apache.fory.reflect.TypeRef;
-/** Immutable constructor and accessor metadata supplied by a language JSON module. */
+/** Immutable construction and accessor metadata supplied by a language JSON module. */
@Internal
public final class JsonObjectModel {
- private final Constructor<?> constructor;
+ private final Executable creator;
+ private final Executable invocationCreator;
+ private final Constructor<?> defaultConstructor;
private final String[] parameterNames;
private final Method[] accessors;
private final Method[] defaultMethods;
+ private final int[] defaultMaskBits;
+ private final boolean[] parameterNullable;
+ private final TypeRef<?>[] parameterTypes;
private final String[] propertyNames;
private final Method[] propertyGetters;
private final Method[] propertySetters;
- private final Type[] propertyTypes;
+ private final TypeRef<?>[] propertyTypes;
+ private final boolean[] propertyReconstructible;
+ private final boolean[] propertyRequired;
+ private final Object fixedInstance;
+ private final Field[] nonPropertyFields;
+ /** Creates one ordinary language object model. */
public JsonObjectModel(
Constructor<?> constructor,
+ Constructor<?> defaultConstructor,
String[] parameterNames,
Method[] accessors,
- Method[] defaultMethods) {
+ Method[] defaultMethods,
+ int[] defaultMaskBits,
+ boolean[] parameterNullable,
+ TypeRef<?>[] parameterTypes,
+ String[] propertyNames,
+ Method[] propertyGetters,
+ Method[] propertySetters,
+ TypeRef<?>[] propertyTypes) {
this(
+ (Executable) constructor,
constructor,
+ defaultConstructor,
parameterNames,
accessors,
defaultMethods,
- parameterNames,
- accessors,
- null,
- constructor.getGenericParameterTypes());
+ defaultMaskBits,
+ parameterNullable,
+ parameterTypes,
+ propertyNames,
+ propertyGetters,
+ propertySetters,
+ propertyTypes,
+ allProperties(propertyNames.length),
+ new boolean[propertyNames.length]);
}
+ /** Creates a model for an explicitly selected constructor or static factory. */
public JsonObjectModel(
- Constructor<?> constructor,
+ Executable creator,
+ Executable invocationCreator,
+ Constructor<?> defaultConstructor,
String[] parameterNames,
Method[] accessors,
Method[] defaultMethods,
+ int[] defaultMaskBits,
+ boolean[] parameterNullable,
+ TypeRef<?>[] parameterTypes,
String[] propertyNames,
Method[] propertyGetters,
- Method[] propertySetters) {
+ Method[] propertySetters,
+ TypeRef<?>[] propertyTypes) {
this(
- constructor,
+ creator,
+ invocationCreator,
+ defaultConstructor,
parameterNames,
accessors,
defaultMethods,
+ defaultMaskBits,
+ parameterNullable,
+ parameterTypes,
propertyNames,
propertyGetters,
propertySetters,
- propertyTypes(
- constructor, parameterNames, propertyNames, propertyGetters, propertySetters));
+ propertyTypes,
+ allProperties(propertyNames.length),
+ new boolean[propertyNames.length]);
}
+ /** Creates a model with exact reconstructibility and deferred-required facts. */
public JsonObjectModel(
- Constructor<?> constructor,
+ Executable creator,
+ Executable invocationCreator,
+ Constructor<?> defaultConstructor,
String[] parameterNames,
Method[] accessors,
Method[] defaultMethods,
+ int[] defaultMaskBits,
+ boolean[] parameterNullable,
+ TypeRef<?>[] parameterTypes,
String[] propertyNames,
Method[] propertyGetters,
Method[] propertySetters,
- Type[] propertyTypes) {
- this.constructor = Objects.requireNonNull(constructor, "constructor");
+ TypeRef<?>[] propertyTypes,
+ boolean[] propertyReconstructible,
+ boolean[] propertyRequired) {
+ this.creator = Objects.requireNonNull(creator, "creator");
+ this.invocationCreator = Objects.requireNonNull(invocationCreator, "invocationCreator");
+ this.defaultConstructor = defaultConstructor;
this.parameterNames = parameterNames.clone();
this.accessors = accessors.clone();
this.defaultMethods = defaultMethods.clone();
+ this.defaultMaskBits = defaultMaskBits.clone();
+ this.parameterNullable = parameterNullable.clone();
+ this.parameterTypes = parameterTypes.clone();
this.propertyNames = propertyNames.clone();
this.propertyGetters = propertyGetters.clone();
- this.propertySetters =
- propertySetters == null ? new Method[propertyNames.length] : propertySetters.clone();
+ this.propertySetters = propertySetters.clone();
this.propertyTypes = propertyTypes.clone();
- int count = constructor.getParameterCount();
- if (this.parameterNames.length != count
- || this.accessors.length != count
- || this.defaultMethods.length != count) {
- throw new IllegalArgumentException("JSON object model arrays must match constructor arity");
+ this.propertyReconstructible = propertyReconstructible.clone();
+ this.propertyRequired = propertyRequired.clone();
+ this.fixedInstance = null;
+ nonPropertyFields = new Field[0];
+ validate();
+ }
+
+ private JsonObjectModel(
+ Object fixedInstance,
+ String[] propertyNames,
+ Method[] propertyGetters,
+ Method[] propertySetters,
+ TypeRef<?>[] propertyTypes,
+ Field[] nonPropertyFields) {
+ creator = null;
+ invocationCreator = null;
+ defaultConstructor = null;
+ parameterNames = new String[0];
+ accessors = new Method[0];
+ defaultMethods = new Method[0];
+ defaultMaskBits = new int[0];
+ parameterNullable = new boolean[0];
+ parameterTypes = new TypeRef<?>[0];
+ if (propertyGetters.length != propertyNames.length
+ || propertySetters.length != propertyNames.length
+ || propertyTypes.length != propertyNames.length) {
+ throw new IllegalArgumentException("Fixed JSON object-model property arrays must match");
}
HashSet<String> names = new HashSet<>();
- for (String name : this.parameterNames) {
+ for (int i = 0; i < propertyNames.length; i++) {
+ String name = propertyNames[i];
if (name == null || name.isEmpty() || !names.add(name)) {
- throw new IllegalArgumentException("Invalid JSON object model parameter name " + name);
+ throw new IllegalArgumentException("Invalid JSON object model property name " + name);
}
+ Objects.requireNonNull(propertyTypes[i], "propertyType");
}
- if (this.propertyGetters.length != this.propertyNames.length
- || this.propertySetters.length != this.propertyNames.length
- || this.propertyTypes.length != this.propertyNames.length) {
+ this.propertyNames = propertyNames.clone();
+ this.propertyGetters = propertyGetters.clone();
+ this.propertySetters = propertySetters.clone();
+ this.propertyTypes = propertyTypes.clone();
+ propertyReconstructible = new boolean[propertyNames.length];
+ propertyRequired = new boolean[propertyNames.length];
+ this.fixedInstance = Objects.requireNonNull(fixedInstance, "fixedInstance");
+ HashSet<Field> fields = new HashSet<>();
+ Class<?> instanceType = fixedInstance.getClass();
+ for (Field field : nonPropertyFields) {
+ Objects.requireNonNull(field, "nonPropertyField");
+ int modifiers = field.getModifiers();
+ if (!field.getDeclaringClass().isAssignableFrom(instanceType)
+ || Modifier.isStatic(modifiers)
+ || !Modifier.isFinal(modifiers)
+ || !fields.add(field)) {
+ throw new IllegalArgumentException("Invalid fixed-model non-property field " + field);
+ }
+ }
+ this.nonPropertyFields = nonPropertyFields.clone();
+ }
+
+ /**
+ * Creates a stateless language singleton model whose JSON representation is exactly {@code {}}.
+ */
+ public static JsonObjectModel fixedInstance(Object instance) {
+ return new JsonObjectModel(
+ instance, new String[0], new Method[0], new Method[0], new TypeRef<?>[0], new Field[0]);
+ }
+
+ /** Creates a singleton candidate with effective-property validation. */
+ public static JsonObjectModel fixedInstance(
+ Object instance,
+ String[] propertyNames,
+ Method[] propertyGetters,
+ Method[] propertySetters,
+ TypeRef<?>[] propertyTypes) {
+ return new JsonObjectModel(
+ instance, propertyNames, propertyGetters, propertySetters, propertyTypes, new Field[0]);
+ }
+
+ /** Creates a fixed model with exact compiler storage excluded from logical JSON state. */
+ public static JsonObjectModel fixedInstance(
+ Object instance,
+ String[] propertyNames,
+ Method[] propertyGetters,
+ Method[] propertySetters,
+ TypeRef<?>[] propertyTypes,
+ Field[] nonPropertyFields) {
+ return new JsonObjectModel(
+ instance,
+ propertyNames,
+ propertyGetters,
+ propertySetters,
+ propertyTypes,
+ nonPropertyFields);
+ }
+
+ public Object fixedInstance() {
+ return fixedInstance;
+ }
+
+ private void validate() {
+ int count = creator.getParameterCount();
+ if (parameterNames.length != count
+ || accessors.length != count
+ || defaultMethods.length != count
+ || defaultMaskBits.length != count
+ || parameterNullable.length != count
+ || parameterTypes.length != count) {
+ throw new IllegalArgumentException("JSON object model arrays must match constructor arity");
+ }
+ if (propertyGetters.length != propertyNames.length
+ || propertySetters.length != propertyNames.length
+ || propertyTypes.length != propertyNames.length
+ || propertyReconstructible.length != propertyNames.length
+ || propertyRequired.length != propertyNames.length) {
throw new IllegalArgumentException(
"JSON object model property arrays must have equal length");
}
+ Class<?>[] logicalCarriers = creator.getParameterTypes();
+ Class<?>[] invocationCarriers = invocationCreator.getParameterTypes();
+ if (invocationCreator.getDeclaringClass() != creator.getDeclaringClass()
+ || invocationCarriers.length < count
+ || invocationCarriers.length > count + 1) {
+ throw new IllegalArgumentException("Invalid JSON object-model invocation constructor");
+ }
+ for (int i = 0; i < count; i++) {
+ if (invocationCarriers[i] != logicalCarriers[i]) {
+ throw new IllegalArgumentException("JSON invocation constructor carrier mismatch");
+ }
+ }
+ if (invocationCarriers.length != count && invocationCarriers[count].isPrimitive()) {
+ throw new IllegalArgumentException("JSON invocation marker must be a reference type");
+ }
+ if (creator instanceof Method || invocationCreator instanceof Method) {
+ if (!(creator instanceof Method)
+ || !(invocationCreator instanceof Method)
+ || invocationCarriers.length != count
+ || defaultConstructor != null) {
+ throw new IllegalArgumentException("Invalid JSON object-model factory targets");
+ }
+ Class<?> owner = creator.getDeclaringClass();
+ if (((Method) creator).getReturnType() != owner
+ || ((Method) invocationCreator).getReturnType() != owner) {
+ throw new IllegalArgumentException("JSON object-model factory must return its exact owner");
+ }
+ }
+ HashSet<String> names = new HashSet<>();
+ for (int i = 0; i < parameterNames.length; i++) {
+ String name = parameterNames[i];
+ if (name == null || name.isEmpty() || !names.add(name)) {
+ throw new IllegalArgumentException("Invalid JSON object model parameter name " + name);
+ }
+ Objects.requireNonNull(parameterTypes[i], "parameterType");
+ if (defaultMethods[i] != null && defaultMaskBits[i] >= 0) {
+ throw new IllegalArgumentException("A constructor parameter has two default mechanisms");
+ }
+ }
names.clear();
- for (String name : this.propertyNames) {
+ for (int i = 0; i < propertyNames.length; i++) {
+ String name = propertyNames[i];
if (name == null || name.isEmpty() || !names.add(name)) {
throw new IllegalArgumentException("Invalid JSON object model property name " + name);
}
+ Objects.requireNonNull(propertyTypes[i], "propertyType");
+ if (propertyRequired[i] && !propertyReconstructible[i]) {
+ throw new IllegalArgumentException(
+ "Required deferred JSON property must be reconstructible " + name);
+ }
+ if (propertyRequired[i]
+ && (propertyTypes[i].getRawType().isPrimitive()
+ || propertyTypes[i].getTypeExtMeta() == null
+ || propertyTypes[i].getTypeExtMeta().nullable()
+ || propertyTypes[i].getTypeExtMeta().nullableWrapper())) {
+ throw new IllegalArgumentException(
+ "Required deferred JSON property must have a non-null reference setter " + name);
+ }
+ }
+ boolean hasMaskedDefault = false;
+ for (int bit : defaultMaskBits) {
+ if (bit >= 0) {
+ hasMaskedDefault = true;
+ break;
+ }
}
- for (Type propertyType : this.propertyTypes) {
- Objects.requireNonNull(propertyType, "propertyType");
+ if (hasMaskedDefault != (defaultConstructor != null)) {
+ throw new IllegalArgumentException(
+ "Compiler-default mask metadata requires exactly one default constructor");
+ }
+ if (defaultConstructor != null) {
+ Class<?>[] invocationTypes = defaultConstructor.getParameterTypes();
+ int maskCount = (count + 31) >>> 5;
+ if (defaultConstructor.getDeclaringClass() != creator.getDeclaringClass()
+ || invocationTypes.length != count + maskCount + 1
+ || !(creator instanceof Constructor)) {
+ throw new IllegalArgumentException("Invalid compiler-default constructor shape");
+ }
+ for (int i = 0; i < count; i++) {
+ if (invocationTypes[i] != creator.getParameterTypes()[i]) {
+ throw new IllegalArgumentException("Compiler-default constructor carrier mismatch");
+ }
+ }
+ for (int i = 0; i < maskCount; i++) {
+ if (invocationTypes[count + i] != int.class) {
+ throw new IllegalArgumentException("Compiler-default mask must use int words");
+ }
+ }
+ if (invocationTypes[invocationTypes.length - 1].isPrimitive()) {
+ throw new IllegalArgumentException("Compiler-default marker must be a reference type");
+ }
+ for (int i = 0; i < count; i++) {
+ int bit = defaultMaskBits[i];
+ if (bit >= 0 && bit != i) {
+ throw new IllegalArgumentException(
+ "Compiler-default mask bit must match parameter index");
+ }
+ }
}
}
- public Constructor<?> constructor() {
- return constructor;
+ public Executable creator() {
+ return creator;
+ }
+
+ public Executable invocationCreator() {
+ return invocationCreator;
+ }
+
+ public Constructor<?> defaultConstructor() {
+ return defaultConstructor;
}
public String[] parameterNames() {
@@ -137,6 +390,18 @@ public final class JsonObjectModel {
return defaultMethods.clone();
}
+ public int[] defaultMaskBits() {
+ return defaultMaskBits.clone();
+ }
+
+ public boolean[] parameterNullable() {
+ return parameterNullable.clone();
+ }
+
+ public TypeRef<?>[] parameterTypes() {
+ return parameterTypes.clone();
+ }
+
public String[] propertyNames() {
return propertyNames.clone();
}
@@ -149,44 +414,81 @@ public final class JsonObjectModel {
return propertySetters.clone();
}
- public Type[] propertyTypes() {
+ public TypeRef<?>[] propertyTypes() {
return propertyTypes.clone();
}
- private static Type[] propertyTypes(
- Constructor<?> constructor,
- String[] parameterNames,
- String[] propertyNames,
- Method[] propertyGetters,
- Method[] propertySetters) {
- Method[] setters = propertySetters == null ? new Method[propertyNames.length] : propertySetters;
- if (propertyGetters.length != propertyNames.length || setters.length != propertyNames.length) {
- throw new IllegalArgumentException(
- "JSON object model property arrays must have equal length");
+ /** Returns whether each property has stable constructor or deferred storage. */
+ public boolean[] propertyReconstructible() {
+ return propertyReconstructible.clone();
+ }
+
+ /** Returns required-deferred flags aligned with {@link #propertyNames()}. */
+ public boolean[] propertyRequired() {
+ return propertyRequired.clone();
+ }
+
+ /** Returns exact compiler storage which is not part of fixed-instance JSON state. */
+ public Field[] nonPropertyFields() {
+ return nonPropertyFields.clone();
+ }
+
+ /** Compares one exact JVM member type with its normalized language-model type. */
+ @Internal
+ public static boolean compatibleType(TypeRef<?> memberType, TypeRef<?> logicalType) {
+ Type member = memberType.getType();
+ Type logical = logicalType.getType();
+ if (member.equals(logical)) {
+ return true;
}
- Type[] parameterTypes = constructor.getGenericParameterTypes();
- Type[] types = new Type[propertyNames.length];
- for (int i = 0; i < types.length; i++) {
- for (int parameterIndex = 0; parameterIndex < parameterNames.length; parameterIndex++) {
- if (propertyNames[i].equals(parameterNames[parameterIndex])) {
- types[i] = parameterTypes[parameterIndex];
- break;
- }
- }
- if (types[i] != null) {
- continue;
- }
- Method getter = propertyGetters[i];
- Method setter = setters[i];
- if (getter != null) {
- types[i] = getter.getGenericReturnType();
- } else if (setter != null && setter.getParameterCount() == 1) {
- types[i] = setter.getGenericParameterTypes()[0];
- } else {
- throw new IllegalArgumentException(
- "JSON object model property has no type source " + propertyNames[i]);
+ if (member instanceof WildcardType
+ && logicalType.getTypeExtMeta() != null
+ && logicalType.getTypeExtMeta().covariant()) {
+ WildcardType wildcard = (WildcardType) member;
+ Type[] upperBounds = wildcard.getUpperBounds();
+ return wildcard.getLowerBounds().length == 0
+ && upperBounds.length == 1
+ && upperBounds[0] != Object.class
+ && compatibleType(TypeRef.of(upperBounds[0]), logicalType);
+ }
+ Class<?> memberRaw = memberType.getRawType();
+ if (memberRaw != logicalType.getRawType()) {
+ return false;
+ }
+ if (memberRaw.isArray()) {
+ TypeRef<?> memberComponent = memberType.getComponentType();
+ TypeRef<?> logicalComponent = logicalType.getComponentType();
+ return memberComponent != null
+ && logicalComponent != null
+ && compatibleType(memberComponent, logicalComponent);
+ }
+ List<TypeRef<?>> memberArguments = memberType.getTypeArguments();
+ List<TypeRef<?>> logicalArguments = logicalType.getTypeArguments();
+ if (memberArguments.size() != logicalArguments.size()) {
+ return false;
+ }
+ for (int i = 0; i < memberArguments.size(); i++) {
+ if (!compatibleType(memberArguments.get(i), logicalArguments.get(i))) {
+ return false;
}
}
- return types;
+ if (member instanceof ParameterizedType && logical instanceof ParameterizedType) {
+ return Objects.equals(
+ ((ParameterizedType) member).getOwnerType(),
+ ((ParameterizedType) logical).getOwnerType());
+ }
+ // Scala 2 qualifies erased Enumeration.Value occurrences with an exact semantic owner.
+ return member instanceof Class
+ && logical instanceof ParameterizedType
+ && memberRaw.getTypeParameters().length == 0
+ && ((ParameterizedType) logical).getActualTypeArguments().length == 0;
+ }
+
+ private static boolean[] allProperties(int count) {
+ boolean[] reconstructible = new boolean[count];
+ for (int i = 0; i < count; i++) {
+ reconstructible[i] = true;
+ }
+ return reconstructible;
}
}
diff --git a/java/fory-json/src/main/java/org/apache/fory/json/codec/MapCodec.java b/java/fory-json/src/main/java/org/apache/fory/json/codec/MapCodec.java
index 2adc5012c..355f374f9 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/codec/MapCodec.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/codec/MapCodec.java
@@ -47,6 +47,7 @@ import org.apache.fory.json.resolver.JsonTypeResolver;
import org.apache.fory.json.writer.JsonWriter;
import org.apache.fory.json.writer.StringJsonWriter;
import org.apache.fory.json.writer.Utf8JsonWriter;
+import org.apache.fory.meta.TypeExtMeta;
import org.apache.fory.platform.GraalvmSupport;
import org.apache.fory.reflect.ReflectionUtils;
import org.apache.fory.reflect.TypeRef;
@@ -130,26 +131,43 @@ public abstract class MapCodec<T extends Map<?, ?>> implements JsonValueCodec<T>
public static MapCodec<?> create(
Class<?> rawType, TypeRef<?> typeRef, JsonTypeResolver resolver) {
Tuple2<TypeRef<?>, TypeRef<?>> keyValueTypeRefs = CodecUtils.mapKeyValueTypeRefs(typeRef);
+ requireNonNullableKey(keyValueTypeRefs.f0);
Type keyType = keyValueTypeRefs.f0.getType();
Class<?> keyRawType = CodecUtils.rawType(keyType, Object.class);
- Type valueType = keyValueTypeRefs.f1.getType();
- Class<?> valueRawType = CodecUtils.rawType(valueType, Object.class);
resolver.checkMapKeySecure(keyRawType);
MapFactory factory = mapFactory(rawType, keyRawType);
- JsonTypeInfo valueTypeInfo = resolver.getTypeInfo(valueType, valueRawType);
+ JsonTypeInfo valueTypeInfo = resolver.getTypeInfo(keyValueTypeRefs.f1);
return create(factory, keyRawType, valueTypeInfo);
}
+ @Internal
+ public static MapCodec<?> create(
+ Class<?> rawType, TypeRef<?> keyType, JsonTypeInfo valueTypeInfo) {
+ requireNonNullableKey(keyType);
+ return create(mapFactory(rawType, keyType.getRawType()), keyType.getRawType(), valueTypeInfo);
+ }
+
@Internal
public static MapCodec<?> create(
Class<?> rawType, Class<?> keyRawType, JsonTypeInfo valueTypeInfo) {
return create(mapFactory(rawType, keyRawType), keyRawType, valueTypeInfo);
}
+ @Internal
+ public static MapCodec<?> create(
+ Class<?> rawType, TypeRef<?> keyType, JsonTypeInfo valueTypeInfo, MapKeyCodec keyCodec) {
+ requireNonNullableKey(keyType);
+ Class<?> keyRawType = keyType.getRawType();
+ return genericMapCodec(
+ mapFactory(rawType, keyRawType),
+ new CheckedMapKeyCodec(keyRawType, keyCodec),
+ valueTypeInfo);
+ }
+
@Internal
public static MapCodec<?> create(
Class<?> rawType, Class<?> keyRawType, JsonTypeInfo valueTypeInfo, MapKeyCodec keyCodec) {
- return new GenericMapCodec(
+ return genericMapCodec(
mapFactory(rawType, keyRawType),
new CheckedMapKeyCodec(keyRawType, keyCodec),
valueTypeInfo);
@@ -160,43 +178,64 @@ public abstract class MapCodec<T extends Map<?, ?>> implements JsonValueCodec<T>
Object valueCodec = valueTypeInfo.stringWriter();
if (keyRawType == String.class) {
if (valueCodec == ScalarCodecs.StringCodec.INSTANCE) {
- return new StringStringMapCodec(factory);
+ return new StringStringMapCodec(factory, valueTypeInfo);
}
if (valueCodec == ScalarCodecs.BooleanCodec.BOXED) {
- return new StringBooleanMapCodec(factory);
+ return new StringBooleanMapCodec(factory, valueTypeInfo);
}
if (valueCodec == ScalarCodecs.IntCodec.BOXED) {
- return new StringIntMapCodec(factory);
+ return new StringIntMapCodec(factory, valueTypeInfo);
}
if (valueCodec == ScalarCodecs.LongCodec.BOXED) {
- return new StringLongMapCodec(factory);
+ return new StringLongMapCodec(factory, valueTypeInfo);
}
if (valueCodec == ScalarCodecs.ShortCodec.BOXED) {
- return new StringShortMapCodec(factory);
+ return new StringShortMapCodec(factory, valueTypeInfo);
}
if (valueCodec == ScalarCodecs.ByteCodec.BOXED) {
- return new StringByteMapCodec(factory);
+ return new StringByteMapCodec(factory, valueTypeInfo);
}
if (valueCodec == ScalarCodecs.FloatCodec.BOXED) {
- return new StringFloatMapCodec(factory);
+ return new StringFloatMapCodec(factory, valueTypeInfo);
}
if (valueCodec == ScalarCodecs.DoubleCodec.BOXED) {
- return new StringDoubleMapCodec(factory);
+ return new StringDoubleMapCodec(factory, valueTypeInfo);
}
if (valueCodec == ScalarCodecs.BigIntegerCodec.INSTANCE) {
- return new StringBigIntegerMapCodec(factory);
+ return new StringBigIntegerMapCodec(factory, valueTypeInfo);
}
if (valueCodec == ScalarCodecs.BigDecimalCodec.INSTANCE) {
- return new StringBigDecimalMapCodec(factory);
+ return new StringBigDecimalMapCodec(factory, valueTypeInfo);
}
}
if (keyRawType == Object.class) {
- return new GenericMapCodec(factory, OBJECT_KEY_CODEC, valueTypeInfo);
+ return genericMapCodec(factory, OBJECT_KEY_CODEC, valueTypeInfo);
}
if (valueCodec == ScalarCodecs.StringCodec.INSTANCE && isNumericKey(keyRawType)) {
- return new NumberStringMapCodec(factory, defaultKeyCodec(keyRawType));
+ return new NumberStringMapCodec(factory, defaultKeyCodec(keyRawType), valueTypeInfo);
+ }
+ return genericMapCodec(factory, defaultKeyCodec(keyRawType), valueTypeInfo);
+ }
+
+ /** Creates a map for an exact factory-owned key codec whose result type is already validated. */
+ @Internal
+ public static MapCodec<?> createUncheckedKeyCodec(
+ Class<?> rawType, Class<?> keyRawType, JsonTypeInfo valueTypeInfo, MapKeyCodec keyCodec) {
+ return genericMapCodec(mapFactory(rawType, keyRawType), keyCodec, valueTypeInfo);
+ }
+
+ private static GenericMapCodec genericMapCodec(
... 27479 lines suppressed ...
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org