Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import com.diffplug.gradle.spotless.JavaExtension
import com.vanniktech.maven.publish.MavenPublishBaseExtension
import java.net.URI
import org.jetbrains.dokka.gradle.DokkaExtension
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.net.URI

buildscript {
dependencies {
val kotlinVersion =
System.getenv("MOSHI_KOTLIN_VERSION")
?: libs.versions.kotlin.get()
val kspVersion =
System.getenv("MOSHI_KSP_VERSION")
?: libs.versions.ksp.get()
val kotlinVersion = System.getenv("MOSHI_KOTLIN_VERSION") ?: libs.versions.kotlin.get()
val kspVersion = System.getenv("MOSHI_KSP_VERSION") ?: libs.versions.ksp.get()
classpath(kotlin("gradle-plugin", version = kotlinVersion))
classpath("com.google.devtools.ksp:symbol-processing-gradle-plugin:$kspVersion")
}
Expand All @@ -31,9 +27,7 @@ allprojects {
group = "com.squareup.moshi"
version = "2.0.0-SNAPSHOT"

repositories {
mavenCentral()
}
repositories { mavenCentral() }
}

spotless {
Expand All @@ -52,20 +46,14 @@ spotless {
targetExclude("**/build/**")
}
kotlin {
ktlint(libs.ktlint.get().version).editorConfigOverride(
mapOf(
"ktlint_standard_filename" to "disabled",
// Making something an expression body should be a choice around readability.
"ktlint_standard_function-expression-body" to "disabled",
),
)
ktfmt(libs.ktfmt.get().version).googleStyle()
target("**/*.kt")
trimTrailingWhitespace()
endWithNewline()
targetExclude("**/Dependencies.kt", "**/build/**")
}
kotlinGradle {
ktlint(libs.ktlint.get().version)
ktfmt(libs.ktfmt.get().version).googleStyle()
target("**/*.gradle.kts")
trimTrailingWhitespace()
endWithNewline()
Expand All @@ -76,14 +64,10 @@ subprojects {
// Apply with "java" instead of just "java-library" so kotlin projects get it too
pluginManager.withPlugin("java") {
configure<JavaPluginExtension> {
toolchain {
languageVersion.set(libs.versions.jdk.map(JavaLanguageVersion::of))
}
toolchain { languageVersion.set(libs.versions.jdk.map(JavaLanguageVersion::of)) }
}
if (project.name != "records-tests") {
tasks.withType<JavaCompile>().configureEach {
options.release.set(8)
}
tasks.withType<JavaCompile>().configureEach { options.release.set(8) }
}
}

Expand All @@ -110,11 +94,7 @@ dependencies {
dokka(project(":moshi-kotlin-codegen"))
}

dokka {
dokkaPublications.html {
outputDirectory.set(layout.projectDirectory.dir("docs/2.x"))
}
}
dokka { dokkaPublications.html { outputDirectory.set(layout.projectDirectory.dir("docs/2.x")) } }

subprojects {
plugins.withId("org.jetbrains.dokka") {
Expand All @@ -135,9 +115,7 @@ subprojects {
sourceLink {
localDirectory.set(layout.projectDirectory.dir("src"))
val relPath =
rootProject.isolated.projectDirectory.asFile
.toPath()
.relativize(projectDir.toPath())
rootProject.isolated.projectDirectory.asFile.toPath().relativize(projectDir.toPath())
remoteUrl("https://github.com/square/moshi/tree/main/$relPath/src")
remoteLineSuffix.set("#L")
}
Expand Down
6 changes: 1 addition & 5 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,5 @@ dependencies {
}

tasks.withType<KotlinCompile>().configureEach {
compilerOptions {
freeCompilerArgs.add(
"-opt-in=kotlin.ExperimentalStdlibApi",
)
}
compilerOptions { freeCompilerArgs.add("-opt-in=kotlin.ExperimentalStdlibApi") }
}
14 changes: 5 additions & 9 deletions examples/src/main/java/com/squareup/moshi/recipes/JsonString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ 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
import okio.BufferedSource

@JsonClass(generateAdapter = true)
data class ExampleClass(val type: Int, @JsonString val rawJson: String)

@Retention(RUNTIME)
@JsonQualifier
annotation class JsonString
@Retention(RUNTIME) @JsonQualifier annotation class JsonString

class JsonStringJsonAdapterFactory : JsonAdapter.Factory {
override fun create(type: Type, annotations: Set<Annotation>, moshi: Moshi): JsonAdapter<*>? {
Expand All @@ -52,17 +50,15 @@ class JsonStringJsonAdapterFactory : JsonAdapter.Factory {
}

fun main() {
//language=JSON
// language=JSON
val json = "{\"type\":1,\"rawJson\":{\"a\":2,\"b\":3,\"c\":[1,2,3]}}"

val moshi = Builder()
.add(JsonStringJsonAdapterFactory())
.build()
val moshi = Builder().add(JsonStringJsonAdapterFactory()).build()

val example: ExampleClass = moshi.adapter<ExampleClass>().fromJson(json)

check(example.type == 1)

//language=JSON
// language=JSON
check(example.rawJson == "{\"a\":2,\"b\":3,\"c\":[1,2,3]}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import com.squareup.moshi.recipes.models.Card

class ReadJsonListKt {

//language=JSON
private val jsonString = """
// language=JSON
private val jsonString =
"""
[{"rank": "4",
"suit": "CLUBS"
},
Expand All @@ -32,7 +33,8 @@ class ReadJsonListKt {
{"rank": "J",
"suit": "SPADES"
}]
""".trimIndent()
"""
.trimIndent()

fun readJsonList() {
val jsonAdapter = Moshi.Builder().build().adapter<List<Card>>()
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ kotlinCompileTesting = { module = "dev.zacsweers.kctfork:core", version.ref = "k
kotlinCompileTesting-ksp = { module = "dev.zacsweers.kctfork:ksp", version.ref ="kotlinCompileTesting" }
truth = "com.google.truth:truth:1.4.5"
googleJavaFormat = "com.google.googlejavaformat:google-java-format:1.33.0"
ktlint = "com.pinterest.ktlint:ktlint-cli:1.8.0"
ktfmt = "com.facebook:ktfmt:0.59"
8 changes: 2 additions & 6 deletions moshi-adapters/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ dependencies {
}

tasks.withType<Jar>().configureEach {
manifest {
attributes("Automatic-Module-Name" to "com.squareup.moshi.adapters")
}
manifest { attributes("Automatic-Module-Name" to "com.squareup.moshi.adapters") }
}

configure<MavenPublishBaseExtension> {
configure(KotlinJvm(javadocJar = None()))
}
configure<MavenPublishBaseExtension> { configure(KotlinJvm(javadocJar = None())) }
8 changes: 2 additions & 6 deletions moshi-adapters/japicmp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ val latest = configurations.create("latest")
dependencies {
baseline("com.squareup.moshi:moshi-adapters:1.15.2") {
isTransitive = false
version {
strictly("1.14.0")
}
version { strictly("1.14.0") }
}
latest(project(":moshi-adapters"))
}
Expand All @@ -30,6 +28,4 @@ val japicmp =
includeSynthetic.set(true)
}

tasks.named("check").configure {
dependsOn(japicmp)
}
tasks.named("check").configure { dependsOn(japicmp) }
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.squareup.moshi

import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
import okio.IOException
import java.util.Date
import okio.IOException

@Deprecated(
"""This class moved to avoid a package name conflict in the Java Platform Module System.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,26 @@ import okio.IOException
* A JsonAdapter for enums that allows having a fallback enum value when a deserialized string does
* not match any enum value. To use, add this as an adapter for your enum type on your
* [Moshi.Builder][com.squareup.moshi.Moshi.Builder]:
*
* ```
* Moshi moshi = new Moshi.Builder()
* .add(CurrencyCode.class, EnumJsonAdapter.create(CurrencyCode.class)
* .withUnknownFallback(CurrencyCode.USD))
* .build();
* ```
*/
public class EnumJsonAdapter<T : Enum<T>> internal constructor(
public class EnumJsonAdapter<T : Enum<T>>
internal constructor(
private val enumType: Class<T>,
private val fallbackValue: T?,
private val useFallbackValue: Boolean,
) : JsonAdapter<T?>() {

private val constants = enumType.enumConstants
private val nameStrings = Array(constants.size) { i ->
val constantName = constants[i].name
enumType.getField(constantName).jsonName(constantName)
}
private val nameStrings =
Array(constants.size) { i ->
val constantName = constants[i].name
enumType.getField(constantName).jsonName(constantName)
}
private val options = Options.of(*nameStrings)

/**
Expand All @@ -66,13 +67,11 @@ public class EnumJsonAdapter<T : Enum<T>> internal constructor(
if (!useFallbackValue) {
val name = reader.nextString()
throw JsonDataException(
"Expected one of ${nameStrings.toList()} but was $name at path ${reader.path}",
"Expected one of ${nameStrings.toList()} but was $name at path ${reader.path}"
)
}
if (reader.peek() != STRING) {
throw JsonDataException(
"Expected a string but was ${reader.peek()} at path ${reader.path}",
)
throw JsonDataException("Expected a string but was ${reader.peek()} at path ${reader.path}")
}
reader.skipValue()
return fallbackValue
Expand All @@ -81,9 +80,7 @@ public class EnumJsonAdapter<T : Enum<T>> internal constructor(
@Throws(IOException::class)
override fun toJson(writer: JsonWriter, value: T?) {
if (value == null) {
throw NullPointerException(
"value was null! Wrap in .nullSafe() to write nullable values.",
)
throw NullPointerException("value was null! Wrap in .nullSafe() to write nullable values.")
}
writer.value(nameStrings[value.ordinal])
}
Expand Down
Loading
Loading