This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val parsed = klx.parseJsonObject(StringReader(f)) | |
val timeSeries = parsed.filter { | |
it.key == dataKey | |
}.map { | |
it.value as JsonObject | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
timeSeries.first().values.forEach { | |
val day = klx.parseFromJsonObject<Daily>(it as JsonObject) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun waitForCount() = runBlocking { | |
val count = async { | |
parseDaily() | |
} | |
count.await() | |
} | |
suspend fun parseDaily(): Int | |
{ | |
val dataKey = "Time Series (Daily)" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 + ".") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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. |
OlderNewer