diff --git a/vlib/x/json2/decoder.v b/vlib/x/json2/decoder.v index 991be8abc6..9f62df3ea7 100644 --- a/vlib/x/json2/decoder.v +++ b/vlib/x/json2/decoder.v @@ -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() ? diff --git a/vlib/x/json2/encoder.v b/vlib/x/json2/encoder.v index 8f6cc50804..d62387fb3a 100644 --- a/vlib/x/json2/encoder.v +++ b/vlib/x/json2/encoder.v @@ -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(`[`) diff --git a/vlib/x/json2/json2_test.v b/vlib/x/json2/json2_test.v index 594c257390..53dc03dd58 100644 --- a/vlib/x/json2/json2_test.v +++ b/vlib/x/json2/json2_test.v @@ -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() }