j-brooke / FracturedJsonJs

JSON formatter that produces highly readable but fairly compact output.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Float 0.0 converted to 0

masaccio opened this issue · comments

Using this script:

const { Formatter, EolStyle } = require("fracturedjsonjs");
const fs = require('fs');

const argv = process.argv.slice(1);

let rawdata = fs.readFileSync(argv[1])
let jsObj = JSON.parse(rawdata);

const formatter = new Formatter();

formatter.alwaysExpandDepth = 0
formatter.maxInlineLength = 10000
formatter.maxInlineComplexity = 100
formatter.maxCompactArrayComplexity = 100

const jsonString = formatter.serialize(jsObj);
console.log(jsonString);

and this JSON:

{
    "AttackPlans": [
        {
            "TeamId": 1,
            "Spawns": [
                {
                    "Time": 0.0,
                    "UnitType": "Grunt",
                    "SpawnPointIndex": 0
                },
                {
                    "Time": 0.0,
                    "UnitType": "Grunt",
                    "SpawnPointIndex": 0
                },
                {
                    "Time": 0.0,
                    "UnitType": "Grunt",
                    "SpawnPointIndex": 0
                }
            ]
        }
    ],
    "DefensePlans": [
        {
            "TeamId": 2,
            "Placements": [
                {
                    "UnitType": "Archer",
                    "Position": [
                        41,
                        7
                    ]
                },
                {
                    "UnitType": "Pikeman",
                    "Position": [
                        40,
                        7
                    ]
                },
                {
                    "UnitType": "Barricade",
                    "Position": [
                        39,
                        7
                    ]
                }
            ]
        }
    ]
}

I get the following output:

{
    "AttackPlans" : [ { "TeamId": 1, "Spawns": [ {"Time": 0, "UnitType": "Grunt", "SpawnPointIndex": 0}, {"Time": 0, "UnitType": "Grunt", "SpawnPointIndex": 0}, {"Time": 0, "UnitType": "Grunt", "SpawnPointIndex": 0} ] } ],
    "DefensePlans": [ { "TeamId": 2, "Placements": [ { "UnitType": "Archer", "Position": [41, 7] }, { "UnitType": "Pikeman", "Position": [40, 7] }, { "UnitType": "Barricade", "Position": [39, 7] } ] }                    ]
}

Note that the Time parameter has been converted from a float to an int (0.0 to 0). Basically I am unable to reproduce the output from https://github.com/j-brooke/FracturedJson/wiki/Options (and not in browser either).

Appreciate this is a python-ism so you may want to reject this based on https://www.json.org/json-en.html

This is essentially another form of j-brooke/FracturedJson#11 and j-brooke/FracturedJson#12 .

The issue is that the JS implementation relies on turning the input text directly into JavaScript objects/arrays/whatever before trying to process the output, so there's no way to preserve any existing formatting of the number (or, in fact, correctly preserve the value of the number if it requires more precision than allowed by a 64-bit float). As a contrast, the C# code uses the exact source text for the number if it decides not to reformat it.

This problem isn't fixable until the JS implementation is rewritten to do its own parsing of the JSON text. (I might do that some day - it would let me add a few more features people have asked for.)

As far as Python goes, if you're following the same approach as the JS code of just letting the built-in parser convert the input into native types, I suspect you're stuck with the same problem.

Yes - same approach of native types in the python implementation. I will see whether I prefer to treat int and float differently even though it's not overly JSON. It's a python parser after all. Thanks for the clarification.