From bc85227f290847a0569b199db0c83d82158c9b12 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Fri, 25 Sep 2020 22:51:05 -0400 Subject: [PATCH] Add JsonString recipe to examples (#1230) * Start recipes module with JsonString recipe * Make recipes a kotlin project * Move JsonString sample to examples * Add license * Include all java modules * Add kotlin options * Spotless * Remove jvmtarget I was today years old when I found out 1.7 isn't a valid target * Make gradle happy * Add codegen * Spotless --- build.gradle.kts | 2 +- examples/build.gradle.kts | 12 +++- .../com/squareup/moshi/recipes/JsonString.kt | 71 +++++++++++++++++++ kotlin/codegen/build.gradle.kts | 6 ++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 examples/src/main/java/com/squareup/moshi/recipes/JsonString.kt diff --git a/build.gradle.kts b/build.gradle.kts index 19c6adb..76f58c3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -112,7 +112,7 @@ subprojects { } } - pluginManager.withPlugin("java-library") { + pluginManager.withPlugin("java") { configure { sourceCompatibility = JavaVersion.VERSION_1_7 targetCompatibility = JavaVersion.VERSION_1_7 diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 5a2b654..dc7a8a8 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -14,11 +14,21 @@ * limitations under the License. */ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + plugins { - `java-library` + kotlin("jvm") + kotlin("kapt") +} + +tasks.withType().configureEach { + kotlinOptions { + freeCompilerArgs = listOf("-progressive") + } } dependencies { + kapt(project(":kotlin:codegen")) compileOnly(Dependencies.jsr305) implementation(project(":moshi")) implementation(project(":adapters")) diff --git a/examples/src/main/java/com/squareup/moshi/recipes/JsonString.kt b/examples/src/main/java/com/squareup/moshi/recipes/JsonString.kt new file mode 100644 index 0000000..975a9cc --- /dev/null +++ b/examples/src/main/java/com/squareup/moshi/recipes/JsonString.kt @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2020 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.recipes + +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.JsonClass +import com.squareup.moshi.JsonQualifier +import com.squareup.moshi.JsonReader +import com.squareup.moshi.JsonWriter +import com.squareup.moshi.Moshi +import com.squareup.moshi.Moshi.Builder +import com.squareup.moshi.Types +import okio.BufferedSource +import java.lang.reflect.Type +import kotlin.annotation.AnnotationRetention.RUNTIME + +@JsonClass(generateAdapter = true) +data class ExampleClass( + val type: Int, + @JsonString val rawJson: String +) + +@Retention(RUNTIME) +@JsonQualifier +annotation class JsonString + +class JsonStringJsonAdapterFactory : JsonAdapter.Factory { + override fun create(type: Type, annotations: Set, moshi: Moshi): JsonAdapter<*>? { + if (type != String::class.java) return null + Types.nextAnnotations(annotations, JsonString::class.java) ?: return null + return JsonStringJsonAdapter().nullSafe() + } + + private class JsonStringJsonAdapter : JsonAdapter() { + override fun fromJson(reader: JsonReader): String = + reader.nextSource().use(BufferedSource::readUtf8) + + override fun toJson(writer: JsonWriter, value: String?) { + writer.valueSink().use { sink -> sink.writeUtf8(checkNotNull(value)) } + } + } +} + +fun main() { + //language=JSON + val json = "{\"type\":1,\"rawJson\":{\"a\":2,\"b\":3,\"c\":[1,2,3]}}" + + val moshi = Builder() + .add(JsonStringJsonAdapterFactory()) + .build() + + val example: ExampleClass = moshi.adapter(ExampleClass::class.java).fromJson(json)!! + + check(example.type == 1) + + //language=JSON + check(example.rawJson == "{\"a\":2,\"b\":3,\"c\":[1,2,3]}") +} diff --git a/kotlin/codegen/build.gradle.kts b/kotlin/codegen/build.gradle.kts index 1eb5159..646a02e 100644 --- a/kotlin/codegen/build.gradle.kts +++ b/kotlin/codegen/build.gradle.kts @@ -35,6 +35,12 @@ tasks.withType().configureEach { } } +// To make Gradle happy +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + val shade: Configuration = configurations.maybeCreate("compileShaded") configurations.getByName("compileOnly").extendsFrom(shade) dependencies {