-
Notifications
You must be signed in to change notification settings - Fork 634
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
Design a general JsonElement-like thing? #1573
Comments
This one is similar: #222 |
#222 is more likely to request a SAX-style reader but I’m asking a generic DOM.
|
It is about a generic tree-like object with string keys. I actually use exactly that in DataForge. But it is tailored for my own needs. I use it for DOM as well and it could be encoded to Json or XML. |
My 5 cents. All formats listed here: YAML, Cbor, MessagePack are based on JSON data information model, so using a |
@elizarov What's more, the current architecture of |
@elizarov the primary difference between JSON and XML is the treatment of same-name-siblings. In Json it is array, in XML, element order matters. I agree that the model is different, but in theory, it is possible to make some kind of common tooling to work with any tree-like structure. |
We can design a |
In a concrete example: data class Request(val identity: GeneralElement, val arg: String)
data class Response(val identity: GeneralElement, val result: String)
fun handleRequest(request: Request): Response {
return Response(request.identity, "Hello, " + request.arg)
} |
As an incremental step it would be very helpful to break out the JsonElement classes and utility functions into a separate, independent, dependency. This would help json5k xn32/json5k#2 so it's possible to encode/decode to/from JsonElements, but does not require muddying the JSON5 code with KxS JSON serialization. |
@aSemy I doubt this is worth it or even possible, because JsonElements serializers heavily rely on functionality specific to Json format (see JsonEncoder and JsonDecoder) |
Currently the
JsonElement
is very specific toJson
, however the similar feature can be used in other formats likeCtor
,Yaml
,MessagePack
, etc.What is your use-case and why do you need this feature?
Similarly to
JsonElement
, sometimes we may need to process the complex struct manually.We may provide response for our api in multi formats (eg.
Json
for normal use &Ctor
for better performance), then we need a more general way to handlexxxElement
.For a detailed example, all Discord APIs support both
Json
andETF
encodings.Describe the solution you'd like
The fundamental
GeneralElement
should be designed asRawMessage
(inspired by Golang), which stores the values in a serializator-special binary format. This data may not have a clear TypeID, so we may not able to read or edit it without addtional information. But round-trip should be supported.RawMessage
is compatible toProtobuf
or other formats that do not have a clear runtime type info.Some formats like
Json
orCtor
provides detailed type info to help us parse the data, then we can parse them intoGeneralObjectElement
,GeneralArrayElement
orGeneralPrimitiveElement
, which generalizdJsonObject
,JsonArray
andJsonPrimitive
.The text was updated successfully, but these errors were encountered: