From 8b30dacd1d96670e9ff58f466ba3f2e49d49a662 Mon Sep 17 00:00:00 2001 From: Sampath <61689771+SampathKumarAmex@users.noreply.github.com> Date: Tue, 11 Jan 2022 15:14:24 +0000 Subject: [PATCH] Converted the read json list Java file to Kotlin (#1495) --- examples/build.gradle.kts | 10 ++++ .../squareup/moshi/recipes/ReadJsonListKt.kt | 50 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 examples/src/main/java/com/squareup/moshi/recipes/ReadJsonListKt.kt diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index e16487f..23bfa13 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -1,3 +1,5 @@ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + plugins { kotlin("jvm") kotlin("kapt") @@ -9,3 +11,11 @@ dependencies { implementation(project(":moshi")) implementation(project(":moshi-adapters")) } + +tasks.withType().configureEach { + kotlinOptions { + freeCompilerArgs += listOf( + "-Xopt-in=kotlin.ExperimentalStdlibApi" + ) + } +} diff --git a/examples/src/main/java/com/squareup/moshi/recipes/ReadJsonListKt.kt b/examples/src/main/java/com/squareup/moshi/recipes/ReadJsonListKt.kt new file mode 100644 index 0000000..444762b --- /dev/null +++ b/examples/src/main/java/com/squareup/moshi/recipes/ReadJsonListKt.kt @@ -0,0 +1,50 @@ +/* + * 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.recipes + +import com.squareup.moshi.Moshi +import com.squareup.moshi.adapter +import com.squareup.moshi.recipes.models.Card + +class ReadJsonListKt { + + //language=JSON + private val jsonString = """ + [{"rank": "4", + "suit": "CLUBS" + }, + {"rank": "A", + "suit": "HEARTS" + }, + {"rank": "J", + "suit": "SPADES" + }] + """.trimIndent() + + fun readJsonList() { + val jsonAdapter = Moshi.Builder().build().adapter>() + val cards = jsonAdapter.fromJson(jsonString)!! + println(cards) + cards[0].run { + println(rank) + println(suit) + } + } +} + +fun main() { + ReadJsonListKt().readJsonList() +}