1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/x/json2
2023-02-15 11:40:11 +02:00
..
json_module_compatibility_test x.json2: fix: #16975 (#16983) 2023-01-15 22:09:02 +02:00
any_test.v x.json2: support time.Time values in encode (#16643) 2022-12-11 16:54:28 +02:00
decode_struct_test.v x.json2: add json2.map_from(t T) (#16797) 2023-01-18 18:55:04 +02:00
decode_struct_todo_test.vv all: change optional to option (#16914) 2023-01-09 09:36:45 +03:00
decoder_test.v json2: decode refactor/fix (#16588) 2022-12-05 16:58:44 +02:00
decoder.v checker: remove c.pref.is_test exception for calling private methods in _other_ modules (#16872) 2023-01-05 15:41:18 +02:00
encode_struct_test.v json2: encode sumtype (#17041) 2023-01-21 20:26:55 +02:00
encode_struct_todo_test.vv json2: refactor, doc, perf, test, errors (#16986) 2023-01-15 22:52:48 +01:00
encoder_test.v json2: encode array of all and verify sum type (#17051) 2023-02-01 15:52:58 +01:00
encoder.v cgen, checker: fix comptimeselector resolution + if comptime branching improvement + comptimeselector cleanup (#17302) 2023-02-15 11:40:11 +02:00
integer_primitives_test.v json2: decode refactor/fix (#16588) 2022-12-05 16:58:44 +02:00
json2_test.v x.json2: add json2.map_from(t T) (#16797) 2023-01-18 18:55:04 +02:00
json2.v json2: encode array of all and verify sum type (#17051) 2023-02-01 15:52:58 +01:00
README.md doc: update the README for json2 (#16657) 2022-12-14 10:21:29 +02:00
scanner_test.v checker: check for reserved type names in for in (fix #14072) (#14212) 2022-04-30 01:25:29 +03:00
scanner.v vfmt: change all '$expr' to '${expr}' (#16428) 2022-11-15 16:53:13 +03:00
types.v x.json2: support time.Time values in encode (#16643) 2022-12-11 16:54:28 +02:00

The name json2 was chosen to avoid any unwanted potential conflicts with the existing codegen tailored for the main json module which is powered by CJSON.

x.json2 is an experimental JSON parser written from scratch on V.

Usage

encode[T]

import x.json2
import time

struct Person {
mut:
	name     string
	age      ?int = 20
	birthday time.Time
	deathday ?time.Time = none
}

fn main() {
	mut person := Person{
		name: 'Bob'
		birthday: time.now()
	}
	person_json := json2.encode[Person](person)
	// person_json == {"name": "Bob", "age": 20, "birthday": "2022-03-11T13:54:25.000Z"}
}

decode[T]

import x.json2
import time

struct Person {
mut:
	name     string
	age      ?int = 20
	birthday time.Time
	deathday ?time.Time = none
}

fn main() {
	resp := '{"name": "Bob", "age": 20, "birthday": ${time.now()}}'
	person := json2.decode[Person](resp)!
	/*
	struct Person {
      mut:
          name "Bob"
          age  20
          birthday "2022-03-11 13:54:25"
      }
	*/
}

decode[T] is smart and can auto-convert the types of struct fields - this means examples below will have the same result

json2.decode[Person]('{"name": "Bob", "age": 20, "birthday": "2022-03-11T13:54:25.000Z"}')!
json2.decode[Person]('{"name": "Bob", "age": 20, "birthday": "2022-03-11 13:54:25.000"}')!
json2.decode[Person]('{"name": "Bob", "age": "20", "birthday": 1647006865}')!
json2.decode[Person]('{"name": "Bob", "age": "20", "birthday": "1647006865"}}')!

raw decode

import x.json2
import net.http

fn main() {
	resp := http.get('https://reqres.in/api/products/1')!

	// This returns an Any type
	raw_product := json2.raw_decode(resp.body)!
}

Casting Any type / Navigating

import x.json2
import net.http

fn main() {
	resp := http.get('https://reqres.in/api/products/1')!

	raw_product := json2.raw_decode(resp.body)!

	product := raw_product.as_map()
	data := product['data'] as map[string]json2.Any

	id := data['id'].int() // 1
	name := data['name'].str() // cerulean
	year := data['year'].int() // 2000
}

Constructing an Any type

import x.json2

fn main() {
	mut me := map[string]json2.Any{}
	me['name'] = 'Bob'
	me['age'] = 18

	mut arr := []json2.Any{}
	arr << 'rock'
	arr << 'papers'
	arr << json2.null
	arr << 12

	me['interests'] = arr

	mut pets := map[string]json2.Any{}
	pets['Sam'] = 'Maltese Shitzu'
	me['pets'] = pets

	// Stringify to JSON
	println(me.str())
	//{
	//   "name":"Bob",
	//   "age":18,
	//   "interests":["rock","papers","scissors",null,12],
	//   "pets":{"Sam":"Maltese"}
	//}
}

Null Values

x.json2 has a separate Null type for differentiating an undefined value and a null value. To verify that the field you're accessing is a Null, use [typ] is json2.Null.

fn (mut p Person) from_json(f json2.Any) {
    obj := f.as_map()
    if obj['age'] is json2.Null {
        // use a default value
        p.age = 10
    }
}

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 JSON string representation of the value.
  4. Casting non-numeric values to int/float (int()/i64()/f32()/f64()) will return zero.