2022-01-05 19:06:08 +03:00
|
|
|
## Description:
|
|
|
|
|
|
|
|
`json` provides encoding/decoding of V data structures to/from JSON.
|
|
|
|
|
|
|
|
## Examples:
|
|
|
|
|
|
|
|
```v
|
|
|
|
import json
|
|
|
|
|
|
|
|
enum JobTitle {
|
|
|
|
manager
|
|
|
|
executive
|
|
|
|
worker
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Employee {
|
|
|
|
name string
|
|
|
|
age int
|
|
|
|
salary f32
|
|
|
|
title JobTitle
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
x := Employee{'Peter', 28, 95000.5, .worker}
|
|
|
|
println(x)
|
|
|
|
//
|
|
|
|
s := json.encode(x)
|
2022-11-15 16:53:13 +03:00
|
|
|
println('Employee x: ${s}')
|
2022-01-05 19:06:08 +03:00
|
|
|
assert s == '{"name":"Peter","age":28,"salary":95000.5,"title":2}'
|
|
|
|
//
|
2022-10-26 11:26:28 +03:00
|
|
|
y := json.decode(Employee, s)!
|
2022-01-05 19:06:08 +03:00
|
|
|
//
|
|
|
|
println(y)
|
|
|
|
assert y == x
|
|
|
|
}
|
|
|
|
```
|