Search code examples
kotlinjacksonyamljackson-databind

Parsing of YAML file with Jackson fails if value starts with special character


I am trying to parse the following YAML-content:

template:
    amplitude: 5
    noise: +-0.01  

This fails with this exception:

com.fasterxml.jackson.databind.JsonMappingException: empty String (through reference chain: ... ->my.package.Template["noise"])

The class Template contains a property noise of type FlexValue. I made a custom JsonDeserializer that is responsible to parse the values for properties of this type. The notation allows somewhat fancy expressions, hence the name FlexValue for the type. The value in this example, "+-0.01", is a valid input for the deserializer. However, it seems like the deserializer isn't even called. The exception seems to be thrown before that.

Here is the relevant class definition of my custom deserializer:

class FlexValueParser : JsonDeserializer<FlexValue>() {

    override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): FlexValue {
        //...
    }

}

The custom deserializer is registered as follows:

@JsonDeserialize(using = FlexValueParser::class)
class FlexValue {
    // ...
}

This setup seems to work fine. It does on other types as well, which are parsed differently.

If I prepend a "0" before "+-", which is also a valid input, it works:

template:
    amplitude: 5
    noise: 0+-0.01

Is "+-" a special cahracter in YAML that causes Jackson to fail parsing it, because it tries to do something else than I expect? I expect it to treat the input "+-0.01" as a String because it doesn't represent a valid number input.

The same problem occurs, when I use noise: ~0.01, which is also a valid input for my deserializer. The same exception is thrown on this and can be avoided by prepending a "0" before the curly "~".

Can someone give me a hint on this?


Solution

  • Found the source of the problem, which had nothing to do with Jackson or YAML. Closing this question therefore.