Skip to content

Commit f67e4ab

Browse files
committed
json: encode_pretty (p. 1)
1 parent db22665 commit f67e4ab

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

vlib/json/json_primitives.v

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module json
77
#flag @VROOT/thirdparty/cJSON/cJSON.o
88
#include "cJSON.h"
99
#define js_get(object, key) cJSON_GetObjectItemCaseSensitive((object), (key))
10+
1011
struct C.cJSON {
1112
valueint int
1213
valuedouble f32
@@ -23,6 +24,11 @@ pub fn encode(x voidptr) string {
2324
return ''
2425
}
2526

27+
pub fn encode_pretty(x voidptr) string {
28+
// compiler implementation
29+
return ''
30+
}
31+
2632
fn decode_int(root &C.cJSON) int {
2733
if isnil(root) {
2834
return 0
@@ -107,21 +113,17 @@ fn decode_string(root &C.cJSON) string {
107113

108114
fn C.cJSON_IsTrue(voidptr) bool
109115

110-
111116
fn C.cJSON_CreateNumber(int) &C.cJSON
112117

113-
114118
fn C.cJSON_CreateBool(bool) &C.cJSON
115119

116-
117120
fn C.cJSON_CreateString(charptr) &C.cJSON
118121

119-
120122
fn C.cJSON_Parse(charptr) &C.cJSON
121123

122-
123124
fn C.cJSON_PrintUnformatted(voidptr) byteptr
124125

126+
fn C.cJSON_Print(voidptr) byteptr
125127

126128
fn decode_bool(root &C.cJSON) bool {
127129
if isnil(root) {
@@ -178,6 +180,7 @@ fn encode_bool(val bool) &C.cJSON {
178180
fn encode_string(val string) &C.cJSON {
179181
return C.cJSON_CreateString(val.str)
180182
}
183+
181184
// ///////////////////////
182185
// user := decode_User(json_parse(js_string_var))
183186
fn json_parse(s string) &C.cJSON {
@@ -190,6 +193,11 @@ fn json_print(json &C.cJSON) string {
190193
return unsafe { tos(s, C.strlen(s)) }
191194
}
192195

196+
fn json_print_pretty(json &C.cJSON) string {
197+
s := C.cJSON_Print(json)
198+
return unsafe { tos(s, C.strlen(s)) }
199+
}
200+
193201
// / cjson wrappers
194202
// fn json_array_for_each(val, root &C.cJSON) {
195203
// #cJSON_ArrayForEach (val ,root)

vlib/v/gen/c/fn.v

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
627627
is_print := name in ['print', 'println', 'eprint', 'eprintln']
628628
print_method := name
629629
is_json_encode := name == 'json.encode'
630+
is_json_encode_pretty := name == 'json.encode_pretty'
630631
is_json_decode := name == 'json.decode'
631632
g.is_json_fn = is_json_encode || is_json_decode
632633
mut json_type_str := ''
@@ -635,7 +636,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
635636
json_obj = g.new_tmp_var()
636637
mut tmp2 := ''
637638
cur_line := g.go_before_stmt(0)
638-
if is_json_encode {
639+
if is_json_encode || is_json_encode_pretty {
639640
g.gen_json_for_type(node.args[0].typ)
640641
json_type_str = g.typ(node.args[0].typ)
641642
// `json__encode` => `json__encode_User`
@@ -650,7 +651,11 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
650651
g.call_args(node)
651652
g.writeln(');')
652653
tmp2 = g.new_tmp_var()
653-
g.writeln('string $tmp2 = json__json_print($json_obj);')
654+
if is_json_encode {
655+
g.writeln('string $tmp2 = json__json_print($json_obj);')
656+
} else {
657+
g.writeln('string $tmp2 = json__json_print_pretty($json_obj);')
658+
}
654659
} else {
655660
ast_type := node.args[0].expr as ast.Type
656661
// `json.decode(User, s)` => json.decode_User(s)

0 commit comments

Comments
 (0)