Skip to content

json #


json provides encoding/decoding of V data structures to/from JSON.


import json

enum JobTitle {

struct Employee {
    name   string
    age    int
    salary f32
    title  JobTitle

fn main() {
    x := Employee{'Peter', 28, 95000.5, .worker}
    s := json.encode(x)
    println('Employee x: ${s}')
    assert s == '{"name":"Peter","age":28,"salary":95000.5,"title":2}'
    y := json.decode(Employee, s)!
    assert y == x

Constants #

const used = 1

fn decode #

fn decode(typ voidptr, s string) !voidptr

decode tries to decode the provided JSON string, into a V structure.
If it can not do that, it returns an error describing the reason for the parsing failure.

fn encode #

fn encode(x voidptr) string

encode serialises the provided V value as a JSON string, optimised for shortness.

fn encode_pretty #

fn encode_pretty(x voidptr) string

encode_pretty serialises the provided V value as a JSON string, in a formatted way, optimised for viewing by humans.

struct C.cJSON #

struct C.cJSON {
	valueint    int
	valuedouble f64
	valuestring &char