JSYAML custom schema for INT

13 Views Asked by At

I would like to write a custom formatting schema for INT for js-yaml library.

https://www.npmjs.com/package/js-yaml

The problem I encountered during the development of the application is that in the input data, a number with a decimal part ".0", is convertet to Number (by JS) and then treated by the library as INT. I would like values of type INT to always have a decimal part such as "1.0", "0.0"...

I am using Vue2 (2.7.x) with the js-yaml library (4.1.x).

Below I attach an example dataset with the expected result.

Vue code:

`      
      const data= this.getData();
      const yaml = YAMLJS.dump(data, {
        noCompatMode: true,
        quotingType: '"',
      });
     

return yaml.toString(); ` Example datasets:

**This is the data from API**

`"myData": {
        "name": "myName",
        "pose": {
            "from": "source",
            "position": {
                "x": 0.0,
                "y": 1.0,
                "z": 2.2
            },
            "orientation": {
                "x": 0.0,
                "y": 1.1,
                "z": 2.0,
                "w": 3.0
            }
        },
}`

**This is the input to the dump function**

`"myData": {
        "name": "myName",
        "pose": {
            "from": "source",
            "position": {
                "x": 0,
                "y": 1,
                "z": 2.2
            },
            "orientation": {
                "x": 0,
                "y": 1.1,
                "z": 2,
                "w": 3
            }
        },
}`

`
**Expected YAML output**
...
    pose:
      from: source
      position:
        x: 0.0
        y: 1.0
        z: 2.2
      orientation:
        x: 0.0
        y: 1.1
        z: 2.0
        w: 3.0
...`
**Acctual YAML output**

`...
    pose:
      from: source
      position:
        x: 0
        y: 1
        z: 2.2
      orientation:
        x: 0
        y: 1.1
        z: 2
        w: 3
...`

I tried solutions from GPT chat and other sources. Maybe I missed something.

0

There are 0 best solutions below