mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 07:59:21 +08:00
Convert Moshi annotations to Kotlin (#1472)
This commit is contained in:
@@ -13,13 +13,11 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.squareup.moshi;
|
package com.squareup.moshi
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import kotlin.annotation.AnnotationRetention.RUNTIME
|
||||||
import java.lang.annotation.Retention;
|
import kotlin.annotation.AnnotationTarget.FUNCTION
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RUNTIME)
|
||||||
@Target(ElementType.METHOD)
|
@Target(FUNCTION)
|
||||||
public @interface ToJson {}
|
public annotation class FromJson
|
@@ -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;
|
|
||||||
}
|
|
48
moshi/src/main/java/com/squareup/moshi/Json.kt
Normal file
48
moshi/src/main/java/com/squareup/moshi/Json.kt
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
@@ -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 Moshi’s built-in
|
|
||||||
* generator to create the API signature to get started, then make your own generator match that
|
|
||||||
* expected signature.
|
|
||||||
*/
|
|
||||||
String generator() default "";
|
|
||||||
}
|
|
67
moshi/src/main/java/com/squareup/moshi/JsonClass.kt
Normal file
67
moshi/src/main/java/com/squareup/moshi/JsonClass.kt
Normal 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 Moshi’s built-in
|
||||||
|
* generator to create the API signature to get started, then make your own generator match that
|
||||||
|
* expected signature.
|
||||||
|
*/
|
||||||
|
val generator: String = ""
|
||||||
|
)
|
@@ -13,17 +13,13 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.squareup.moshi;
|
package com.squareup.moshi
|
||||||
|
|
||||||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
|
import kotlin.annotation.AnnotationRetention.RUNTIME
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
import kotlin.annotation.AnnotationTarget.ANNOTATION_CLASS
|
||||||
|
|
||||||
import java.lang.annotation.Documented;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
/** Annotates another annotation, causing it to specialize how values are encoded and decoded. */
|
/** Annotates another annotation, causing it to specialize how values are encoded and decoded. */
|
||||||
@Target(ANNOTATION_TYPE)
|
@Target(ANNOTATION_CLASS)
|
||||||
@Retention(RUNTIME)
|
@Retention(RUNTIME)
|
||||||
@Documented
|
@MustBeDocumented
|
||||||
public @interface JsonQualifier {}
|
public annotation class JsonQualifier
|
@@ -13,13 +13,11 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.squareup.moshi;
|
package com.squareup.moshi
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import kotlin.annotation.AnnotationRetention.RUNTIME
|
||||||
import java.lang.annotation.Retention;
|
import kotlin.annotation.AnnotationTarget.FUNCTION
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RUNTIME)
|
||||||
@Target(ElementType.METHOD)
|
@Target(FUNCTION)
|
||||||
public @interface FromJson {}
|
public annotation class ToJson
|
Reference in New Issue
Block a user