Skip to content

Commit

Permalink
docs: add a JSON section, and subsections for encoding and decoding (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bbSnavy committed Feb 8, 2022
1 parent 4ef7d26 commit f080682
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions doc/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ For more details and troubleshooting, please visit the [vab GitHub repository](h
* [Spawning Concurrent Tasks](#spawning-concurrent-tasks)
* [Channels](#channels)
* [Shared Objects](#shared-objects)
* [Decoding JSON](#decoding-json)
* [JSON](#json)
* [Decoding JSON](#decoding-json)
* [Encoding JSON](#encoding-json)
* [Testing](#testing)
* [Memory management](#memory-management)
* [Stack and Heap](#stack-and-heap)
Expand Down Expand Up @@ -3714,7 +3716,14 @@ fn main() {
```
Shared variables must be structs, arrays or maps.

## Decoding JSON
## JSON

Because of the ubiquitous nature of JSON, support for it is built directly into V.

V generates code for JSON encoding and decoding.
No runtime reflection is used. This results in much better performance.

### Decoding JSON

```v
import json
Expand Down Expand Up @@ -3752,14 +3761,32 @@ println(foos[0].x)
println(foos[1].x)
```

Because of the ubiquitous nature of JSON, support for it is built directly into V.

The `json.decode` function takes two arguments:
the first is the type into which the JSON value should be decoded and
the second is a string containing the JSON data.

V generates code for JSON encoding and decoding.
No runtime reflection is used. This results in much better performance.
### Encoding JSON

```v
import json
struct User {
name string
score i64
}
mut data := map[string]int{}
user := &User{
name: 'Pierre'
score: 1024
}
data['x'] = 42
data['y'] = 360
println(json.encode(data)) // {"x":42,"y":360}
println(json.encode(user)) // {"name":"Pierre","score":1024}
```

## Testing

Expand Down

0 comments on commit f080682

Please sign in to comment.