Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonDecodingException: Unexpected JSON token at offset #1545

Closed
Jawman5070 opened this issue Jun 10, 2021 · 3 comments
Closed

JsonDecodingException: Unexpected JSON token at offset #1545

Jawman5070 opened this issue Jun 10, 2021 · 3 comments

Comments

@Jawman5070
Copy link

Jawman5070 commented Jun 10, 2021

Describe the bug
Greetings! And hope you are all doing well.

I have been using the kotlinx.serialization library for about 2 months now, without any issues, and then saw an unexpected crash as follows:

E/AndroidRuntime: FATAL EXCEPTION: Thread-63
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 90953: Unexpected symbol '-' in numeric literal
JSON input: .....,"z":3.0542363E-4,"timestamp":1623-0.17467605,"y":8.477471,"...

Acceleration Events from the Device sensors are encoded to JSON, written to a .dat file, and later decoded.

While the error states the issue is with decoding, I believe the actual issue has to do with the encoding. The class being serialized is shown below.

AccelerationEvent

And example file after encoding and being written would look something like this.

List

However, I am getting files that just have random bits just straight-up missing. Which is why the file cannot be decoded afterwards.

Instead of an event being something like:
{"x":-3.0542363E-4,"y":2.2906772E-4,"z":1.5271181E-4,"timestamp":1623311424262464}
I get this:
{"x":-3.0542363E-4,"y":2.2906772E-4,"z":1.tamp":1623311424262464}
Where literally 18-20 characters are just gone. I thought it was a fluke, but this seems to happen 1 out of 10,000 events.

To Reproduce

Here is the code I use to encode the data.

/** Parses the list of Events into a JSON string and writes to file. **/
fun writeAccelerationsToFile(dataList: List<AccelerationEvent>) {
    Thread {
        try {
            val date = Date().time
            val titleString = "${id}_$date"
            val dataFile = File(application.applicationContext.getExternalFilesDir("accelerations"), "$titleString.dat")
            val fileOutputStream = FileOutputStream(dataFile)
            val objectOutputStream = ObjectOutputStream(fileOutputStream)
            val jsonList = Json.encodeToString(dataList)
            objectOutputStream.writeObject(jsonList)
            objectOutputStream.flush()
            objectOutputStream.close()
            fileOutputStream.flush()
            fileOutputStream.close()
        } catch (e: Exception) {
            Timber.e("Error writing accelerations to file.")
        }
    }.start()
}

Expected behavior
Should take a list of serializable events, encode, and write to file to be decoded at a later time.

Environment

  • Kotlin version: 1.5.10

  • Library version: implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.0")

  • Kotlin platforms: Native

  • Gradle version: classpath 'com.android.tools.build:gradle:4.2.1'

  • IDE version Android Studio 4.2.1
    Runtime version: 11.0.8+10-b944.6842174 amd64
    VM: OpenJDK 64-Bit Server VM by N/A
    Windows 10 10.0

  • Other relevant context [e.g. OS version, JRE version, ... ]

@shanshin
Copy link
Contributor

Is this bug reproduced in library version 1.2.1?

@Jawman5070
Copy link
Author

Updated to 1.21 last night right after making the post, and ran tests all last night. So far no issues with approximately 20,000 events. A co-worker suggested the possibility of the scientific notation being the problem, but I will keep testing and if I can't reproduce today, I will get you an update!

@Jawman5070
Copy link
Author

Just checked the 1.2.1 release notes and saw the Thread safety broken in 1.2.0 (Kotlin 1.5) #1455 resolution. I believe this matches my issue as we do use a lot of multi-threading for this specific application. It looks like the issue has already been addressed. Sorry to bug ya'll with an issue that's already been resolved and thank you for the great library! Keep up the awesome work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants