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