0

Per the YAML spec, Not-a-number is represented as .NAN https://yaml.org/refcard.html

However, when deserialized with jackson-dataformats-text's YAMLMapper, we get:

Malformed numeric value '.NAN'

How do you tell an ObjectMapper to accept a given string as a representing null without diving into custom deserializers? Or is there a YAML-specific feature I should be enabling?

If you use NULL, it works on the Jackson side, but then it's no longer valid YAML and schema-aware editors like VS Code know it, which is confusing to end users:

VS code failing to validate null

J. Dimeo
  • 262
  • 2
  • 10
  • Is the issue here that it's expecting an integer, but NaN isn't a thing with integers? Is it possible just to omit it? – Andy Turner Oct 25 '21 at 13:31
  • Yes, @AndyTurner - it's expecting a numeric type. I can't omit it because a) this is kinda a DSL and I want users to be allowed to be explicit vs. implicit and b) in some cases I need the key to be defined even if the value is missing – J. Dimeo Oct 25 '21 at 13:44

1 Answers1

0

I've solved this by bypassing the YAMLMapper and going to SnakeYAML's Yaml directly. I did this to fix the issue of Jackson not resolving anchors, but then I realized it also solved the .NaN/null problem as well- .NaNs are now parsed correctly as Double.NaN

static final Yaml YAML = new Yaml();
static final ObjectMapper MAPPER = new ObjectMapper();
...

// Use SnakeYAML directly to resolve anchors and handle .NaN
protected static <T> T load(Path p, Class<T> cls) throws IOException {
    val yaml = YAML.load(Files.newInputStream(p));
    return MAPPER.treeToValue(MAPPER.valueToTree(yaml), cls);
}
J. Dimeo
  • 262
  • 2
  • 10