Skip to content

json decode transforms empty objects to empty strings #19791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
einar-hjortdal opened this issue Nov 6, 2023 · 2 comments
Open

json decode transforms empty objects to empty strings #19791

einar-hjortdal opened this issue Nov 6, 2023 · 2 comments
Labels
Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. Unit: Documentation Bugs/feature requests, that are related to the documentations.

Comments

@einar-hjortdal
Copy link
Contributor

einar-hjortdal commented Nov 6, 2023

Describe the bug

module main

import json

const hypothetical_json = "{\"propertyOne\":\"property_two should be parsed as a string representing an empty object {}\",\"propertyTwo\":{},\"property_three\":{\"nestedOne\":\"\"},\"propertyFour\":{}}"

struct TestStructOne {
	property_one string [json: 'propertyOne']
	property_two string [json: 'propertyTwo']
	property_three TestStructTwo [json: 'property_three']
	property_four TestStructTwo [json: 'propertyFour']
}

struct TestStructTwo {
	nested_one string [json: 'nestedOne']
}

fn main() {
	// suppose TestStructOne is received as request body from a JSON API or frontend framework
	// suppose property_two expects an arbitrary object, stringified with JSON.stringify
	// JSON.stringify outputs {} to represent an empty object
	// json.decode parses {} as ''
	// An empty string in JSON is "", not {}
println(json.decode(TestStructOne, hypothetical_json)!)
}

Reproduction Steps

Run the code above

Expected Behavior

{} should be parsed as '{}'

Current Behavior

{} is parsed as ""

Possible Solution

No response

Additional Information/Context

No response

V version

Current V version: V 0.4.2 a92700e, timestamp: 2023-11-06 20:09:05 +0200

Environment details (OS name and version, etc.)

V full version: V 0.4.2 95509cf.a92700e
OS: linux, Linux version 5.14.0-284.30.1.el9_2.x86_64 (mockbuild@mj.euro.kojibuilder106) (gcc (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4), GNU ld version 2.35.2-37.el9) #1 SMP PREEMPT_DYNAMIC Fri Sep 15 08:06:33 UTC 2023
Processor: 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz

getwd: /home/coachonko
vexe: /home/coachonko/.local/lib64/v/v
vexe mtime: 2023-11-06 21:32:00

vroot: OK, value: /home/coachonko/.local/lib64/v
VMODULES: OK, value: /home/coachonko/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.39.3
Git vroot status: weekly.2023.43-126-ga92700e9
.git/config present: true

CC version: cc (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4)
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

@einar-hjortdal einar-hjortdal added the Bug This tag is applied to issues which reports bugs. label Nov 6, 2023
@einar-hjortdal
Copy link
Contributor Author

einar-hjortdal commented Nov 6, 2023

Maybe not a bug but expected.

An empty map is represented by {}, but anything else is also turned to {}

struct Any {}
struct TestStructThree {
	prop_one map[string]Any [json: 'propOne']
}
fn main() {
	test_struct := TestStructThree{}
	println(json.encode(test_struct)) // {"propOne":{}}
	println(json.decode(TestStructThree, '{"propertyOne":{}}')!)
	//TestStructThree{
	//	prop_one: {}
	//}
// Hoever
test := json.decode(TestStructThree, '\"{\"propertyOne\":\"should be parsed as a string\"}\"')!
println(test) // also results as empty object

test_two := json.decode(TestStructThree, '"{\n  \"topLevelProperty\": {\n    \"nestedProperty1\": \"Value 1\",\n    \"nestedProperty2\": \"Value 2\",\n    \"nestedProperty3\": {\n      \"subNestedProperty1\": \"Sub-Value 1\",\n      \"subNestedProperty2\": \"Sub-Value 2\"\n    }\n  }\n}"')!
println(test_two) // also results as empty object
}

I believe:

  1. {} should fail to be decoded to string.
  2. There should be a type that accepts raw json such that it can be passed through V to a database

@ArtemkaKun ArtemkaKun added the Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. label Nov 7, 2023
@einar-hjortdal
Copy link
Contributor Author

einar-hjortdal commented Nov 7, 2023

Searched the source code: json [raw] attribute exists but is not documented.
This issue needs relabeling. It's a documentation issue now

@ArtemkaKun ArtemkaKun added Unit: Documentation Bugs/feature requests, that are related to the documentations. Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. and removed Bug This tag is applied to issues which reports bugs. Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. labels Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. Unit: Documentation Bugs/feature requests, that are related to the documentations.
Projects
None yet
Development

No branches or pull requests

2 participants