x.json2: fix compilation with -autofree

pull/12600/head
Delyan Angelov 2021-11-28 18:31:41 +02:00
parent 5deb56fc79
commit 1913de0187
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 15 additions and 12 deletions

View File

@ -142,6 +142,7 @@ fn (mut p Parser) decode_value() ?Any {
return Any(null)
}
[manualfree]
fn (mut p Parser) decode_array() ?Any {
mut items := []Any{}
p.next_with_err() ?

View File

@ -19,6 +19,7 @@ fn write_value(v Any, i int, len int, mut wr strings.Builder) {
}
// str returns the string representation of the `map[string]Any`.
[manualfree]
pub fn (flds map[string]Any) str() string {
mut wr := strings.new_builder(200)
wr.write_b(`{`)
@ -37,6 +38,7 @@ pub fn (flds map[string]Any) str() string {
}
// str returns the string representation of the `[]Any`.
[manualfree]
pub fn (flds []Any) str() string {
mut wr := strings.new_builder(200)
wr.write_b(`[`)

View File

@ -16,10 +16,10 @@ pub mut:
fn (e Employee) to_json() string {
mut mp := map[string]json2.Any{}
mp['name'] = e.name
mp['age'] = e.age
mp['salary'] = e.salary
mp['title'] = int(e.title)
mp['name'] = json2.Any(e.name)
mp['age'] = json2.Any(e.age)
mp['salary'] = json2.Any(e.salary)
mp['title'] = json2.Any(int(e.title))
/*
$for field in Employee.fields {
d := e.$(field.name)
@ -36,10 +36,10 @@ fn (e Employee) to_json() string {
fn (mut e Employee) from_json(any json2.Any) {
mp := any.as_map()
e.name = mp['name'] or { 0 }.str()
e.age = mp['age'] or { 0 }.int()
e.salary = mp['salary'] or { 0 }.f32()
e.title = JobTitle(mp['title'] or { 0 }.int())
e.name = mp['name'] or { json2.Any('') }.str()
e.age = mp['age'] or { json2.Any(0) }.int()
e.salary = mp['salary'] or { json2.Any(0) }.f32()
e.title = JobTitle(mp['title'] or { json2.Any(0) }.int())
}
fn test_simple() {
@ -157,10 +157,10 @@ fn (u User) to_json() string {
'age': json2.Any(u.age)
}
mp['nums'] = u.nums.map(json2.Any(it))
mp['lastName'] = u.last_name
mp['IsRegistered'] = u.is_registered
mp['type'] = u.typ
mp['pet_animals'] = u.pets
mp['lastName'] = json2.Any(u.last_name)
mp['IsRegistered'] = json2.Any(u.is_registered)
mp['type'] = json2.Any(u.typ)
mp['pet_animals'] = json2.Any(u.pets)
return mp.str()
}