Skip to content

Map fastjson features to the right fastjson2 enum and migrate @JSONField imports - #1342

Merged
timtebeek merged 2 commits into
mainfrom
tim/fastjson2-feature-mapping
Oct 24, 2026
Merged

timtebeek merged 2 commits into
mainfrom
tim/fastjson2-feature-mapping

Conversation

@timtebeek

Copy link
Copy Markdown
Member

Fixes G8 from #1330.

MigrateFastjsonForJakarta10 (part of JakartaEE10) mapped fastjson 1's write-side SerializerFeature to fastjson2's read-side JSONReader.Feature and vice versa. The dotted nested-type names also left references unqualified and unimported, so JSON.toJSONString(o, SerializerFeature.PrettyFormat) became JSON.toJSONString(o, Feature.PrettyFormat) with no import. The targets are now swapped and use the $ notation ChangeType expects for nested types.

ChangePackage com.alibaba.fastjson isn't recursive, so @JSONField and the other annotations kept their fastjson 1 imports after the dependency was swapped to fastjson2. I didn't make it recursive because the other subpackages (serializer, parser, support.*) don't map 1:1 to fastjson2: a recursive rename would just move the unmapped types into fastjson2 packages that don't exist. Instead there are now explicit ChangeTypes for the four annotation types. @JSONPOJOBuilder maps to @JSONBuilder.

Things a reviewer should know:

  • Many feature constants don't exist under the new enum names. I checked fastjson 1.2.83 against fastjson2 2.0.60 with javap. 20 of the 30 SerializerFeature constants exist in JSONWriter.Feature, including PrettyFormat, WriteMapNullValue, WriteClassName and BeanToArray. The ones that don't include DisableCircularReferenceDetect, WriteDateUseDateFormat, SortField, QuoteFieldNames, UseISO8601DateFormat and SkipTransientField. Only 6 of the 27 parser.Feature constants exist in JSONReader.Feature: AllowUnQuotedFieldNames, SupportArrayToBean, SupportAutoType, InitStringFieldAsEmpty, NonStringKeyAsString and ErrorOnEnumNotMatch. This PR doesn't map the renamed or removed constants. Before it, none of them resolved.
  • The annotation attributes serialzeFeatures/parseFeatures were renamed to serializeFeatures/deserializeFeatures in fastjson2, and those aren't renamed here either.
  • There's an upstream ChangeType bug: changing a top-level type to a nested type that's used as a static-field qualifier also adds an unused import com.alibaba.fastjson2.JSONWriter.Feature;. It reproduces with JDK types too (TextStyle.FULL to Locale$Category gives import java.util.Locale.Category; next to Locale.Category.FULL). On its own that's harmless, so the expected outputs include it. A file that uses both enums ends up importing two Features, though, and won't compile. readerAndWriterFeatureInOneFile covers that case as @ExpectedToFail until the fix lands in rewrite-java.
  • fastjson 1.2.83 was added as a testRuntimeOnly dependency so the tests can parse against classpath("fastjson").

Verified with ./gradlew test --tests 'org.openrewrite.java.migrate.jakarta.MigrateFastjsonForJakarta10Test' --tests 'org.openrewrite.java.migrate.jakarta.JakartaEE10Test' --tests 'org.openrewrite.java.migrate.jakarta.JakartaEE11Test': 3 passed, 1 expected to fail. Before the fix, all three fastjson tests failed: the enum tests produced an unqualified, unimported Feature, and @JSONField was left untouched. recipeCsvGenerate only changed the counts for this recipe, JakartaEE10 and JakartaEE11.

…Feature` to `JSONReader.Feature`

The write-side `SerializerFeature` was mapped to the read-side `JSONReader.Feature` and vice versa, and the dotted nested-type names left the references unqualified and unimported, so `JSON.toJSONString(o, SerializerFeature.PrettyFormat)` no longer compiled after the migration.
….fastjson2.annotation`

`ChangePackage` for `com.alibaba.fastjson` is not recursive, so `@JSONField`, `@JSONType`, `@JSONCreator` and `@JSONPOJOBuilder` kept their fastjson 1 imports while the dependency was swapped to fastjson2. Those four have direct counterparts in `com.alibaba.fastjson2.annotation` (`@JSONPOJOBuilder` is `@JSONBuilder` there); other subpackages don't map 1:1, so they are not made recursive.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Oct 24, 2026
@timtebeek
timtebeek merged commit 62f3a51 into main Oct 24, 2026
2 checks passed
@timtebeek
timtebeek deleted the tim/fastjson2-feature-mapping branch October 24, 2026 14:05
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant