All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.funnysaltyfish.partialjsonparser.PartialJsonParser.kt Maven / Gradle / Ivy

Go to download

Parse incomplete JSON (like what ChatGPT generates in stream mode) in Kotlin, obtain as much as possible information fastly.

There is a newer version: 1.0.2
Show newest version
package com.funnysaltyfish.partialjsonparser

import com.funnysaltyfish.partialjsonparser.parser.Parser
import com.funnysaltyfish.partialjsonparser.tokenizer.Tokenizer

object PartialJsonParser {
    /**
     * Parse an incomplete JSON directly, the output should either be [String], [Boolean], [Number], `null`, [List] or [Map]
     *
     * Some examples:
     * ```plain
     * {"key"                    -> {}
     * {"key":                   -> {}
     * {"key":"te                -> {key=te}
     * {"key":"text", "key2      -> {key=text}
     * {"key":"text", "key2":"t} -> {key=text, key2=t}}
     * ```
     *
     * Adapted from [Here](https://github.com/SimonTart/json-fragment-parser) by GitHub Copilot
     *
     * @param str incomplete JSON string
     * @return parsed result, normally a [Map] (`{...}`) or [List] (`[...]`)
     * @throws SyntaxException
     * @throws JsonParseException
     */
    fun parse(str: String): Any? {
        val tokens = Tokenizer.tokenize(str)
        return Parser.parseTokens(tokens)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy