1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

json: encode_pretty (p. 1)

This commit is contained in:
Alexander Medvednikov
2021-02-10 10:12:49 +01:00
parent db2266598a
commit f67e4ab57c
2 changed files with 20 additions and 7 deletions

View File

@@ -627,6 +627,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
is_print := name in ['print', 'println', 'eprint', 'eprintln']
print_method := name
is_json_encode := name == 'json.encode'
is_json_encode_pretty := name == 'json.encode_pretty'
is_json_decode := name == 'json.decode'
g.is_json_fn = is_json_encode || is_json_decode
mut json_type_str := ''
@@ -635,7 +636,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
json_obj = g.new_tmp_var()
mut tmp2 := ''
cur_line := g.go_before_stmt(0)
if is_json_encode {
if is_json_encode || is_json_encode_pretty {
g.gen_json_for_type(node.args[0].typ)
json_type_str = g.typ(node.args[0].typ)
// `json__encode` => `json__encode_User`
@@ -650,7 +651,11 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
g.call_args(node)
g.writeln(');')
tmp2 = g.new_tmp_var()
g.writeln('string $tmp2 = json__json_print($json_obj);')
if is_json_encode {
g.writeln('string $tmp2 = json__json_print($json_obj);')
} else {
g.writeln('string $tmp2 = json__json_print_pretty($json_obj);')
}
} else {
ast_type := node.args[0].expr as ast.Type
// `json.decode(User, s)` => json.decode_User(s)