-
Notifications
You must be signed in to change notification settings - Fork 2
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
Allow working with JSON5 without @Serializable
classes
#2
Comments
I've thought about this more and I think option 2 (re-using existing |
I agree, this is definitely an important feature. I am still undecided about the two options, though. On the one hand, a forced dependency on the JSON format will be undesired noise for most application scenarios of the library and should therefore be avoided. On the other hand, returning a The ideal solution would probably be a format-independent version of Let me think about it while I work on the Multiplatform version! 🙂 |
There have been a few relevant updates from KxS JSON that I think are worth considering when implementing an equivalent in json5k. I'm mentioning them because they might help guide development in json5k.
ProposalWith the previous points in mind, I'd propose the following types: sealed interface Json5Element
//region Json5 primitives
sealed interface Json5Primitive : Json5Element
/** internal primitive implementation, containing metadata about the content and how to encode/decode it */
internal class Json5Literal : Json5Primitive()
object Json5Null : Json5Primitive
//endregion
//region primitive constructors
fun Json5Primitive(value: String): Json5Primitive = // ...
fun Json5Primitive(value: Int): Json5Primitive = // ...
fun Json5Primitive(value: Boolean): Json5Primitive = // ...
// ...
fun Json5Primitive(value: Nothing?): Json5Null = Json5Null
//endregion
//region Json5 structures
interface Json5Object : Json5Element, Map<String, Json5Element>
interface MutableJson5Object : Json5Object, MutableMap<String, Json5Element>
interface Json5Array : Json5Element, List<Json5Element>
interface MutableJson5Array : Json5Array, MutableList<Json5Element>
//endregion Notessealed interface vs classI used SerializersI didn't include the No nullable Json5Primitive parametersThe Json5Primitive function parameters aren't nullable, instead there's a Mutable type hierarchyMutableJson5Object implements Json5Object, to match the List/MutableList pattern in Kotlin stdlib (and the same for Json5Array) Raw encodingRaw encoding is not critical for an initial implementation, but the function would look like this: fun Json5UnquotedLiteral(value: String): Json5Element = // ... Builder functionsBecause there's a mutable variants of object and array, the builder functions would be simpler than KxS JSON. fun buildJson5Object(build: MutableJson5Object.() -> Unit): Json5Object {
// ...
}
fun buildJson5Array(build: MutableJson5Array.() -> Unit): Json5Array {
// ...
} CommentsSomething that JSON doesn't have is comments. How should json5k handle them? Perhaps this can be handled with a specific Or maybe comments need to be a specific subtype? public interface Json5Comment : Json5Element |
I would like to be able to encode or decode JSON5 without using
@Serializable
classes.Context
This is useful for tweaking JSON5 elements during serialization (for example, in a custom
KSerializer
), or for use in contexts where the Kotlinx Serialization plugin isn't available (Gradle config, .main.kts scripts)KxS
JsonElement
Kotlinx Serialization already provides
JsonElement
for this purpose.JsonElement
exampleOptions
I think there are two options
JsonElement
code, but for JSON5. This is mostly an exercise in renaming!JsonElement
code. Since JSON and JSON5 are so similar, I suspect this is the easiest approach.The text was updated successfully, but these errors were encountered: