v/vlib/json/json_primitives.v

232 lines
4.3 KiB
V
Raw Normal View History

2022-01-04 10:21:08 +01:00
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
2019-06-23 04:21:30 +02:00
// 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
#flag -I @VEXEROOT/thirdparty/cJSON
#flag @VEXEROOT/thirdparty/cJSON/cJSON.o
2019-06-23 09:03:52 +02:00
#include "cJSON.h"
2020-05-04 16:46:36 +02:00
#define js_get(object, key) cJSON_GetObjectItemCaseSensitive((object), (key))
2021-02-10 10:12:49 +01:00
2019-06-22 20:20:28 +02:00
struct C.cJSON {
valueint int
2021-09-24 15:49:00 +02:00
valuedouble f64
valuestring &char
2019-06-22 20:20:28 +02:00
}
fn C.cJSON_IsTrue(&C.cJSON) bool
fn C.cJSON_CreateNumber(int) &C.cJSON
fn C.cJSON_CreateBool(bool) &C.cJSON
fn C.cJSON_CreateString(&char) &C.cJSON
fn C.cJSON_Parse(&char) &C.cJSON
fn C.cJSON_PrintUnformatted(&C.cJSON) &char
fn C.cJSON_Print(&C.cJSON) &char
2020-05-01 12:02:31 +02:00
pub fn decode(typ voidptr, s string) ?voidptr {
2020-04-14 00:37:47 +02:00
// compiler implementation
return 0
}
pub fn encode(x voidptr) string {
// compiler implementation
return ''
}
2021-02-10 10:12:49 +01:00
pub fn encode_pretty(x voidptr) string {
// compiler implementation
return ''
}
2020-05-04 16:46:36 +02:00
fn decode_int(root &C.cJSON) int {
2019-06-22 20:20:28 +02:00
if isnil(root) {
return 0
}
return root.valueint
}
2020-05-04 16:46:36 +02:00
fn decode_i8(root &C.cJSON) i8 {
if isnil(root) {
return i8(0)
}
return i8(root.valueint)
}
2020-05-04 16:46:36 +02:00
fn decode_i16(root &C.cJSON) i16 {
if isnil(root) {
return i16(0)
}
return i16(root.valueint)
}
2020-05-04 16:46:36 +02:00
fn decode_i64(root &C.cJSON) i64 {
if isnil(root) {
return i64(0)
}
2019-12-21 23:41:42 +01:00
return i64(root.valuedouble) // i64 is double in C
}
2022-04-15 13:58:56 +02:00
fn decode_u8(root &C.cJSON) byte {
if isnil(root) {
2022-04-15 13:58:56 +02:00
return u8(0)
}
2022-04-15 13:58:56 +02:00
return u8(root.valueint)
}
2021-09-24 15:49:00 +02:00
fn decode_u8(root &C.cJSON) u8 {
if isnil(root) {
2022-04-15 13:58:56 +02:00
return u8(0)
2021-09-24 15:49:00 +02:00
}
2022-04-15 13:58:56 +02:00
return u8(root.valueint)
2021-09-24 15:49:00 +02:00
}
2020-05-04 16:46:36 +02:00
fn decode_u16(root &C.cJSON) u16 {
if isnil(root) {
return u16(0)
}
return u16(root.valueint)
}
2020-05-04 16:46:36 +02:00
fn decode_u32(root &C.cJSON) u32 {
if isnil(root) {
return u32(0)
}
return u32(root.valueint)
}
2020-05-04 16:46:36 +02:00
fn decode_u64(root &C.cJSON) u64 {
if isnil(root) {
return u64(0)
}
return u64(root.valuedouble)
}
2020-05-04 16:46:36 +02:00
fn decode_f32(root &C.cJSON) f32 {
if isnil(root) {
return f32(0)
}
2021-09-24 15:49:00 +02:00
return f32(root.valuedouble)
}
2020-05-04 16:46:36 +02:00
fn decode_f64(root &C.cJSON) f64 {
if isnil(root) {
return f64(0)
}
2021-09-24 15:49:00 +02:00
return root.valuedouble
}
fn decode_rune(root &C.cJSON) rune {
if isnil(root) {
return rune(0)
}
if isnil(root.valuestring) {
return rune(0)
}
// TODO: Parse as runes, bypassing string casting...?
2022-04-15 13:58:56 +02:00
return unsafe { tos_clone(&u8(root.valuestring)).runes().first() }
}
2020-05-04 16:46:36 +02:00
fn decode_string(root &C.cJSON) string {
2019-06-22 20:20:28 +02:00
if isnil(root) {
return ''
}
if isnil(root.valuestring) {
return ''
}
2020-05-04 16:46:36 +02:00
// println('decode string valuestring="$root.valuestring"')
2019-06-22 20:20:28 +02:00
// return tos(root.valuestring, _strlen(root.valuestring))
2022-04-15 13:58:56 +02:00
return unsafe { tos_clone(&u8(root.valuestring)) } // , _strlen(root.valuestring))
2019-06-22 20:20:28 +02:00
}
2020-05-04 16:46:36 +02:00
fn decode_bool(root &C.cJSON) bool {
2019-06-22 20:20:28 +02:00
if isnil(root) {
return false
}
return C.cJSON_IsTrue(root)
}
// ///////////////////
2020-04-14 00:37:47 +02:00
fn encode_int(val int) &C.cJSON {
2019-06-22 20:20:28 +02:00
return C.cJSON_CreateNumber(val)
}
2020-04-14 00:37:47 +02:00
fn encode_i8(val i8) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2020-04-14 00:37:47 +02:00
fn encode_i16(val i16) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2020-04-14 00:37:47 +02:00
fn encode_i64(val i64) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2022-04-15 13:58:56 +02:00
fn encode_u8(val byte) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2021-09-24 15:49:00 +02:00
fn encode_u8(val u8) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2020-04-14 00:37:47 +02:00
fn encode_u16(val u16) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2020-04-14 00:37:47 +02:00
fn encode_u32(val u32) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2020-04-14 00:37:47 +02:00
fn encode_u64(val u64) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2020-04-14 00:37:47 +02:00
fn encode_f32(val f32) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2020-04-14 00:37:47 +02:00
fn encode_f64(val f64) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
2020-04-14 00:37:47 +02:00
fn encode_bool(val bool) &C.cJSON {
2019-06-22 20:20:28 +02:00
return C.cJSON_CreateBool(val)
}
2021-09-24 15:49:00 +02:00
fn encode_rune(val rune) &C.cJSON {
return C.cJSON_CreateString(&char(val.str().str))
}
2020-04-14 00:37:47 +02:00
fn encode_string(val string) &C.cJSON {
return C.cJSON_CreateString(&char(val.str))
2019-06-22 20:20:28 +02:00
}
2021-02-10 10:12:49 +01:00
2019-06-22 20:20:28 +02:00
// ///////////////////////
// user := decode_User(json_parse(js_string_var))
2019-09-02 18:17:05 +02:00
fn json_parse(s string) &C.cJSON {
return C.cJSON_Parse(&char(s.str))
2019-06-22 20:20:28 +02:00
}
// 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)
2022-04-15 13:58:56 +02:00
return unsafe { tos(&u8(s), C.strlen(&char(s))) }
2019-06-22 20:20:28 +02:00
}
2021-02-10 10:12:49 +01:00
fn json_print_pretty(json &C.cJSON) string {
s := C.cJSON_Print(json)
2022-04-15 13:58:56 +02:00
return unsafe { tos(&u8(s), C.strlen(&char(s))) }
2021-02-10 10:12:49 +01:00
}
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)
// }