1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: json sumtype inlining (#11961)

This commit is contained in:
Anton Zavodchikov
2021-09-24 18:49:00 +05:00
committed by GitHub
parent 430677a0c0
commit c75271fcb7
3 changed files with 178 additions and 29 deletions

View File

@@ -10,7 +10,7 @@ module json
struct C.cJSON {
valueint int
valuedouble f32
valuedouble f64
valuestring &char
}
@@ -78,6 +78,13 @@ fn decode_byte(root &C.cJSON) byte {
return byte(root.valueint)
}
fn decode_u8(root &C.cJSON) u8 {
if isnil(root) {
return byte(0)
}
return byte(root.valueint)
}
fn decode_u16(root &C.cJSON) u16 {
if isnil(root) {
return u16(0)
@@ -103,14 +110,26 @@ fn decode_f32(root &C.cJSON) f32 {
if isnil(root) {
return f32(0)
}
return root.valuedouble
return f32(root.valuedouble)
}
fn decode_f64(root &C.cJSON) f64 {
if isnil(root) {
return f64(0)
}
return f64(root.valuedouble)
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...?
return unsafe { tos_clone(&byte(root.valuestring)).runes().first() }
}
fn decode_string(root &C.cJSON) string {
@@ -153,6 +172,10 @@ fn encode_byte(val byte) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
fn encode_u8(val u8) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
fn encode_u16(val u16) &C.cJSON {
return C.cJSON_CreateNumber(val)
}
@@ -177,6 +200,10 @@ fn encode_bool(val bool) &C.cJSON {
return C.cJSON_CreateBool(val)
}
fn encode_rune(val rune) &C.cJSON {
return C.cJSON_CreateString(&char(val.str().str))
}
fn encode_string(val string) &C.cJSON {
return C.cJSON_CreateString(&char(val.str))
}