Repository navigation
Open
Description
Output Language: Python, Go
CLI, npm, or app.quicktype.io: CLI and npm (quicktype / quicktype-core)
Version: 26.0.0 (also on master c39af7d)
Description
Round-tripping JSON samples through the generated Python and Go code fails for the property names below:
- Python (
objand optionallist):- A property named
objis stored in a local variable namedobjinsidefrom_dict(obj), overwriting theobjparameter so every subsequentobj.get(...)/obj[...]reads from the wrong value and raisesAttributeError(26.0.0) orTypeError(master). - An optional property named
listemitslist: list[...] | None = Nonein the class body. In Python, the= Noneassignment runs before the annotation is evaluated in the class scope, solistisNonewhenlist[...]is evaluated, raisingTypeError: 'NoneType' object is not subscriptableon import.
- A property named
- Go (
-, backticks, and commas/quotes in JSON keys):- A property named
"-"produces the struct tag`json:"-"`, which tellsencoding/jsonto always omit the field instead of matching the key"-"(which requires`json:"-,"`per the encoding/json docs). - A property containing a backtick produces an unclosed raw string literal in the struct tag and does not compile.
- A property containing a comma (such as
"a,b") is emitted unescaped in`json:"a,b"`, soencoding/jsontreats,bas tag options and reads/writes key"a"instead. Keys with quotes or backslashes failencoding/json'sisValidTagcheck and silently fall back to the Go struct field name.
- A property named
Input Data
{"obj": {"a": 1}, "x": [{"b": 1}]}{"x": [{"list": [1]}, {}]}{"-": 1, "a,b": 2, "x": 3}{"a`b": 1}Expected Behaviour / Output
- Both generated Python modules import cleanly and
a.root_to_dict(a.root_from_dict(...))returns{'obj': {'a': 1}, 'x': [{'b': 1}]}. - The generated Go code compiles and round-trips
{"-":1,"a,b":2,"x":3}.
Current Behaviour / Output
Python output (a.py and b.py):
File "a.py", line 64, in from_dict
x = from_list(X.from_dict, obj.get("x"))
AttributeError: 'Obj' object has no attribute 'get'
File "b.py", line 39, in X
list: list[int] | None = None
TypeError: 'NoneType' object is not subscriptable
(On master c39af7d, a.py raises TypeError: 'Obj' object is not subscriptable on obj["x"].)
Go output (c.go and d/d.go):
{"a":0,"x":3}
c.go: Empty int64 `json:"-"`
c.go: AB int64 `json:"a,b"`
c.go: X int64 `json:"x"`
d/d.go: AB int64 `json:"a`b"`
vet: d/d.go:22:20: expected ';', found b (and 2 more errors)
Steps to Reproduce
# npm i quicktype@26.0.0
echo '{"obj": {"a": 1}, "x": [{"b": 1}]}' > a.json
echo '{"x": [{"list": [1]}, {}]}' > b.json
npx quicktype --lang python --top-level Root -o a.py a.json
npx quicktype --lang python --top-level Root -o b.py b.json
python3 -c 'import json, a; print(a.root_to_dict(a.root_from_dict(json.load(open("a.json")))))'
python3 -c 'import b'
echo '{"-": 1, "a,b": 2, "x": 3}' > c.json
echo '{"a`b": 1}' > d.json
mkdir -p d
npx quicktype --lang go --package main --top-level Root -o c.go c.json
npx quicktype --lang go --package main -o d/d.go d.json
cat > main.go <<'GO'
package main
import ("fmt"; "os")
func main() {
b, _ := os.ReadFile("c.json")
r, _ := UnmarshalRoot(b)
out, _ := r.Marshal()
fmt.Println(string(out))
}
GO
go mod init repro >/dev/null 2>&1; go run main.go c.go
grep json: c.go d/d.go
go vet ./dPossible Solution
- In Python,
bool,dict,float,int, andstrare inforbiddenPropertyNamesinpackages/quicktype-core/src/language/Python/constants.ts, andJSONPythonRendererreservesfrom_dict/to_dict(fix(python): reserve dict conversion methods #3506). AddinglisttoforbiddenPropertyNamesand reservingobjinJSONPythonRenderer.forbiddenForObjectProperties(or renaming thefrom_dictparameter) avoids both collisions. - In
GolangRenderer, emitjson:"-,"(orjson:"-,omitempty") when the JSON property name is"-", and use a quoted string literal for the struct tag when the key contains a backtick.
Credit: https://ucsbplse.github.io/
Metadata
Metadata
Assignees
Labels
No labels
Activity