vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.7k stars 2.16k forks source link

json2: encode array of arrays, bad encoding 1.0 -> should be 1 #22363

Open esquerbatua opened 20 hours ago

esquerbatua commented 20 hours ago

Describe the bug

As json, in x.json2, we need the same functionality, encoding arrays of arrays it's not working as spected

Reproduction Steps

module main

import json
import x.json2

pub struct Data {
    name string
    data [][]f64
}

fn main() {
    data := Data{"test", [[1.0,2,3],[4.0,5,6]]}

    info_encoded := json.encode(data)
    println(info_encoded)
    info2_encoded := json2.encode(data)
    println(info2_encoded)
}

Expected Behavior

{"name":"test","data":[[1,2,3],[4,5,6]]}
{"name":"test","data":[[1,2,3],[4,5,6]]}

Current Behavior

{"name":"test","data":[[1,2,3],[4,5,6]]}
{"name":"test","data":[[1.0,2.0,3.0],[4.0,5.0,6.0]]}

Possible Solution

No response

Additional Information/Context

No response

V version

V full version: V 0.4.7 372a402.2db53a4

Environment details (OS name and version, etc.)

V full version: V 0.4.7 372a402.2db53a4 OS: linux, Ubuntu 24.04.1 LTS Processor: 16 cpus, 64bit, little endian, AMD Ryzen 7 7840HS w/ Radeon 780M Graphics

vexe: /home/esquerbatua/git/v/v vexe mtime: 2024-09-27 21:16:15

vroot: OK, value: /home/esquerbatua/git/v VMODULES: OK, value: /home/esquerbatua/.vmodules VTMP: OK, value: /tmp/v_1000

Git version: git version 2.43.0 Git vroot status: weekly.2024.39-30-g2db53a4b (20 commit(s) behind V master) .git/config present: true

CC version: cc (Ubuntu 13.2.0-23ubuntu4) 13.2.0 thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

[!NOTE] You can use the šŸ‘ reaction to increase the issue's priority for developers.

Please note that only the šŸ‘ reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

esquerbatua commented 20 hours ago

Bad example, it is working, but not in the cleanest way possible: ex: 1.0 -> 1

felipensp commented 9 hours ago

JSON does not have distinct types for integers and floating-point values. Therefore, the presence or absence of a decimal point is not enough to distinguish between integers and non-integers. For example, 1 and 1.0 are two ways to represent the same value in JSON.

This is the reason for this behavior from cJSON library. To make a distinct about it, we need to patch cJSON code.

esquerbatua commented 9 hours ago

JSON does not have distinct types for integers and floating-point values. Therefore, the presence or absence of a decimal point is not enough to distinguish between integers and non-integers. For example, 1 and 1.0 are two ways to represent the same value in JSON.

This is the reason for this behavior from cJSON library. To make a distinct about it, we need to patch cJSON code.

With cJSON it seems that it's working correctly, the error it's in x.json2

esquerbatua commented 8 hours ago

Tiny example of error:

import json
import x.json2

pub struct Data {
    float64 f64
}

fn main() {
    data := Data{1.0}

    info_encoded := json.encode(data)
    println(info_encoded)
    info2_encoded := json2.encode(data)
    println(info2_encoded)
    assert info2_encoded == '{"float64":1}'
}