Not possible to render mapping with Int keys in YAML #262

Closed
opened 2025-12-30 01:22:56 +01:00 by adam · 5 comments
Owner

Originally created by @artemijan on GitHub (Jan 10, 2025).

ll = new Mapping<Int, String>{
  [1] = ""
}

when trying to render it, I get an error:

–– Pkl Error ––
Cannot render object with non-string key as XML.
Object: new Mapping { [1] = "" }
Key   : 1
Originally created by @artemijan on GitHub (Jan 10, 2025). ``` ll = new Mapping<Int, String>{ [1] = "" } ``` when trying to render it, I get an error: ``` –– Pkl Error –– Cannot render object with non-string key as XML. Object: new Mapping { [1] = "" } Key : 1 ```
adam closed this issue 2025-12-30 01:22:57 +01:00
Author
Owner

@StefMa commented on GitHub (Jan 10, 2025):

Seems it is not allowed to use numbers only in xml as a tag name.
See also https://www.w3.org/TR/xml/#NT-Name

However, you should be able to use _1 or a1... Which is, in return, in pkl a string 🙃

@StefMa commented on GitHub (Jan 10, 2025): Seems it is not allowed to use **numbers only** in xml as a tag name. See also https://www.w3.org/TR/xml/#NT-Name However, you should be able to use `_1` or `a1`... Which is, in return, in pkl a string 🙃
Author
Owner

@artemijan commented on GitHub (Jan 10, 2025):

I actually tried it also for a json and yaml. Output is the same. I need this for yaml file.

@artemijan commented on GitHub (Jan 10, 2025): I actually tried it also for a json and yaml. Output is the same. I need this for yaml file.
Author
Owner

@HT154 commented on GitHub (Jan 10, 2025):

JSON requires string keys. I was less sure about YAML (1.2) and looked up the spec: non-string keys (including seq or map) are explicitly allowed, but many YAML implementations do not support them.

This includes Pkl's currently, which I think should be considered a bug. This construct is legal in both Pkl and YAML, so the renderer should accept it (at least for scalar types).

If you can accept numeric string keys, as a workaround you may be able to handle this at render time with a converter, e.g.:

output {
  renderer = new YamlRenderer {
    converters {
      [Mapping] = (it) -> new Mapping {
        for (k, v in it) {
          [k.toString()] = v
        }
      }
    }
  }
}
@HT154 commented on GitHub (Jan 10, 2025): JSON _requires_ string keys. I was less sure about YAML (1.2) and looked up the spec: non-string keys (including seq or map) are explicitly allowed, but many YAML implementations do not support them. This includes Pkl's currently, which I think should be considered a bug. This construct is legal in both Pkl and YAML, so the renderer should accept it (at least for scalar types). If you can accept numeric string keys, as a workaround you may be able to handle this at render time with a converter, e.g.: ```pkl output { renderer = new YamlRenderer { converters { [Mapping] = (it) -> new Mapping { for (k, v in it) { [k.toString()] = v } } } } } ```
Author
Owner

@HT154 commented on GitHub (Jan 10, 2025):

Okay, PR open with the fix. You can also use render directives to get the desired output until this fix is released:

output {
  renderer = new YamlRenderer {
    converters {
      [Mapping] = (it) -> new Mapping {
        for (k, v in it) {
          [if (k is Number || k is Boolean || k is Null) new RenderDirective { text = k.toString() } else k] = v
        }
      }
    }
  }
}
@HT154 commented on GitHub (Jan 10, 2025): Okay, PR open with the fix. You can also use render directives to get the desired output until this fix is released: ```pkl output { renderer = new YamlRenderer { converters { [Mapping] = (it) -> new Mapping { for (k, v in it) { [if (k is Number || k is Boolean || k is Null) new RenderDirective { text = k.toString() } else k] = v } } } } } ```
Author
Owner

@bioball commented on GitHub (Jan 11, 2025):

Changing the title (this behavior is correct in XML and JSON).

@bioball commented on GitHub (Jan 11, 2025): Changing the title (this behavior is correct in XML and JSON).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#262