Skip to content

Is there a way to omit specially formatted keys in strict mode deserialization? #595

Closed Answered by xpathexception
xpathexception asked this question in Q&A
Discussion options

You must be logged in to vote

@charleskorn thanks for your answer, I appreciate that!

I've found a simple workaround that works for me. Not that effective, but better than nothing.

Since parsing and deserialization can be done separately, we can simply omit unneeded keys after parsing and before deserealization like this:

val parsed = Yaml.default.parseToYamlNode(yaml).dropKeys { it.content.startsWith("-") }
val result = parsed.decode<Root>()

With a simple function like this:

fun YamlNode.dropKeys(
    predicate: (YamlScalar) -> Boolean,
): YamlNode = when (this) {
    is YamlMap -> copy(
        entries = entries.filterKeys {
            !predicate(it)
        }.mapValues {
            it.value.dropKeys(predicate)
  …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by xpathexception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants