this is my project structure, I want to parse json...
# multiplatform
s
this is my project structure, I want to parse json to kotlin object, and use it in ios side as well, who can help me? I need to:
get file path
decode it kotlin data type
use inside iosApp
question: do I need to do anything with expect/actual?
l
You can use okio to read the file, and kotlinx.serialization to parse the json. Both are multiplatform.
If the file is using the compose resources plugin, I believe there's a method to get the contents of a resource as a byte array, which you can convert to a String, but I don't remember the method name off-hand.
s
my main problem is getting the file path, to get the json from file
a
Are you using the latest version of CMP? I'm reading a txt file that is located in the
composeResources
folder with:
Copy code
@OptIn(InternalResourceApi::class)
    private suspend fun getWordsFromLocal(board: List<String>): List<String> {
        val dictionary = readResourceBytes(boardDictionary).decodeToString().split("\r?\n|\r".toRegex()).toList()
Can't adjust code, I'm on cellphone 😅 File is located in
Copy code
private var boardDictionary = "files/en_dictionary.txt"
j
If using kotlinx serialization dont need the file path. Depending how you use it ofc. Like this: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/decode-from-byte-array.html
s
I decode from local json file
j
Yes that should work