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.68k stars 2.15k forks source link

json: not parsing embedded struct #22255

Open esquerbatua opened 1 day ago

esquerbatua commented 1 day ago

Describe the bug

Try to parse a json to a struct with another embedded.

Reproduction Steps

module main

import json

struct Json2 {
    inner []f64
}

struct Json {
    Json2
    test f64
}

fn main() {
    str:= '{
        "inner": [1, 2, 3, 4, 5],
        "test": 1.0
    }'
    data := json.decode(Json, str) or {
        eprintln('Failed to decode json, error: ${err}')
        return
    }
    println(data)
    assert data.inner.len == 5
    assert data.inner[0] == 1.0
    assert data.inner[4] == 5.0
    assert data.test == 1.0
}

Expected Behavior

Run correctly, with all of the data parsed

Current Behavior

Json{
    Json2: Json2{
        inner: []
    }
    test: 1.0
}
break.v:24: FAIL: fn main.main: assert data.inner.len == 5
   left value: data.inner.len = 0
  right value: 5
V panic: Assertion failed...
v hash: 5f6015f
/tmp/v_1000/break.01J83016DQE40509DMP512ZNQ5.tmp.c:7493: at _v_panic: Backtrace
/tmp/v_1000/break.01J83016DQE40509DMP512ZNQ5.tmp.c:13964: by main__main
/tmp/v_1000/break.01J83016DQE40509DMP512ZNQ5.tmp.c:14058: by main

Possible Solution

No response

Additional Information/Context

No response

V version

V full version: V 0.4.7 01fd719.263ba23

Environment details (OS name and version, etc.)

V full version: V 0.4.7 01fd719.263ba23 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-18 16:39:38

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.37-25-g263ba232 .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 1 day ago

This works if you create a field in the json with the name of the embedded element

module main

import json

struct Json2 {
    inner []f64
}

struct Json {
    Json2
    test f64
}

fn main() {
    str:= '{
        "Json2": {
            "inner": [1.0, 2.0, 3.0, 4.0, 5.0]
        },
        "test": 1.0
    }'
    data := json.decode(Json, str) or {
        eprintln('Failed to decode json, error: ${err}')
        return
    }
    println(data)
    assert data.inner.len == 5
    assert data.inner[0] == 1.0
    assert data.inner[4] == 5.0
    assert data.test == 1.0
}
zhwei820 commented 41 minutes ago

kind of different with in GO