diff --git a/moshi/src/main/java/com/squareup/moshi/ToJson.java b/moshi/src/main/java/com/squareup/moshi/FromJson.kt similarity index 68% rename from moshi/src/main/java/com/squareup/moshi/ToJson.java rename to moshi/src/main/java/com/squareup/moshi/FromJson.kt index 82380be..f9e96d8 100644 --- a/moshi/src/main/java/com/squareup/moshi/ToJson.java +++ b/moshi/src/main/java/com/squareup/moshi/FromJson.kt @@ -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 diff --git a/moshi/src/main/java/com/squareup/moshi/Json.java b/moshi/src/main/java/com/squareup/moshi/Json.java deleted file mode 100644 index 1b5bf1a..0000000 --- a/moshi/src/main/java/com/squareup/moshi/Json.java +++ /dev/null @@ -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. - * - *
Although this annotation doesn't declare a {@link Target}, it is only honored in the following - * elements: - * - *
Users of the AutoValue: Moshi - * Extension 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. - * - *
Note: this has no effect in enums or record classes. - */ - boolean ignore() default false; -} diff --git a/moshi/src/main/java/com/squareup/moshi/Json.kt b/moshi/src/main/java/com/squareup/moshi/Json.kt new file mode 100644 index 0000000..5f5d97c --- /dev/null +++ b/moshi/src/main/java/com/squareup/moshi/Json.kt @@ -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" + } +} diff --git a/moshi/src/main/java/com/squareup/moshi/JsonClass.java b/moshi/src/main/java/com/squareup/moshi/JsonClass.java deleted file mode 100644 index 8be06ce..0000000 --- a/moshi/src/main/java/com/squareup/moshi/JsonClass.java +++ /dev/null @@ -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. - * - *
There are currently some restrictions on which types that can be used with generated - * adapters: - * - *
Requirements for generated adapter class signatures: - * - *
Example for a class "CustomType": - * - *
{@code - * class CustomTypeJsonAdapter(moshi: Moshi, types: Array- * - *) : JsonAdapter () { - * // ... - * } - * }
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 "";
-}
diff --git a/moshi/src/main/java/com/squareup/moshi/JsonClass.kt b/moshi/src/main/java/com/squareup/moshi/JsonClass.kt
new file mode 100644
index 0000000..5823064
--- /dev/null
+++ b/moshi/src/main/java/com/squareup/moshi/JsonClass.kt
@@ -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