Skip to content

Instantly share code, notes, and snippets.

val data1 = Klaxon().parse<String>(File(pathname).toString())
// This just tried to parse the filename.
val data2 = Klaxon().parse<File>(StringReader(File(pathname).toString())
// What's a Reader? I think that's what I need.
val data3 = Klaxon().parse<String>(File(pathname).readText())
// Oh, there's a readText(). Why didn't this work? It seems it's the wrong parse call evenough the types match.
val data4 = Klaxon().parseJsonObject(StringReader(File(pathname).readText()))
val parsed = klx.parseJsonObject(StringReader(f))
val timeSeries = parsed.filter {
it.key == dataKey
}.map {
it.value as JsonObject
}
timeSeries.first().values.forEach {
val day = klx.parseFromJsonObject<Daily>(it as JsonObject)
}
data class Daily
(
@Json(name = "1. open")
val open: String,
@Json(name = "2. high")
val high: String,
@Json(name = "3. low")
val low: String,
@Json(name = "4. close")
val close: String,
@Test fun testParseDaily1()
{
val cnt = 0
val dataKey = "Time Series (Daily)"
val klx = Klaxon()
val f = File(pathname).readText()
val parsed = klx.parseJsonObject(StringReader(f))
val timeSeries = parsed.filter {
it.key == dataKey
}.map {
fun waitForCount() = runBlocking {
val count = async {
parseDaily()
}
count.await()
}
suspend fun parseDaily(): Int
{
val dataKey = "Time Series (Daily)"
@Test fun testParseDaily2()
{
val expected = 13
val klx = Klaxon()
val dataKey = "Time Series (Daily)"
val days = klx.parseJsonObject(StringReader(File(pathname).readText()))
.filter { it.key == dataKey }
.map { it.value as JsonObject }.first().values
.map { klx.parseFromJsonObject<Daily>(it as JsonObject)?.close }
assertEquals(days.size, expected, "Bad count of " + days.size + ", expected " + expected + ".")
@d108
d108 / DataDecoder.swift
Last active June 3, 2023 06:26
Useful for decoding JSON data from a file by passing in a type.
/*
* SPDX-FileCopyrightText: © 2023 Daniel Zhang <https://github.com/d108/>
* SPDX-License-Identifier: MIT License
*/
import Foundation
struct DataDecoder
{
/// Generic decoder with a switch statement to handle specific types.