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

Possible bad transcription of struct to JSON - indentation of member of type structure without given name #20600

Open VitSimon opened 8 months ago

VitSimon commented 8 months ago

Describe the bug

Hi,

I have tried code in Reproduction Steps and I think there is bad processing of anonymous members (composition) of type structure in structures. JSON text is providing another extra level for non named member of type struct.

In comparison to Golang there is not any extra level in this case.

If I need to have all values on same level, what I need to do more to reach it? Thanks for advice.

Reproduction Steps

struct Article {
    title string
    description string
}

struct ArticlePlus {
    Article
    perex string
}

@['/']
pub fn (mut app App) page_home() vweb.Result {
    list_of_object := [
        ArticlePlus{
            title: 'One good title'
            description: 'this is the first'
        },
        ArticlePlus{
            title: 'Other good title'
            description: 'more one'
        },
        ArticlePlus{
            title: 'Other good title'
            description: 'more one'
            perex: 'good perex'
        },
    ]
    return app.json(list_of_object)
}

with vweb module

and getting this structure in browser:

0    
    Article    
        title    "One good title"
        description    "this is the first"
    perex    ""
1    
    Article    
        title    "Other good title"
        description    "more one"
    perex    ""
2    
    Article    
        title    "Other good title"
        description    "more one"
    perex    "good perex"

raw text:

[{"Article":{"title":"One good title","description":"this is the first"},"perex":""},{"Article":{"title":"Other good title","description":"more one"},"perex":""},{"Article":{"title":"Other good title","description":"more one"},"perex":"good perex"}]

Does it is correct?

Expected Behavior

0
    title    "One good title"
    description    "this is the first"
    perex    ""
1    
    title    "Other good title"
    description    "more one"
    perex    ""
2    
    title    "Other good title"
    description    "more one"
    perex    "good perex"

raw text:

[{"title":"One good title","description":"this is the first","perex":""},{"title":"Other good title","description":"more one","perex":""},{"title":"Other good title","description":"more one","perex":"good perex"}]

Current Behavior

and getting this structure in browser:

0    
    Article    
        title    "One good title"
        description    "this is the first"
    perex    ""
1    
    Article    
        title    "Other good title"
        description    "more one"
    perex    ""
2    
    Article    
        title    "Other good title"
        description    "more one"
    perex    "good perex"

raw text:

[{"Article":{"title":"One good title","description":"this is the first"},"perex":""},{"Article":{"title":"Other good title","description":"more one"},"perex":""},{"Article":{"title":"Other good title","description":"more one"},"perex":"good perex"}]

Possible Solution

I am surprised by:

struct Article {
    title string
    description string
}

struct ArticlePlus {
    Article
    perex string
}

coding structure ArticlePlus to:

[{"Article":{"title":"One good title","description":"this is the first"},"perex":""},{"Article":{"title":"Other good title","description":"more one"},"perex":""},{"Article":{"title":"Other good title","description":"more one"},"perex":"good perex"}]

There is non named property so I thogut it should be fully embedded on same level as rest of struct members. Instead of it That member is named as same as given type and its data are inside another brackets. When I give a name to anonymous member, like:

struct ArticlePlus { articleData Article perex string }

then json is changed:

[{"articleData":{"title":"One good title","description":"this is the first"},"perex":""},{"articleData":{"title":"Other good title","description":"more one"},"perex":""},{"articleData":{"title":"Other good title","description":"more one"},"perex":"good perex"}]

When I compare it to golang, then there is no sub level for composition of structures. json

Additional Information/Context

No response

V version

0.4.4 629bae4, timestamp: 2024-01-20 12:15:58

Environment details (OS name and version, etc.)

Microsoft Windows [Version 10.0.19045.3930], x64

[!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.

VitSimon commented 8 months ago

I have altered examples\json.v locally to test it on smaller footprint. I can verify there is the same behavior so it is independent on vweb package.

VitSimon commented 7 months ago

Hello, does there is any update to this issue? Thanks for reply.

ttytm commented 5 months ago

Hey @VitSimon, thanks for reporting the issue! Can you provide a self-contained example that can be used to reproduce the issue? This would simplify looking into it and speed up a fix. For more complex issues, please provide a repo with the smallest sample that reproduces the bug.

VitSimon commented 5 months ago

@ttytm Hello, example is prepared here: https://github.com/VitSimon/IssueAttachments/tree/vlang-v-20600 (based on example file in vlang repository, only simply altered for my scenario) Produced output: [{"Address":{"addr":""},"name":"Frodo","age":25,"is_registered":true},{"Address":{"addr":""},"name":"Bobby","age":10,"is_registered":false}] Predicted output to be produced: [{"addr":"","name":"Frodo","age":25,"is_registered":true},{"addr":"","name":"Bobby","age":10,"is_registered":false}]