v/vlib/json
2024-09-22 07:54:22 +03:00
..
cjson all: change single blank comment to blank line (#22016) 2024-08-09 14:55:58 +03:00
json_alias_test.v json: fix json.decode with map alias (#17925) 2023-04-10 19:50:35 +03:00
json_decode_anon_struct_test.v json: allow passing an anon struct as a decode type (#22228) 2024-09-17 15:33:17 +03:00
json_decode_embed_test.v json: fix decoding of structs with embeds (#22264) 2024-09-21 09:01:11 +03:00
json_decode_option_enum_test.v json: fix struct with option enum field (fix #20597) #20597 2024-01-20 19:54:12 +02:00
json_decode_struct_ptr_test.v json: fix decode struct ptr (#20828) 2024-02-15 06:41:10 +02:00
json_decode_test.v all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
json_decode_with_encode_arg_test.v db, json, time, term: change optional to result (#16201) 2022-10-26 11:26:28 +03:00
json_decode_with_generic_array_test.v checker: fix json decode with generic array of struct (fix #18300) (#18308) 2023-05-30 14:25:33 +02:00
json_decode_with_generic_test.v all: replace generic <> with [] - part 2 (#16536) 2022-11-26 18:23:26 +02:00
json_decode_with_option_arg_test.v all: change optional to option (#16914) 2023-01-09 09:36:45 +03:00
json_decode_with_sumtype_test.v db, json, time, term: change optional to result (#16201) 2022-10-26 11:26:28 +03:00
json_embed_test.v json: fix json encode/decode with embed support (#22277) 2024-09-22 07:54:22 +03:00
json_encode_embed_test.v json: fix json encode/decode with embed support (#22277) 2024-09-22 07:54:22 +03:00
json_encode_enum_test.v fmt: fix alignment of struct init fields (#22025) 2024-08-11 09:11:24 +03:00
json_encode_map_test.v json: fix -cstrict build + optional map (#18014) 2023-04-22 10:55:25 +03:00
json_encode_primite_test.v json: allow decode/encode of alias to primitive type (#18003) 2023-04-21 19:39:40 +03:00
json_encode_recursive_ptr_test.v fmt: fix and simplify align of struct fields (#21995) 2024-08-05 20:23:39 +03:00
json_encode_struct_with_option_field_test.v cgen: fix json encoding of structs with option fields (skip the fields with a value of none) (#16916) 2023-01-09 15:33:08 +02:00
json_encode_sumtype_test.v json: fix option sumtype with int types (#18013) 2023-04-22 10:58:01 +03:00
json_encode_with_mut_test.v fmt: fix alignment of struct init fields (#22025) 2024-08-11 09:11:24 +03:00
json_encode_with_ptr_test.v fmt: fix formating a file in an oscillating manner (fix #22223, fix #22026) (#22232) 2024-09-17 09:47:38 +03:00
json_generic_array_test.v fmt: fix alignment of struct init fields (#22025) 2024-08-11 09:11:24 +03:00
json_i32_test.v fmt: fix alignment of struct init fields (#22025) 2024-08-11 09:11:24 +03:00
json_omitempty_test.v fmt: fix alignment of struct init fields (#22025) 2024-08-11 09:11:24 +03:00
json_omitempty_types_test.v all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
json_option_alias_test.v fmt: fix alignment of struct init fields (#22025) 2024-08-11 09:11:24 +03:00
json_option_none_test.v json: fix option state (#18802) 2023-07-09 08:23:24 +03:00
json_option_raw_test.v all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
json_option_struct_test.v fmt: fix alignment of struct init fields (#22025) 2024-08-11 09:11:24 +03:00
json_option_test.v all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
json_primitives.c.v breaking,vlib: update handling of imports whose symbols are not directly used in imported file, remove pub const is_used = 1 workarounds (#21160) 2024-04-01 22:07:05 +03:00
json_raw_test.v fmt: fix alignment of struct init fields (#22025) 2024-08-11 09:11:24 +03:00
json_struct_option_test.v fmt: fix alignment of struct init fields (#22025) 2024-08-11 09:11:24 +03:00
json_sumtype_test.v json: increase test cases before enabling sumtype decode in all json libraries (#21958) 2024-07-30 03:36:37 +03:00
json_test.v fmt: fix formating a file in an oscillating manner (fix #22223, fix #22026) (#22232) 2024-09-17 09:47:38 +03:00
README.md fmt: fix formating a file in an oscillating manner (fix #22223, fix #22026) (#22232) 2024-09-17 09:47:38 +03:00

Description

The json module provides encoding/decoding of V data structures to/from JSON. For more details, see also the JSON section of the V documentation

Examples

Here is an example of encoding and decoding a V struct with several fields. Note that you can specify different names in the json encoding for the fields, and that you can skip fields too, if needed.

import json

enum JobTitle {
	manager
	executive
	worker
}

struct Employee {
mut:
	name   string
	family string @[json: '-'] // this field will be skipped
	age    int
	salary f32
	title  JobTitle @[json: 'ETitle'] // the key for this field will be 'ETitle', not 'title'
	notes  string   @[omitempty]      // the JSON property is not created if the string is equal to '' (an empty string).
	// TODO: document @[raw]
}

fn main() {
	x := Employee{'Peter', 'Begins', 28, 95000.5, .worker, ''}
	println(x)
	s := json.encode(x)
	println('JSON encoding of employee x: ${s}')
	assert s == '{"name":"Peter","age":28,"salary":95000.5,"ETitle":"worker"}'
	mut y := json.decode(Employee, s)!
	assert y != x
	assert y.family == ''
	y.family = 'Begins'
	assert y == x
	println(y)
	ss := json.encode(y)
	println('JSON encoding of employee y: ${ss}')
	assert ss == s
}