json: encode_pretty (p. 1)

pull/8672/head
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

@ -7,6 +7,7 @@ module json
#flag @VROOT/thirdparty/cJSON/cJSON.o #flag @VROOT/thirdparty/cJSON/cJSON.o
#include "cJSON.h" #include "cJSON.h"
#define js_get(object, key) cJSON_GetObjectItemCaseSensitive((object), (key)) #define js_get(object, key) cJSON_GetObjectItemCaseSensitive((object), (key))
struct C.cJSON { struct C.cJSON {
valueint int valueint int
valuedouble f32 valuedouble f32
@ -23,6 +24,11 @@ pub fn encode(x voidptr) string {
return '' return ''
} }
pub fn encode_pretty(x voidptr) string {
// compiler implementation
return ''
}
fn decode_int(root &C.cJSON) int { fn decode_int(root &C.cJSON) int {
if isnil(root) { if isnil(root) {
return 0 return 0
@ -107,21 +113,17 @@ fn decode_string(root &C.cJSON) string {
fn C.cJSON_IsTrue(voidptr) bool fn C.cJSON_IsTrue(voidptr) bool
fn C.cJSON_CreateNumber(int) &C.cJSON fn C.cJSON_CreateNumber(int) &C.cJSON
fn C.cJSON_CreateBool(bool) &C.cJSON fn C.cJSON_CreateBool(bool) &C.cJSON
fn C.cJSON_CreateString(charptr) &C.cJSON fn C.cJSON_CreateString(charptr) &C.cJSON
fn C.cJSON_Parse(charptr) &C.cJSON fn C.cJSON_Parse(charptr) &C.cJSON
fn C.cJSON_PrintUnformatted(voidptr) byteptr fn C.cJSON_PrintUnformatted(voidptr) byteptr
fn C.cJSON_Print(voidptr) byteptr
fn decode_bool(root &C.cJSON) bool { fn decode_bool(root &C.cJSON) bool {
if isnil(root) { if isnil(root) {
@ -178,6 +180,7 @@ fn encode_bool(val bool) &C.cJSON {
fn encode_string(val string) &C.cJSON { fn encode_string(val string) &C.cJSON {
return C.cJSON_CreateString(val.str) return C.cJSON_CreateString(val.str)
} }
// /////////////////////// // ///////////////////////
// user := decode_User(json_parse(js_string_var)) // user := decode_User(json_parse(js_string_var))
fn json_parse(s string) &C.cJSON { fn json_parse(s string) &C.cJSON {
@ -190,6 +193,11 @@ fn json_print(json &C.cJSON) string {
return unsafe { tos(s, C.strlen(s)) } return unsafe { tos(s, C.strlen(s)) }
} }
fn json_print_pretty(json &C.cJSON) string {
s := C.cJSON_Print(json)
return unsafe { tos(s, C.strlen(s)) }
}
// / cjson wrappers // / cjson wrappers
// fn json_array_for_each(val, root &C.cJSON) { // fn json_array_for_each(val, root &C.cJSON) {
// #cJSON_ArrayForEach (val ,root) // #cJSON_ArrayForEach (val ,root)

View File

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