mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
json: tag functions that cgen may call internally for json.encode/json.decode, as [markused]
This commit is contained in:
parent
42efc383d2
commit
857e047d01
@ -52,6 +52,7 @@ pub fn encode_pretty(x voidptr) string {
|
||||
return ''
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_int(root &C.cJSON) int {
|
||||
if isnil(root) {
|
||||
return 0
|
||||
@ -59,6 +60,7 @@ fn decode_int(root &C.cJSON) int {
|
||||
return root.valueint
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_i8(root &C.cJSON) i8 {
|
||||
if isnil(root) {
|
||||
return i8(0)
|
||||
@ -66,6 +68,7 @@ fn decode_i8(root &C.cJSON) i8 {
|
||||
return i8(root.valueint)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_i16(root &C.cJSON) i16 {
|
||||
if isnil(root) {
|
||||
return i16(0)
|
||||
@ -73,6 +76,7 @@ fn decode_i16(root &C.cJSON) i16 {
|
||||
return i16(root.valueint)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_i64(root &C.cJSON) i64 {
|
||||
if isnil(root) {
|
||||
return i64(0)
|
||||
@ -81,10 +85,12 @@ fn decode_i64(root &C.cJSON) i64 {
|
||||
}
|
||||
|
||||
// TODO: remove when `byte` is removed
|
||||
[markused]
|
||||
fn decode_byte(root &C.cJSON) byte {
|
||||
return byte(decode_u8(root))
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_u8(root &C.cJSON) u8 {
|
||||
if isnil(root) {
|
||||
return u8(0)
|
||||
@ -92,6 +98,7 @@ fn decode_u8(root &C.cJSON) u8 {
|
||||
return u8(root.valueint)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_u16(root &C.cJSON) u16 {
|
||||
if isnil(root) {
|
||||
return u16(0)
|
||||
@ -99,6 +106,7 @@ fn decode_u16(root &C.cJSON) u16 {
|
||||
return u16(root.valueint)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_u32(root &C.cJSON) u32 {
|
||||
if isnil(root) {
|
||||
return u32(0)
|
||||
@ -106,6 +114,7 @@ fn decode_u32(root &C.cJSON) u32 {
|
||||
return u32(root.valueint)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_u64(root &C.cJSON) u64 {
|
||||
if isnil(root) {
|
||||
return u64(0)
|
||||
@ -113,6 +122,7 @@ fn decode_u64(root &C.cJSON) u64 {
|
||||
return u64(root.valuedouble)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_f32(root &C.cJSON) f32 {
|
||||
if isnil(root) {
|
||||
return f32(0)
|
||||
@ -120,6 +130,7 @@ fn decode_f32(root &C.cJSON) f32 {
|
||||
return f32(root.valuedouble)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_f64(root &C.cJSON) f64 {
|
||||
if isnil(root) {
|
||||
return f64(0)
|
||||
@ -127,6 +138,7 @@ fn decode_f64(root &C.cJSON) f64 {
|
||||
return root.valuedouble
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_rune(root &C.cJSON) rune {
|
||||
if isnil(root) {
|
||||
return rune(0)
|
||||
@ -139,6 +151,7 @@ fn decode_rune(root &C.cJSON) rune {
|
||||
return unsafe { tos_clone(&u8(root.valuestring)).runes().first() }
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_string(root &C.cJSON) string {
|
||||
if isnil(root) {
|
||||
return ''
|
||||
@ -149,6 +162,7 @@ fn decode_string(root &C.cJSON) string {
|
||||
return unsafe { tos_clone(&u8(root.valuestring)) } // , _strlen(root.valuestring))
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn decode_bool(root &C.cJSON) bool {
|
||||
if isnil(root) {
|
||||
return false
|
||||
@ -158,70 +172,86 @@ fn decode_bool(root &C.cJSON) bool {
|
||||
|
||||
// ///////////////////
|
||||
|
||||
[markused]
|
||||
fn encode_int(val int) &C.cJSON {
|
||||
return C.cJSON_CreateNumber(val)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn encode_i8(val i8) &C.cJSON {
|
||||
return C.cJSON_CreateNumber(val)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn encode_i16(val i16) &C.cJSON {
|
||||
return C.cJSON_CreateNumber(val)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn encode_i64(val i64) &C.cJSON {
|
||||
return C.cJSON_CreateNumber(val)
|
||||
}
|
||||
|
||||
// TODO: remove when `byte` is removed
|
||||
[markused]
|
||||
fn encode_byte(root byte) &C.cJSON {
|
||||
return encode_u8(u8(root))
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn encode_u8(val u8) &C.cJSON {
|
||||
return C.cJSON_CreateNumber(val)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn encode_u16(val u16) &C.cJSON {
|
||||
return C.cJSON_CreateNumber(val)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn encode_u32(val u32) &C.cJSON {
|
||||
return C.cJSON_CreateNumber(val)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn encode_u64(val u64) &C.cJSON {
|
||||
return C.cJSON_CreateNumber(val)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn encode_f32(val f32) &C.cJSON {
|
||||
return C.cJSON_CreateNumber(val)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn encode_f64(val f64) &C.cJSON {
|
||||
return C.cJSON_CreateNumber(val)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn encode_bool(val bool) &C.cJSON {
|
||||
return C.cJSON_CreateBool(val)
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn encode_rune(val rune) &C.cJSON {
|
||||
return C.cJSON_CreateString(&char(val.str().str))
|
||||
}
|
||||
|
||||
[markused]
|
||||
fn encode_string(val string) &C.cJSON {
|
||||
return C.cJSON_CreateString(&char(val.str))
|
||||
}
|
||||
|
||||
// ///////////////////////
|
||||
// user := decode_User(json_parse(js_string_var))
|
||||
[markused]
|
||||
fn json_parse(s string) &C.cJSON {
|
||||
return C.cJSON_Parse(&char(s.str))
|
||||
}
|
||||
|
||||
// json_string := json_print(encode_User(user))
|
||||
[markused]
|
||||
fn json_print(json &C.cJSON) string {
|
||||
s := C.cJSON_PrintUnformatted(json)
|
||||
r := unsafe { tos_clone(&u8(s)) }
|
||||
|
1
vlib/v/tests/skip_unused/json_decode_u32.run.out
Normal file
1
vlib/v/tests/skip_unused/json_decode_u32.run.out
Normal file
@ -0,0 +1 @@
|
||||
[]
|
@ -0,0 +1 @@
|
||||
[]
|
17
vlib/v/tests/skip_unused/json_decode_u32.vv
Normal file
17
vlib/v/tests/skip_unused/json_decode_u32.vv
Normal file
@ -0,0 +1,17 @@
|
||||
module main
|
||||
|
||||
import json
|
||||
|
||||
struct Message {
|
||||
content string
|
||||
}
|
||||
|
||||
struct LinksData {
|
||||
total_results u32
|
||||
messages [][]Message
|
||||
}
|
||||
|
||||
fn main() {
|
||||
links_data := json.decode(LinksData, '{}')?
|
||||
println(links_data.messages)
|
||||
}
|
Loading…
Reference in New Issue
Block a user