Skip to content

Commit 759e3e4

Browse files
committed
Create json parsing using moshi
1 parent dceb9e4 commit 759e3e4

File tree

6 files changed

+2741
-1
lines changed

6 files changed

+2741
-1
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ agpVersion = "8.8.0"
44

55
compileSdk = "34"
66
minSdk = "24"
7+
moshiKotlinVersion = "1.15.2"
78
targetSdk = "33"
89

910
jdk-target = "1.8"
@@ -231,6 +232,7 @@ mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version.ref = "
231232

232233
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
233234

235+
moshi-kotlin = { module = "com.squareup.moshi:moshi-kotlin", version.ref = "moshiKotlinVersion" }
234236
reactivestreams = "org.reactivestreams:reactive-streams:1.0.4"
235237

236238
robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }

workflow-trace-viewer/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ kotlin {
2323
implementation(compose.desktop.currentOs)
2424
implementation(libs.kotlinx.coroutines.swing)
2525
implementation(compose.materialIconsExtended)
26+
implementation(libs.moshi.kotlin)
2627
}
2728
}
2829
}

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/App.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@ import androidx.compose.runtime.Composable
55

66
@Composable
77
fun App() {
8-
Text("Hello world!")
8+
val jsonString = object {}.javaClass.getResource("/workflow-simple.json")?.readText()
9+
val root = jsonString?.let { FetchRoot(it) }
10+
println(root)
11+
if (root != null) {
12+
DrawWorkflowTree(root)
13+
} else {
14+
Text("Empty data or failed to parse data") // TODO: proper handling of error
15+
}
916
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.squareup.workflow1.traceviewer
2+
3+
import com.squareup.moshi.JsonDataException
4+
import com.squareup.moshi.Moshi
5+
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
6+
import java.io.IOException
7+
8+
/**
9+
* Parses a JSON string into [WorkflowNode] with Moshi adapters
10+
* All the caught exceptions should be handled by the caller, and appropriate UI feedback should be provided to user
11+
*
12+
*/
13+
public fun FetchRoot(json: String): WorkflowNode? {
14+
return try{
15+
val moshi = Moshi.Builder()
16+
.add(KotlinJsonAdapterFactory())
17+
.build()
18+
val workflowAdapter = moshi.adapter(WorkflowNode::class.java)
19+
val root = workflowAdapter.fromJson(json)
20+
root
21+
} catch (e: JsonDataException) {
22+
throw JsonDataException("Failed to parse JSON: ${e.message}", e)
23+
} catch (e: IOException){
24+
throw IOException("Malformed JSON: ${e.message}", e)
25+
}
26+
}
27+

0 commit comments

Comments
 (0)