0

I'm trying to parce my collocation.json file from asset file in kotlin. I wrote some codes for this. There is a json file named collocations.json in my own asset file and I want to parse it, but when I run the project, the following error appears. What is the reason of this ?

ERROR

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.enestigli.dictionaryapp, PID: 10313
    kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the object '{', but had 'EOF' instead
    JSON input: collocations.json
        at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)
        at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:32)
        at kotlinx.serialization.json.internal.AbstractJsonLexer.fail(AbstractJsonLexer.kt:524)
        at kotlinx.serialization.json.internal.AbstractJsonLexer.fail$kotlinx_serialization_json(AbstractJsonLexer.kt:221)
        at kotlinx.serialization.json.internal.AbstractJsonLexer.unexpectedToken(AbstractJsonLexer.kt:204)
        at kotlinx.serialization.json.internal.StringJsonLexer.consumeNextToken(StringJsonLexer.kt:74)
        at kotlinx.serialization.json.internal.StreamingJsonDecoder.beginStructure(StreamingJsonDecoder.kt:41)
        at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:29)
        at kotlinx.serialization.internal.AbstractCollectionSerializer.deserialize(CollectionSerializers.kt:43)
        at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59)
        at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:36)
        at kotlinx.serialization.json.Json.decodeFromString(Json.kt:100)
        at com.enestigli.dictionaryapp.presentation.CollocationScreen.CollocationScreenKt$gridView$2$1$1$1$1$1$1.invoke(CollocationScreen.kt:270)
        at com.enestigli.dictionaryapp.presentation.CollocationScreen.CollocationScreenKt$gridView$2$1$1$1$1$1$1.invoke(CollocationScreen.kt:148)
        at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke-k-4lQ0M(Clickable.kt:153)
        at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke(Clickable.kt:142)
        at androidx.compose.foundation.gestures.TapGestureDetectorKt$detectTapAndPress$2$1$1.invokeSuspend(TapGestureDetector.kt:220)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:178)
        at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:166)
        at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:397)
        at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:431)
        at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:420)
        at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:328)
        at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter$PointerEventHandlerCoroutine.offerPointerEvent(SuspendingPointerInputFilter.kt:560)
        at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.dispatchPointerEvent(SuspendingPointerInputFilter.kt:452)
        at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.onPointerEvent-H0pRuoY(SuspendingPointerInputFilter.kt:465)
        at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:310)
        at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:297)
        at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:297)
        at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:297)
        at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:297)
        at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:297)
        at androidx.compose.ui.input.pointer.NodeParent.dispatchMainEventPass(HitPathTracker.kt:179)
        at androidx.compose.ui.input.pointer.HitPathTracker.dispatchChanges(HitPathTracker.kt:98)
        at androidx.compose.ui.input.pointer.PointerInputEventProcessor.process-BIzXfog(PointerInputEventProcessor.kt:80)
        at androidx.compose.ui.platform.AndroidComposeView.sendMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1261)
E/AndroidRuntime:     at androidx.compose.ui.platform.AndroidComposeView.handleMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1211)
        at androidx.compose.ui.platform.AndroidComposeView.dispatchTouchEvent(AndroidComposeView.android.kt:1150)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3121)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2802)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3121)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2802)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3121)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2802)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3121)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2802)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:498)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1899)
        at android.app.Activity.dispatchTouchEvent(Activity.java:4262)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:456)
        at android.view.View.dispatchPointerEvent(View.java:15263)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6548)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:6348)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5804)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5861)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5827)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5992)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5835)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:6049)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5808)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5861)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5827)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5835)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5808)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8857)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8808)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8777)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8980)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:267)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:335)
        at android.os.Looper.loopOnce(Looper.java:161)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
        Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [androidx.compose.ui.platform.MotionDurationScaleImpl@d43928e, androidx.compose.runtime.BroadcastFrameClock@440daaf, StandaloneCoroutine{Cancelling}@4989ebc, AndroidUiDispatcher@6c10e45]

my collocations.json file in asset

{
    "A": [
        {
            "collocation": "above average",
            "meaning": "more than average, esp. in amount, age, height, weight etc. "
        },
        {
            "collocation": "absolutely necessary",
            "meaning": "totally or completely necessary"
        },
        {
            "collocation": "abuse drugs",
            "meaning": "to use drugs in a way that's harmful to yourself or others"
        },
        {
            "collocation": "abuse of power",
            "meaning": "the harmful or unethical use of power"
        }
    ],
    "B": [
        {
            "collocation": "back pay",
            "meaning": "money a worker earned in the past but hasn't been paid yet  "
        },
        {
            "collocation": "back road",
            "meaning": "a small country road "
        },
        {
            "collocation": "back street",
            "meaning": "a street in a town or city that's away from major roads or central areas"
        },
        {
            "collocation": "back taxes",
            "meaning": "taxes that weren't paid when they were due"
        },
        {
            "collocation": "bad breath",
            "meaning": "breath that doesn't smell nice"
        }],

this way it goes up to the letter W.

my data class

@Serializable
data class Collocation(
    val collocation: String,
    val meaning: String
)

I did something like this

fun loadCollocationJson(){

        val json = "collocations.json"
        val collocations = Json.decodeFromString<Map<String, List<Collocation>>>(json)

        val collocationsForA = collocations["A"]
        println("collocationsForA:"+ collocationsForA)
    }

I wanted to bring the data starting with the letter A to be an example here. But it gives the above error. What can be the problem.?

SOLVE

I changed

val json = "collocations.json"

with this

val jsonStr : String = context.assets.open("collocations.json").bufferedReader().use { it.readText() }
NewPartizal
  • 604
  • 4
  • 18
  • 1
    This is happening because decodeFromString takes a String that needs to be decoded and you're trying to give it a file. You first have to load your file into a String and then pass the String to the function. If you're working on a KMM project you'll have to either use some external dependency or write your own expect / actual functions to do the file reading part. – Antonio Gauta Nov 20 '22 at 20:42
  • oh I didnt know that sorry. I thought we were entering the name of the file as a string. Thank you for answer. it works fine right now. – NewPartizal Nov 21 '22 at 09:24

0 Answers0