mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
V 0.0.12 open-source release
This commit is contained in:
66
json/json_primitives.v
Normal file
66
json/json_primitives.v
Normal file
@@ -0,0 +1,66 @@
|
||||
module json
|
||||
|
||||
// #include "json/cJSON/cJSON.c"
|
||||
#include "json/cJSON/cJSON.h"
|
||||
struct C.cJSON {
|
||||
valueint int
|
||||
valuestring byteptr
|
||||
}
|
||||
|
||||
fn jsdecode_int(root *C.cJSON) int {
|
||||
if isnil(root) {
|
||||
return 0
|
||||
}
|
||||
return root.valueint
|
||||
}
|
||||
|
||||
fn jsdecode_string(root *C.cJSON) string {
|
||||
if isnil(root) {
|
||||
return ''
|
||||
}
|
||||
if isnil(root.valuestring) {
|
||||
return ''
|
||||
}
|
||||
// println('jsdecode string valuestring="$root.valuestring"')
|
||||
// return tos(root.valuestring, _strlen(root.valuestring))
|
||||
return tos_clone(root.valuestring)// , _strlen(root.valuestring))
|
||||
}
|
||||
|
||||
fn jsdecode_bool(root *C.cJSON) bool {
|
||||
if isnil(root) {
|
||||
return false
|
||||
}
|
||||
return C.cJSON_IsTrue(root)
|
||||
}
|
||||
|
||||
// ///////////////////
|
||||
fn jsencode_int(val int) *C.cJSON {
|
||||
return C.cJSON_CreateNumber(val)
|
||||
}
|
||||
|
||||
fn jsencode_bool(val bool) *C.cJSON {
|
||||
return C.cJSON_CreateBool(val)
|
||||
}
|
||||
|
||||
fn jsencode_string(val string) *C.cJSON {
|
||||
clone := val.clone()
|
||||
return C.cJSON_CreateString(clone.str)
|
||||
// return C.cJSON_CreateString2(val.str, val.len)
|
||||
}
|
||||
|
||||
// ///////////////////////
|
||||
// user := decode_User(json_parse(js_string_var))
|
||||
fn json_parse(s string) *C.cJSON {
|
||||
return C.cJSON_Parse(s.str)
|
||||
}
|
||||
|
||||
// json_string := json_print(encode_User(user))
|
||||
fn json_print(json *C.cJSON) string {
|
||||
s := C.cJSON_PrintUnformatted(json)
|
||||
return tos(s, _strlen(s))
|
||||
}
|
||||
|
||||
// / cjson wrappers
|
||||
// fn json_array_for_each(val, root *C.cJSON) {
|
||||
// #cJSON_ArrayForEach (val ,root)
|
||||
// }
|
||||
Reference in New Issue
Block a user