core/encoding/json

encoding_json

Types

30

Error

Error :: enum int { None = 0, EOF = 1, // Not necessarily an error // Tokenizing Errors Illegal_Character = 2, Invalid_Number = 3, String_Not_Terminated = 4, Invalid_String = 5, Invalid_Rune = 6, // Parsing Errors Unexpected_Token = 7, Expected_String_For_Object_Key = 8, Duplicate_Object_Key = 9, Expected_Colon_After_Key = 10, // Allocating Errors Invalid_Allocator = 11, Out_Of_Memory = 12, }Source

Marshal_Options

Marshal_Options :: struct { // output based on spec spec: Specification, // Use line breaks & tabs/spaces pretty: bool, // Use spaces for indentation instead of tabs use_spaces: bool, // Given use_spaces true, use this many spaces per indent level. 0 means 4 spaces. spaces: int, // Output uint as hex in JSON5 & MJSON write_uint_as_hex: bool, // If spec is MJSON and this is true, then keys will be quoted. // // WARNING: If your keys contain whitespace and this is false, then the // output will be bad. mjson_keys_use_quotes: bool, // If spec is MJSON and this is true, then use '=' as delimiter between // keys and values, otherwise ':' is used. mjson_keys_use_equal_sign: bool, // When outputting a map, sort the output by key. // // NOTE: This will temp allocate and sort a list for each map. sort_maps_by_key: bool, // Output enum value's name instead of its underlying value. // // NOTE: If a name isn't found it'll use the underlying value. use_enum_names: bool, // Internal state indentation: int, mjson_skipped_first_braces_start: bool, mjson_skipped_first_braces_end: bool, }Source

careful with MJSON maps & non quotes usage as keys with whitespace will lead to bad results

Match_Error

Match_Error :: enum int { None = 0, Invalid_Argument = 1, Invalid_Type_For_Index = 2, Invalid_Type_For_Key = 3, Key_Not_Found = 4, Out_Of_Bounds_Index = 5, }Source

Specification

Specification :: enum int { JSON = 0, JSON5 = 1, // https://json5.org/ SJSON = 2, // https://bitsquid.blogspot.com/2009/10/simplified-json-notation.html Bitsquid = SJSON, MJSON = SJSON, }Source

Token_Kind

Token_Kind :: enum int { Invalid = 0, EOF = 1, Null = 2, False = 3, True = 4, Infinity = 5, NaN = 6, Ident = 7, Integer = 8, Float = 9, String = 10, Colon = 11, Comma = 12, Open_Brace = 13, Close_Brace = 14, Open_Bracket = 15, Close_Bracket = 16, }Source

Constants

1

Variables

2

_user_marshalers

_user_marshalers :: ^map[typeid]User_MarshalerSource

Example User Marshaler: Custom Marshaler for int Some_Marshaler :: proc(w: io.Writer, v: any, opt: ^json.Marshal_Options) -> json.Marshal_Error {

io.write_string(w, fmt.tprintf("%b", v))
	return json.Marshal_Data_Error.None
}

main :: proc() {
	// Ensure the json._user_marshaler map is initialized
	json.set_user_marshalers(new(map[typeid]json.User_Marshaler))
	reg_err := json.register_user_marshaler(type_info_of(int).id, Some_Marshaler)
	assert(reg_err == .None)


	// Use the custom marshaler
	SomeType :: struct {
		value: int,
	}

	x := SomeType{42}
	data, marshal_err := json.marshal(x)
	assert(marshal_err == nil)
	defer delete(data)

	fmt.println("Custom output:", string(data)) // Custom output: {"value":101010}
}
NOTE(Jeroen): This is a pointer to prevent accidental additions
it is prefixed with `_` rather than marked with a private attribute so that users can access it if necessary

Procedures

50

register_user_unmarshaler

register_user_unmarshaler :: proc(id: typeid, unmarshaler: User_Unmarshaler) -> (Register_User_Unmarshaler_Error)Source

Registers a user-defined unmarshaler for a specific typeid.

WARNING: set_user_unmarshalers must be called before using this procedure.

  • id: The typeid of the custom type.
  • unmarshaler: The User_Unmarshaler function for the custom type.

Example:

import "core:fmt"
import "core:encoding/json"
import "core:strconv"

// Custom Unmarshaler for `int`
some_unmarshaler :: proc(p: ^json.Parser, v: any) -> json.Unmarshal_Error {
	token := p.curr_token.text
	i, ok := strconv.parse_i64_of_base(token, 2)
	if !ok {
		return .Invalid_Data
	}

	(^int)(v.data)^ = int(i)

	json.advance_token(p)
	return nil
}

register_user_unmarshaler_example :: proc() {
	// Ensure the `json._user_unmarshalers` map is initialized.
	json.set_user_unmarshalers(new(map[typeid]json.User_Unmarshaler))
	reg_err := json.register_user_unmarshaler(typeid_of(int), some_unmarshaler)
	assert(reg_err == .None)

	data := `{"value":101010}`
	SomeType :: struct {
		value: int,
	}
	y: SomeType

	unmarshal_err := json.unmarshal(transmute([]byte)data, &y)
	fmt.println(y, unmarshal_err)
}

Output:

SomeType{value = 42} nil

Procedure Groups

2

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.