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

Include failed content when decoding #197

Open
hfhbd opened this issue Feb 13, 2024 · 3 comments
Open

Include failed content when decoding #197

hfhbd opened this issue Feb 13, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@hfhbd
Copy link

hfhbd commented Feb 13, 2024

Like kotlinx.serialization.json, it would be helpful to include the content when decoding, ideally the attribute/element (including its namespace) that failed.

Given class:

@Serializable
data class A(val a: String, val c: Int)

Using json Json.decodeFromString(A.serializer(), """{"a": "b", "d": 42}"""):

kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 13: Encountered an unknown key 'd' at path: $.a
Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys.
JSON input: {"a":  "b", "d":  42}

Using xml XML.decodeFromString(A.serializer(), """<A a="asd" d="asfd"/>"""):

nl.adaptivity.xmlutil.serialization.UnknownXmlFieldException: Could not find a field for name (ATest.A) A/d (Attribute)
  candidates: a (Attribute), c (Attribute) at position Line number = 1
Column number = 22
System Id = null
Public Id = null
Location Uri= null
CharacterOffset = 21
@pdvrieze
Copy link
Owner

I may add support for this, but it can't be default behaviour in all cases as it requires having access to the original string (you don't want to do this on streaming). Note that it already provides line/character number as well as the context (type, parentTagName/unknownName) and the expected names (and what type they are). It does provide namespaces, but hides empty namespaces to keep the output compact.

@pdvrieze pdvrieze added the enhancement New feature or request label Feb 13, 2024
@hfhbd
Copy link
Author

hfhbd commented Feb 13, 2024

Yes, I don't expect the full input string, but the failed element only, which is already needed by the parser/decoder, isn't it? With large inputs, the full string is also not really helpful too.

AFAIK the json deserializer also only contains the current item (and the next x characters).

@pdvrieze
Copy link
Owner

@hfhbd The problem is that for element (rather than attribute) children the "current" element is the child, rather than the container/parent. And the place in the code where this triggers is used to resolve both tag and attribute names.

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

No branches or pull requests

2 participants