From c01bad21bf410639ea7f3a73b18dd23344a26a2a Mon Sep 17 00:00:00 2001 From: AJ Alt Date: Thu, 14 Sep 2023 18:05:21 +0000 Subject: [PATCH] Fix string quoting in json sample (#456) --- samples/json/README.md | 3 +- .../clikt/samples/json/JsonValueSource.kt | 28 ++++++++++++------- .../github/ajalt/clikt/samples/json/main.kt | 2 +- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/samples/json/README.md b/samples/json/README.md index d5e1fe764..270d56680 100644 --- a/samples/json/README.md +++ b/samples/json/README.md @@ -1,6 +1,7 @@ # JSON Configuration files -This example shows how to read option values from JSON files using the kotlinx.serialization library. +This example shows how to read option values from JSON files using the `kotlinx.serialization` +library. It reads config files from the `config.json` file located in this directory. ``` ./runsample json subcommand diff --git a/samples/json/src/main/kotlin/com/github/ajalt/clikt/samples/json/JsonValueSource.kt b/samples/json/src/main/kotlin/com/github/ajalt/clikt/samples/json/JsonValueSource.kt index 824e2a8bf..412be16cb 100644 --- a/samples/json/src/main/kotlin/com/github/ajalt/clikt/samples/json/JsonValueSource.kt +++ b/samples/json/src/main/kotlin/com/github/ajalt/clikt/samples/json/JsonValueSource.kt @@ -5,10 +5,7 @@ import com.github.ajalt.clikt.core.InvalidFileFormat import com.github.ajalt.clikt.parameters.options.Option import com.github.ajalt.clikt.sources.ValueSource import kotlinx.serialization.SerializationException -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonArray -import kotlinx.serialization.json.JsonElement -import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.* import java.io.File /** @@ -27,10 +24,17 @@ class JsonValueSource( } if (cursor == null) return emptyList() - // This implementation interprets a list as multiple invocations, but you could also - // implement it as a single invocation with multiple values. - if (cursor is JsonArray) return cursor.map { ValueSource.Invocation.value(it) } - return ValueSource.Invocation.just(cursor) + try { + // This implementation interprets a list as multiple invocations, but you could also + // implement it as a single invocation with multiple values. + if (cursor is JsonArray) return cursor.map { + ValueSource.Invocation.value(it.jsonPrimitive.content) + } + return ValueSource.Invocation.just(cursor.jsonPrimitive.content) + } catch (e: IllegalArgumentException) { + // This implementation skips invalid values, but you could handle them differently. + return emptyList() + } } companion object { @@ -41,12 +45,16 @@ class JsonValueSource( Json.parseToJsonElement(file.readText()) as? JsonObject ?: throw InvalidFileFormat(file.path, "object expected", 1) } catch (e: SerializationException) { - if (requireValid) throw InvalidFileFormat(file.name, e.message ?: "could not read file") + if (requireValid) { + throw InvalidFileFormat(file.name, e.message ?: "could not read file") + } JsonObject(emptyMap()) } return JsonValueSource(json) } - fun from(file: String, requireValid: Boolean = false): JsonValueSource = from(File(file), requireValid) + fun from(file: String, requireValid: Boolean = false): JsonValueSource { + return from(File(file), requireValid) + } } } diff --git a/samples/json/src/main/kotlin/com/github/ajalt/clikt/samples/json/main.kt b/samples/json/src/main/kotlin/com/github/ajalt/clikt/samples/json/main.kt index 9f7a45b8a..1f928c81b 100644 --- a/samples/json/src/main/kotlin/com/github/ajalt/clikt/samples/json/main.kt +++ b/samples/json/src/main/kotlin/com/github/ajalt/clikt/samples/json/main.kt @@ -20,7 +20,7 @@ class Cli : CliktCommand(help = "An example using json files for configuration v init { context { valueSources( - JsonValueSource.from(System.getProperty("user.dir") + "config.json"), + JsonValueSource.from(System.getProperty("user.dir") + "/config.json"), JsonValueSource.from(System.getProperty("user.dir") + "/samples/json/config.json") ) }