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

Add way to log unknown keys when ignoreUnknownKeys is enabled #2698

Open
paulhobbel opened this issue Jun 3, 2024 · 3 comments
Open

Add way to log unknown keys when ignoreUnknownKeys is enabled #2698

paulhobbel opened this issue Jun 3, 2024 · 3 comments
Labels

Comments

@paulhobbel
Copy link

What is your use-case and why do you need this feature?
We are building a platform that implements OCPI (a protocol that is used in the EV industry). Even though OCPI is a "standard", companies tend to add extra information to messages.

Because of this we have ignoreUnknownKeys set to true. We would still like to know which keys had been ignored so we can consider whether we should add these in the future as well. We used Jackson before and in Jackson there was a DeserializationProblemHandler that you could register as module.

The solution we have in place as of right now is extending the StringFormat and have a custom try-catch clause around decodeFromString and the ignoreUnknownKeys set to false.

Describe the solution you'd like
Provide a way to configure some sort of problem handler, this could already be in the form of a lambda function you pass to the Json builder.

Someting as follows maybe?

fun reportOnUnknownKey(key: string, descriptor: SerialDescriptor) {
   logger.warn("Unknown key '$key' in ${descriptor.serialName}"
}

val jsonFormat =
    Json {
        ignoreUnknownKeys = true
        explicitNulls = false
        onUnknownKey = reportOnUnknownKey
    }
@pdvrieze
Copy link
Contributor

pdvrieze commented Jun 3, 2024

My Xml format supports this, but I found that using custom types for handling this does not scale well. Instead it has a policy parameter where (various) behavioural functions are provided. In the case of Json the functions that come to mind are handling unknown keys. Handling "invalid" values (e.g. strings instead of Int). The algorithm that determines the key name (for example converting camel case to snake case).

@sandwwraith
Copy link
Member

Can't this be covered by #1978? By using additional map in the object itself, you can still log them when map is non-empty

@pdvrieze
Copy link
Contributor

The xml use case for the policy function is mainly to support error correction (including the various "almost xml" variants out there that real world code has no choice but to deal with). The ability to log the key is a side effect. I don't think that json really has such a big problem with key mismatch (no namespaces to get wrong, etc.)

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

No branches or pull requests

3 participants