feat(json): allow @JsonWriter on enum instance method and property getter#705
Open
dsudomoin wants to merge 1 commit into
Open
Conversation
…tter
Previously an enum's @JsonWriter value extractor had to be a static method (Kotlin companion object / Java public static) taking the enum and returning the JSON value. This extends the supported forms: an instance method `@JsonWriter fun jsonValue(): V` (0 params, including `@JsonWriter override fun toString()`), and a Kotlin property getter `@get:JsonWriter val value: V` (KSP only - Java has no property concept).
The static (companion) form is unchanged and still validated; the two method forms are disambiguated by parameter count (static: 1 param of the enum type; instance: 0 params). Generated code references an instance member as Enum::member (a (Enum) -> V function), the static one via a { e -> Enum.m(e) } lambda - no runtime EnumJsonWriter change.
KSP: EnumJsonWriterGenerator.detectWriterMethod now accepts body methods and getter-annotated properties; JsonSymbolProcessor triggers writer generation for a @get:JsonWriter property (handling both KSPropertyDeclaration and KSPropertyGetter, since the reported symbol varies by KSP version). APT: EnumWriterGenerator.detectWriterMethod accepts instance methods; the Enum::method codegen already resolves to Function<Enum,V> for both static and instance forms.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Enum
@JsonWriter(from #701) required the value extractor to be a static method — a Kotlincompanion objectfunction (or Javapublic static) taking the enum and returning the JSON value. Putting@JsonWriteron a plain instance method produced a hardmust be staticerror, and there was no property-level form. This PR broadens the accepted shapes so the common cases read naturally.What changed
@JsonWriteron an enum now additionally accepts:@JsonWriter fun jsonValue(): V(0 params), including@JsonWriter override fun toString(): V;@get:JsonWriter val value: V(KSP only; Java has no property concept).The static (companion) form is unchanged and validated exactly as before. The forms are disambiguated by parameter count:
public static)@get:on the propertyValue-source priority is unchanged:
@JsonWriter(any form) →@Jsongetter →toString.Examples
Implementation
No runtime change — the generated writer still delegates to
EnumJsonWriter<T,V>; only the extractor code block and the value type change.EnumJsonWriterGenerator.detectWriterMethod: accepts body methods and getter-annotated properties in addition to companion methods. An instance member is emitted asEnum::member(a(Enum) -> V), the static one as{ e -> Enum.m(e) }.JsonSymbolProcessor: triggers writer generation for a@get:JsonWriterproperty, handling bothKSPropertyDeclarationandKSPropertyGetter(the symbol reported bygetSymbolsWithAnnotationfor a@get:target varies by KSP version).EnumWriterGenerator.detectWriterMethod: accepts instance methods; the existingEnum::methodcodegen already resolves toFunction<Enum,V>for both static and instance forms, sogenerateEnumWriteris untouched. (Property getters are Kotlin-only, so APT stays method-only.)Tests
Added to
EnumTest(KSP + APT), covering: instance method,toStringoverride, property getter (with and without@Json, to exercise the generation trigger), the from-extension path, and negative cases (instance method with params →no parameters; non-public →must be public). Full:json:json-symbol-processor:testand:json:json-annotation-processor:testare green.