json: fix `f32 is not struct`

pull/5145/head
Delyan Angelov 2020-05-31 14:17:20 +03:00
parent faf3248e98
commit f1f6fb1a9f
2 changed files with 7 additions and 3 deletions

View File

@ -3,18 +3,22 @@ import json
struct Employee { struct Employee {
name string name string
age int age int
salary f32
} }
fn test_simple() { fn test_simple() {
x := Employee{'Peter', 28} x := Employee{'Peter', 28, 95000.5}
s := json.encode(x) s := json.encode(x)
assert s == '{"name":"Peter","age":28}' eprintln('Employee x: $s')
assert s == '{"name":"Peter","age":28,"salary":95000.5}'
y := json.decode(Employee, s) or { y := json.decode(Employee, s) or {
assert false assert false
Employee{} Employee{}
} }
eprintln('Employee y: $y')
assert y.name == 'Peter' assert y.name == 'Peter'
assert y.age == 28 assert y.age == 28
assert y.salary == 95000.5
} }
struct User2 { struct User2 {

View File

@ -22,7 +22,7 @@ fn (mut g Gen) gen_json_for_type(typ table.Type) {
mut enc := strings.new_builder(100) mut enc := strings.new_builder(100)
sym := g.table.get_type_symbol(typ) sym := g.table.get_type_symbol(typ)
styp := g.typ(typ) styp := g.typ(typ)
if sym.name in ['int', 'string', 'bool'] { if sym.name in ['int', 'string', 'bool', 'f32'] {
return return
} }
if sym.kind == .array { if sym.kind == .array {