2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 21:20:28 +03:00
|
|
|
module json
|
|
|
|
|
2019-07-13 17:47:23 +03:00
|
|
|
#flag -I @VROOT/thirdparty/cJSON
|
2019-09-01 22:37:22 +03:00
|
|
|
#flag @VROOT/thirdparty/cJSON/cJSON.o
|
2019-06-23 10:03:52 +03:00
|
|
|
#include "cJSON.h"
|
2020-05-04 17:46:36 +03:00
|
|
|
#define js_get(object, key) cJSON_GetObjectItemCaseSensitive((object), (key))
|
2021-02-10 12:12:49 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
struct C.cJSON {
|
|
|
|
valueint int
|
2019-09-01 22:37:22 +03:00
|
|
|
valuedouble f32
|
2021-04-14 12:47:24 +03:00
|
|
|
valuestring &char
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2021-03-18 12:58:32 +03:00
|
|
|
fn C.cJSON_IsTrue(&C.cJSON) bool
|
|
|
|
|
|
|
|
fn C.cJSON_CreateNumber(int) &C.cJSON
|
|
|
|
|
|
|
|
fn C.cJSON_CreateBool(bool) &C.cJSON
|
|
|
|
|
2021-04-14 12:47:24 +03:00
|
|
|
fn C.cJSON_CreateString(&char) &C.cJSON
|
2021-03-18 12:58:32 +03:00
|
|
|
|
2021-04-14 12:47:24 +03:00
|
|
|
fn C.cJSON_Parse(&char) &C.cJSON
|
2021-03-18 12:58:32 +03:00
|
|
|
|
2021-04-14 12:47:24 +03:00
|
|
|
fn C.cJSON_PrintUnformatted(&C.cJSON) &char
|
2021-03-18 12:58:32 +03:00
|
|
|
|
2021-04-14 12:47:24 +03:00
|
|
|
fn C.cJSON_Print(&C.cJSON) &char
|
2021-03-18 12:58:32 +03:00
|
|
|
|
2020-05-01 13:02:31 +03:00
|
|
|
pub fn decode(typ voidptr, s string) ?voidptr {
|
2020-04-14 01:37:47 +03:00
|
|
|
// compiler implementation
|
2020-04-13 20:59:57 +03:00
|
|
|
return 0
|
2020-04-12 18:45:04 +03:00
|
|
|
}
|
|
|
|
|
2020-04-13 20:59:57 +03:00
|
|
|
pub fn encode(x voidptr) string {
|
|
|
|
// compiler implementation
|
|
|
|
return ''
|
2020-04-12 18:45:04 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 12:12:49 +03:00
|
|
|
pub fn encode_pretty(x voidptr) string {
|
|
|
|
// compiler implementation
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:46:36 +03:00
|
|
|
fn decode_int(root &C.cJSON) int {
|
2019-06-22 21:20:28 +03:00
|
|
|
if isnil(root) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return root.valueint
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:46:36 +03:00
|
|
|
fn decode_i8(root &C.cJSON) i8 {
|
2019-06-24 23:25:35 +03:00
|
|
|
if isnil(root) {
|
|
|
|
return i8(0)
|
|
|
|
}
|
|
|
|
return i8(root.valueint)
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:46:36 +03:00
|
|
|
fn decode_i16(root &C.cJSON) i16 {
|
2019-06-24 23:25:35 +03:00
|
|
|
if isnil(root) {
|
|
|
|
return i16(0)
|
|
|
|
}
|
|
|
|
return i16(root.valueint)
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:46:36 +03:00
|
|
|
fn decode_i64(root &C.cJSON) i64 {
|
2019-06-24 23:25:35 +03:00
|
|
|
if isnil(root) {
|
|
|
|
return i64(0)
|
|
|
|
}
|
2019-12-22 01:41:42 +03:00
|
|
|
return i64(root.valuedouble) // i64 is double in C
|
2019-06-24 23:25:35 +03:00
|
|
|
}
|
|
|
|
|
2020-05-04 17:46:36 +03:00
|
|
|
fn decode_byte(root &C.cJSON) byte {
|
2019-08-26 18:01:23 +03:00
|
|
|
if isnil(root) {
|
2019-09-01 22:37:22 +03:00
|
|
|
return byte(0)
|
2019-08-26 18:01:23 +03:00
|
|
|
}
|
2019-09-01 22:37:22 +03:00
|
|
|
return byte(root.valueint)
|
2019-08-26 18:01:23 +03:00
|
|
|
}
|
|
|
|
|
2020-05-04 17:46:36 +03:00
|
|
|
fn decode_u16(root &C.cJSON) u16 {
|
2019-08-26 18:01:23 +03:00
|
|
|
if isnil(root) {
|
|
|
|
return u16(0)
|
|
|
|
}
|
|
|
|
return u16(root.valueint)
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:46:36 +03:00
|
|
|
fn decode_u32(root &C.cJSON) u32 {
|
2019-08-26 18:01:23 +03:00
|
|
|
if isnil(root) {
|
|
|
|
return u32(0)
|
|
|
|
}
|
|
|
|
return u32(root.valueint)
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:46:36 +03:00
|
|
|
fn decode_u64(root &C.cJSON) u64 {
|
2019-08-26 18:01:23 +03:00
|
|
|
if isnil(root) {
|
|
|
|
return u64(0)
|
|
|
|
}
|
|
|
|
return u64(root.valueint)
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:46:36 +03:00
|
|
|
fn decode_f32(root &C.cJSON) f32 {
|
2019-06-24 23:25:35 +03:00
|
|
|
if isnil(root) {
|
|
|
|
return f32(0)
|
|
|
|
}
|
2019-12-07 17:13:25 +03:00
|
|
|
return root.valuedouble
|
2019-06-24 23:25:35 +03:00
|
|
|
}
|
|
|
|
|
2020-05-04 17:46:36 +03:00
|
|
|
fn decode_f64(root &C.cJSON) f64 {
|
2019-06-24 23:25:35 +03:00
|
|
|
if isnil(root) {
|
|
|
|
return f64(0)
|
|
|
|
}
|
|
|
|
return f64(root.valuedouble)
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:46:36 +03:00
|
|
|
fn decode_string(root &C.cJSON) string {
|
2019-06-22 21:20:28 +03:00
|
|
|
if isnil(root) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
if isnil(root.valuestring) {
|
|
|
|
return ''
|
|
|
|
}
|
2020-05-04 17:46:36 +03:00
|
|
|
// println('decode string valuestring="$root.valuestring"')
|
2019-06-22 21:20:28 +03:00
|
|
|
// return tos(root.valuestring, _strlen(root.valuestring))
|
2021-04-14 12:47:24 +03:00
|
|
|
return unsafe { tos_clone(&byte(root.valuestring)) } // , _strlen(root.valuestring))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-05-04 17:46:36 +03:00
|
|
|
fn decode_bool(root &C.cJSON) bool {
|
2019-06-22 21:20:28 +03:00
|
|
|
if isnil(root) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return C.cJSON_IsTrue(root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ///////////////////
|
2020-04-14 01:37:47 +03:00
|
|
|
fn encode_int(val int) &C.cJSON {
|
2019-06-22 21:20:28 +03:00
|
|
|
return C.cJSON_CreateNumber(val)
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:37:47 +03:00
|
|
|
fn encode_i8(val i8) &C.cJSON {
|
2019-06-24 23:25:35 +03:00
|
|
|
return C.cJSON_CreateNumber(val)
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:37:47 +03:00
|
|
|
fn encode_i16(val i16) &C.cJSON {
|
2019-06-24 23:25:35 +03:00
|
|
|
return C.cJSON_CreateNumber(val)
|
|
|
|
}
|
2019-08-26 18:01:23 +03:00
|
|
|
|
2020-04-14 01:37:47 +03:00
|
|
|
fn encode_i64(val i64) &C.cJSON {
|
2019-06-24 23:25:35 +03:00
|
|
|
return C.cJSON_CreateNumber(val)
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:37:47 +03:00
|
|
|
fn encode_byte(val byte) &C.cJSON {
|
2019-08-26 18:01:23 +03:00
|
|
|
return C.cJSON_CreateNumber(val)
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:37:47 +03:00
|
|
|
fn encode_u16(val u16) &C.cJSON {
|
2019-08-26 18:01:23 +03:00
|
|
|
return C.cJSON_CreateNumber(val)
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:37:47 +03:00
|
|
|
fn encode_u32(val u32) &C.cJSON {
|
2019-08-26 18:01:23 +03:00
|
|
|
return C.cJSON_CreateNumber(val)
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:37:47 +03:00
|
|
|
fn encode_u64(val u64) &C.cJSON {
|
2019-08-26 18:01:23 +03:00
|
|
|
return C.cJSON_CreateNumber(val)
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:37:47 +03:00
|
|
|
fn encode_f32(val f32) &C.cJSON {
|
2019-06-24 23:25:35 +03:00
|
|
|
return C.cJSON_CreateNumber(val)
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:37:47 +03:00
|
|
|
fn encode_f64(val f64) &C.cJSON {
|
2019-06-24 23:25:35 +03:00
|
|
|
return C.cJSON_CreateNumber(val)
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:37:47 +03:00
|
|
|
fn encode_bool(val bool) &C.cJSON {
|
2019-06-22 21:20:28 +03:00
|
|
|
return C.cJSON_CreateBool(val)
|
|
|
|
}
|
|
|
|
|
2020-04-14 01:37:47 +03:00
|
|
|
fn encode_string(val string) &C.cJSON {
|
2021-04-04 17:43:32 +03:00
|
|
|
return C.cJSON_CreateString(&char(val.str))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2021-02-10 12:12:49 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// ///////////////////////
|
|
|
|
// user := decode_User(json_parse(js_string_var))
|
2019-09-02 19:17:05 +03:00
|
|
|
fn json_parse(s string) &C.cJSON {
|
2021-04-04 17:43:32 +03:00
|
|
|
return C.cJSON_Parse(&char(s.str))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// json_string := json_print(encode_User(user))
|
2019-09-02 19:17:05 +03:00
|
|
|
fn json_print(json &C.cJSON) string {
|
2019-06-22 21:20:28 +03:00
|
|
|
s := C.cJSON_PrintUnformatted(json)
|
2021-04-14 12:47:24 +03:00
|
|
|
return unsafe { tos(&byte(s), C.strlen(&char(s))) }
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 12:12:49 +03:00
|
|
|
fn json_print_pretty(json &C.cJSON) string {
|
|
|
|
s := C.cJSON_Print(json)
|
2021-04-14 12:47:24 +03:00
|
|
|
return unsafe { tos(&byte(s), C.strlen(&char(s))) }
|
2021-02-10 12:12:49 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// / cjson wrappers
|
2019-09-02 19:17:05 +03:00
|
|
|
// fn json_array_for_each(val, root &C.cJSON) {
|
2019-06-22 21:20:28 +03:00
|
|
|
// #cJSON_ArrayForEach (val ,root)
|
|
|
|
// }
|