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

docs_ci: check all md files except thirdparty (#6855)

This commit is contained in:
Lukas Neubert
2020-11-18 18:28:28 +01:00
committed by GitHub
parent d8f64f516b
commit df4165c7ee
20 changed files with 373 additions and 221 deletions

View File

@@ -1,11 +1,12 @@
> `json2` was named just to avoid any unwanted potential conflicts with the existing codegen tailored for the main `json` module which is powered by CJSON.
> `json2` was named just to avoid any unwanted potential conflicts with the existing codegen
> tailored for the main `json` module which is powered by CJSON.
An experimental version of the JSON parser written from scratch on V.
An experimental version of the JSON parser written from scratch on V.
## Usage
```v
```v oksyntax
import x.json2
import http
import net.http
fn main() {
// Decoding
@@ -18,7 +19,7 @@ fn main() {
person := raw_person.as_map()
name := person['name'].str() // Bob
age := person['age'].int() // 19
pi := person['pi'].f64() // 3.14....
pi := person['pi'].f64() // 3.14....
// Constructing an `Any` type
mut me := map[string]json2.Any
@@ -34,7 +35,7 @@ fn main() {
me['interests'] = arr
mut pets := map[string]json2.Any
pets['Sam'] = 'Maltese Shitzu'
pets['Sam'] = 'Maltese Shitzu'
me['pets'] = pets
// Stringify to JSON
@@ -46,11 +47,16 @@ fn main() {
}
```
## Using `decode<T>` and `encode<T>`
> Codegen for this feature is still WIP. You need to manually define the methods before using the module to structs.
> Codegen for this feature is still WIP.
> You need to manually define the methods before using the module to structs.
In order to use the `decode<T>` and `encode<T>` function, you need to explicitly define two methods: `from_json` and `to_json`. `from_json` accepts a `json2.Any` argument and inside of it you need to map the fields you're going to put into the type. As for `to_json` method, you just need to map the values into `json2.Any` and turn it into a string.
In order to use the `decode<T>` and `encode<T>` function, you need to explicitly define
two methods: `from_json` and `to_json`. `from_json` accepts a `json2.Any` argument
and inside of it you need to map the fields you're going to put into the type.
As for `to_json` method, you just need to map the values into `json2.Any`
and turn it into a string.
```v
```v ignore
struct Person {
mut:
name string
@@ -88,12 +94,14 @@ fn main() {
```
## Using struct tags
`x.json2` cannot use struct tags just like when you use the `json` module. However, it emits an `Any` type when decoding so it can be flexible on the way you use it.
`x.json2` cannot use struct tags just like when you use the `json` module.
However, it emits an `Any` type when decoding so it can be flexible on the way you use it.
### Null Values
`x.json2` have a `null` value for differentiating an undefined value and a null value. Use `is` for verifying the field you're using is a null.
`x.json2` have a `null` value for differentiating an undefined value and a null value.
Use `is` for verifying the field you're using is a null.
```v
```v ignore
fn (mut p Person) from_json(f json2.Any) {
obj := f.as_map()
if obj['age'] is json2.Null {
@@ -104,16 +112,18 @@ fn (mut p Person) from_json(f json2.Any) {
```
### Custom field names
In `json`, you can specify the field name you're mapping into the struct field by specifying a `json:` tag. In `x.json2`, just simply cast the base field into a map (`as_map()`) and get the value of the field you wish to put into the struct/type.
In `json`, you can specify the field name you're mapping into the struct field by specifying
a `json:` tag. In `x.json2`, just simply cast the base field into a map (`as_map()`)
and get the value of the field you wish to put into the struct/type.
```v
```v ignore
fn (mut p Person) from_json(f json2.Any) {
obj := f.as_map()
p.name = obj['nickname'].str()
}
```
```v
```v oksyntax
fn (mut p Person) to_json() string {
obj := f.as_map()
obj['nickname'] = p.name
@@ -122,12 +132,16 @@ fn (mut p Person) to_json() string {
```
### Undefined Values
Getting undefined values has the same behavior as regular V types. If you're casting a base field into `map[string]json2.Any` and fetch an undefined entry/value, it simply returns empty. As for the `[]json2.Any`, it returns an index error.
Getting undefined values has the same behavior as regular V types.
If you're casting a base field into `map[string]json2.Any` and fetch an undefined entry/value,
it simply returns empty. As for the `[]json2.Any`, it returns an index error.
## Casting a value to an incompatible type
`x.json2` provides methods for turning `Any` types into usable types. The following list shows the possible outputs when casting a value to an incompatible type.
`x.json2` provides methods for turning `Any` types into usable types.
The following list shows the possible outputs when casting a value to an incompatible type.
1. Casting non-array values as array (`arr()`) will return an array with the value as the content.
2. Casting non-map values as map (`as_map()`) will return a map with the value as the content.
3. Casting non-string values to string (`str()`) will return the stringified representation of the value.
4. Casting non-numeric values to int/float (`int()`/`f64()`) will return zero.
3. Casting non-string values to string (`str()`)
will return the stringified representation of the value.
4. Casting non-numeric values to int/float (`int()`/`f64()`) will return zero.