Skip to content

Define '[]map[string]?Any' when it comes to time? Any 'type cannot be converted to JSON type #24175

@CryingN

Description

@CryingN

Describe the bug

This is a rather bizarre issue. When I use the following code, if the User struct does not include a time.Time field, or if the field is defined as []map[string]Any instead of []map[string]?Any, it runs normally without any errors.

Reproduction Steps

import json
import time

type Any = string | []map[string]?Any | time.Time

struct Users {
mut:
    id         string
    created_at time.Time
}

data_all := [Users{
    id: '123'
    created_at : time.now()
}, Users{
    id: 'asd'
    created_at : time.now()
}]



mut result := []map[string]?Any{}
for raw in data_all {
    mut data := map[string]?Any{}
    data['id'] = raw.id
    data['time'] = raw.created_at
    result << data
}

println(result)

msg := json.encode(result)
println(msg)

Expected Behavior

like this:

[{'id': Any('123'), 'time': Any(2025-04-09 23:35:48)}, {'id': Any('asd'), 'time': Any(2025-04-09 23:35:48)}]
[{"id":"123","time":{"_type":"Time","value":1744241748}},{"id":"asd","time":{"_type":"Time","value":1744241748}}]

Current Behavior

[root_cn@archlinux json_null]$ v test_none.v -cg
/tmp/v_1000/test_none.01JRDKHSCE9ZJ2EKMDKRKYCFSX.tmp.c:2551: error: cannot convert 'struct main__Any' to 'struct _option_main__Any'
builder error:
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .
[root_cn@archlinux json_null]$ v test_none.v -show-c-output
======== Output of the C Compiler (/mnt/d/file/vlang/v/thirdparty/tcc/tcc.exe) ========
/tmp/v_1000/test_none.01JRDKJEY77SZTADGV5TDTXSPQ.tmp.c:2545: error: cannot convert 'struct main__Any' to 'struct _option_main__Any'
=======================================================================================
======== Output of the C Compiler (cc) ========
/tmp/v_1000/test_none.01JRDKJEY77SZTADGV5TDTXSPQ.tmp.c: In function ‘json__decode__option_main__Any’:
/tmp/v_1000/test_none.01JRDKJEY77SZTADGV5TDTXSPQ.tmp.c:2545:39: error: incompatible types when assigning to type ‘_option_main__Any’ from type ‘main__Any’
 2545 |                                 res = time__Time_to_sumtype_main__Any(&_t6);
      |                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
===============================================

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.10 55b16fb

Environment details (OS name and version, etc.)

in linux:

|V full version      |V 0.4.10 f7f89b3c3c6ce0fc71f8a1ca2f85e00f318a1ff6.55b16fb
|:-------------------|:-------------------
|OS                  |linux, Linux version 4.4.0-19041-Microsoft ([email protected]) (gcc version 5.4.0 (GCC) ) #4355-Microsoft Thu Apr 12 17:37:00 PST 2024 (VM) (WSL)
|Processor           |8 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i5-1130G7 @ 1.10GHz
|Memory              |7.19GB/15.79GB
|                    |
|V executable        |/mnt/d/file/vlang/v/v
|V last modified time|2025-04-09 15:43:13
|                    |
|V home dir          |OK, value: /mnt/d/file/vlang/v
|VMODULES            |OK, value: /home/root_cn/.vmodules
|VTMP                |OK, value: /tmp/v_1000
|Current working dir |OK, value: /mnt/d/file/vlang/test/json_null
|                    |
|Git version         |git version 2.49.0
|V git status        |weekly.2025.13-75-g55b16fbd
|.git/config present |true
|                    |
|cc version          |cc (GCC) 14.2.1 20250207
|gcc version         |gcc (GCC) 14.2.1 20250207
|clang version       |clang version 19.1.7
|tcc version         |tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
|tcc git status      |thirdparty-linux-amd64 696c1d84
|emcc version        |N/A
|glibc version       |ldd (GNU libc) 2.41

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.

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions