Convert Moshi annotations to Kotlin (#1472)

This commit is contained in:
Zac Sweers
2022-01-03 13:53:56 -05:00
committed by GitHub
parent 82cf56ab76
commit e5e4c48767
7 changed files with 133 additions and 159 deletions

View File

@@ -13,13 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.moshi;
package com.squareup.moshi
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ToJson {}
@Retention(RUNTIME)
@Target(FUNCTION)
public annotation class FromJson

View File

@@ -1,56 +0,0 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed 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
*
* https://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 com.squareup.moshi;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* Customizes how a field is encoded as JSON.
*
* <p>Although this annotation doesn't declare a {@link Target}, it is only honored in the following
* elements:
*
* <ul>
* <li><strong>Java class fields</strong>
* <li><strong>Kotlin properties</strong> for use with {@code moshi-kotlin} or {@code
* moshi-kotlin-codegen}. This includes both properties declared in the constructor and
* properties declared as members.
* </ul>
*
* <p>Users of the <a href="https://github.com/rharter/auto-value-moshi">AutoValue: Moshi
* Extension</a> may also use this annotation on abstract getters.
*/
@Retention(RUNTIME)
@Documented
public @interface Json {
/** The default value of {@link #name()}. Should only be used to check if it's been set. */
String UNSET_NAME = "\u0000";
/** The name of the field when encoded as JSON. */
String name() default UNSET_NAME;
/**
* If true, this field/property will be ignored. This is semantically similar to use of {@code
* transient} on the JVM.
*
* <p><strong>Note:</strong> this has no effect in enums or record classes.
*/
boolean ignore() default false;
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed 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
*
* https://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 com.squareup.moshi
import kotlin.annotation.AnnotationRetention.RUNTIME
/**
* Customizes how a field is encoded as JSON.
*
* Although this annotation doesn't declare a [Target], it is only honored in the following
* elements:
* - **Java class fields**
* - **Kotlin properties** for use with `moshi-kotlin` or `moshi-kotlin-codegen`. This includes both properties
* declared in the constructor and properties declared as members.
*
* Users of the [AutoValue: Moshi Extension](https://github.com/rharter/auto-value-moshi) may also use this
* annotation on abstract getters.
*/
@Retention(RUNTIME)
@MustBeDocumented
public annotation class Json(
/** The name of the field when encoded as JSON. */
val name: String = UNSET_NAME,
/**
* If true, this field/property will be ignored. This is semantically similar to use of `transient` on the JVM.
*
* **Note:** this has no effect in `enum` or `record` classes.
*/
val ignore: Boolean = false
) {
public companion object {
/** The default value of [name]. Should only be used to check if it's been set. */
public const val UNSET_NAME: String = "\u0000"
}
}

View File

@@ -1,77 +0,0 @@
/*
* Copyright (C) 2018 Square, Inc.
*
* Licensed 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
*
* https://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 com.squareup.moshi;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.reflect.Type;
/** Customizes how a type is encoded as JSON. */
@Retention(RUNTIME)
@Documented
public @interface JsonClass {
/**
* True to trigger the annotation processor to generate an adapter for this type.
*
* <p>There are currently some restrictions on which types that can be used with generated
* adapters:
*
* <ul>
* <li>The class must be implemented in Kotlin (unless using a custom generator, see {@link
* #generator()}).
* <li>The class may not be an abstract class, an inner class, or a local class.
* <li>All superclasses must be implemented in Kotlin.
* <li>All properties must be public, protected, or internal.
* <li>All properties must be either non-transient or have a default value.
* </ul>
*/
boolean generateAdapter();
/**
* An optional custom generator tag used to indicate which generator should be used. If empty,
* Moshi's annotation processor will generate an adapter for the annotated type. If not empty,
* Moshi's processor will skip it and defer to a custom generator. This can be used to allow other
* custom code generation tools to run and still allow Moshi to read their generated JsonAdapter
* outputs.
*
* <p>Requirements for generated adapter class signatures:
*
* <ul>
* <li>The generated adapter must subclass {@link JsonAdapter} and be parameterized by this
* type.
* <li>{@link Types#generatedJsonAdapterName} should be used for the fully qualified class name
* in order for Moshi to correctly resolve and load the generated JsonAdapter.
* <li>The first parameter must be a {@link Moshi} instance.
* <li>If generic, a second {@link Type Type[]} parameter should be declared to accept type
* arguments.
* </ul>
*
* <p>Example for a class "CustomType":
*
* <pre>{@code
* class CustomTypeJsonAdapter(moshi: Moshi, types: Array<Type>) : JsonAdapter<CustomType>() {
* // ...
* }
* }</pre>
*
* <p>To help ensure your own generator meets requirements above, you can use Moshis built-in
* generator to create the API signature to get started, then make your own generator match that
* expected signature.
*/
String generator() default "";
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright (C) 2018 Square, Inc.
*
* Licensed 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
*
* https://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 com.squareup.moshi
import java.lang.reflect.Type
import kotlin.annotation.AnnotationRetention.RUNTIME
/** Customizes how a type is encoded as JSON. */
@Retention(RUNTIME)
@MustBeDocumented
public annotation class JsonClass(
/**
* True to trigger the annotation processor to generate an adapter for this type.
*
* There are currently some restrictions on which types that can be used with generated
* adapters:
* - The class must be implemented in Kotlin (unless using a custom generator, see [generator]).
* - The class may not be an abstract class, an inner class, or a local class.
* - All superclasses must be implemented in Kotlin.
* - All properties must be public, protected, or internal.
* - All properties must be either non-transient or have a default value.
*
*/
val generateAdapter: Boolean,
/**
* An optional custom generator tag used to indicate which generator should be used. If empty,
* Moshi's annotation processor will generate an adapter for the annotated type. If not empty,
* Moshi's processor will skip it and defer to a custom generator. This can be used to allow other
* custom code generation tools to run and still allow Moshi to read their generated JsonAdapter
* outputs.
*
* Requirements for generated adapter class signatures:
* - The generated adapter must subclass [JsonAdapter] and be parameterized by this
* type.
* - [Types.generatedJsonAdapterName] should be used for the fully qualified class name
* in order for Moshi to correctly resolve and load the generated JsonAdapter.
* - The first parameter must be a [Moshi] instance.
* - If generic, a second [Array<Type>][Type] parameter should be declared to accept type
* arguments.
*
* Example for a class "CustomType":
*
* ```
* class CustomTypeJsonAdapter(moshi: Moshi, types: Array<Type>) : JsonAdapter<CustomType>() {
* // ...
* }
* ```
*
* To help ensure your own generator meets requirements above, you can use Moshis built-in
* generator to create the API signature to get started, then make your own generator match that
* expected signature.
*/
val generator: String = ""
)

View File

@@ -13,17 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.moshi;
package com.squareup.moshi
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.ANNOTATION_CLASS
/** Annotates another annotation, causing it to specialize how values are encoded and decoded. */
@Target(ANNOTATION_TYPE)
@Target(ANNOTATION_CLASS)
@Retention(RUNTIME)
@Documented
public @interface JsonQualifier {}
@MustBeDocumented
public annotation class JsonQualifier

View File

@@ -13,13 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.moshi;
package com.squareup.moshi
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface FromJson {}
@Retention(RUNTIME)
@Target(FUNCTION)
public annotation class ToJson