v/vlib/json/json_primitives.v

181 lines
3.3 KiB
V
Raw Normal View History

2019-06-23 04:21:30 +02:00
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
2019-06-22 20:20:28 +02:00
module json
2019-07-13 16:47:23 +02:00
#flag -I @VROOT/thirdparty/cJSON
2019-09-01 21:37:22 +02:00
#flag @VROOT/thirdparty/cJSON/cJSON.o
2019-06-23 09:03:52 +02:00
#include "cJSON.h"
2019-07-13 16:47:23 +02:00
2019-06-22 20:20:28 +02:00
struct C.cJSON {
valueint int
2019-09-01 21:37:22 +02:00
valuedouble f32
2019-06-22 20:20:28 +02:00
valuestring byteptr
}
2019-09-02 18:17:05 +02:00
fn jsdecode_int(root &C.cJSON) int {
2019-06-22 20:20:28 +02:00
if isnil(root) {
return 0
}
return root.valueint
}
2019-09-02 18:17:05 +02:00
fn jsdecode_i8(root &C.cJSON) i8 {
if isnil(root) {
return i8(0)
}
return i8(root.valueint)
}
2019-09-02 18:17:05 +02:00
fn jsdecode_i16(root &C.cJSON) i16 {
if isnil(root) {
return i16(0)
}
return i16(root.valueint)
}
2019-09-02 18:17:05 +02:00
fn jsdecode_i64(root &C.cJSON) i64 {
if isnil(root) {
return i64(0)
}
return i64(root.valuedouble) //i64 is double in C
}
2019-09-02 18:17:05 +02:00
fn jsdecode_byte(root &C.cJSON) byte {
if isnil(root) {
2019-09-01 21:37:22 +02:00
return byte(0)
}
2019-09-01 21:37:22 +02:00
return byte(root.valueint)
}
2019-09-02 18:17:05 +02:00
fn jsdecode_u16(root &C.cJSON) u16 {
if isnil(root) {
return u16(0)
}
return u16(root.valueint)
}
2019-09-02 18:17:05 +02:00
fn jsdecode_u32(root &C.cJSON) u32 {
if isnil(root) {
return u32(0)
}
return u32(root.valueint)
}
2019-09-02 18:17:05 +02:00
fn jsdecode_u64(root &C.cJSON) u64 {
if isnil(root) {
return u64(0)
}
return u64(root.valueint)
}
2019-09-02 18:17:05 +02:00
fn jsdecode_f32(root &C.cJSON) f32 {
if isnil(root) {
return f32(0)
}
return f32(root.valuedouble)
}
2019-09-02 18:17:05 +02:00
fn jsdecode_f64(root &C.cJSON) f64 {
if isnil(root) {
return f64(0)
}
return f64(root.valuedouble)
}
2019-09-02 18:17:05 +02:00
fn jsdecode_string(root &C.cJSON) string {
2019-06-22 20:20:28 +02:00
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))
}
2019-11-24 04:27:02 +01:00
fn C.cJSON_IsTrue() bool
fn C.cJSON_CreateNumber() &C.cJSON
fn C.cJSON_CreateBool() &C.cJSON
fn C.cJSON_CreateString() &C.cJSON
fn C.cJSON_Parse() &C.cJSON
fn C.cJSON_PrintUnformatted() byteptr
2019-09-02 18:17:05 +02:00
fn jsdecode_bool(root &C.cJSON) bool {
2019-06-22 20:20:28 +02:00
if isnil(root) {
return false
}
return C.cJSON_IsTrue(root)
}
// ///////////////////
2019-09-02 18:17:05 +02:00
fn jsencode_int(val int) &C.cJSON {
2019-06-22 20:20:28 +02:00
return C.cJSON_CreateNumber(val)
}
2019-09-02 18:17:05 +02:00
fn jsencode_i8(val i8) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2019-09-02 18:17:05 +02:00
fn jsencode_i16(val i16) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2019-09-02 18:17:05 +02:00
fn jsencode_i64(val i64) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2019-09-02 18:17:05 +02:00
fn jsencode_byte(val byte) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2019-09-02 18:17:05 +02:00
fn jsencode_u16(val u16) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2019-09-02 18:17:05 +02:00
fn jsencode_u32(val u32) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2019-09-02 18:17:05 +02:00
fn jsencode_u64(val u64) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2019-09-02 18:17:05 +02:00
fn jsencode_f32(val f32) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2019-09-02 18:17:05 +02:00
fn jsencode_f64(val f64) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2019-09-02 18:17:05 +02:00
fn jsencode_bool(val bool) &C.cJSON {
2019-06-22 20:20:28 +02:00
return C.cJSON_CreateBool(val)
}
2019-09-02 18:17:05 +02:00
fn jsencode_string(val string) &C.cJSON {
2019-06-22 20:20:28 +02:00
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))
2019-09-02 18:17:05 +02:00
fn json_parse(s string) &C.cJSON {
2019-06-22 20:20:28 +02:00
return C.cJSON_Parse(s.str)
}
// json_string := json_print(encode_User(user))
2019-09-02 18:17:05 +02:00
fn json_print(json &C.cJSON) string {
2019-06-22 20:20:28 +02:00
s := C.cJSON_PrintUnformatted(json)
return tos(s, C.strlen(s))
2019-06-22 20:20:28 +02:00
}
// / cjson wrappers
2019-09-02 18:17:05 +02:00
// fn json_array_for_each(val, root &C.cJSON) {
2019-06-22 20:20:28 +02:00
// #cJSON_ArrayForEach (val ,root)
// }