json/load_json_unmarshal

Code

load_json_unmarshal.odin ¶
39 linesSource

1package load_json_unmarshal
2
3import "core:os"
4import "core:fmt"
5import "core:encoding/json"
6
7Game_Settings :: struct {
8	window_width: i32,
9	window_height: i32,
10	window_title: string,
11	rendering_api: string,
12	renderer_settings: struct{
13		msaa: bool,
14		depth_testing: bool,
15	},
16}
17
18main :: proc(){
19	// Load in your json file!
20	data, read_err := os.read_entire_file("game_settings.json", context.allocator)
21	if read_err != nil {
22		fmt.eprintfln("Failed to load the file: %v", read_err)
23		return
24	}
25	defer delete(data) // Free the memory at the end
26
27	// Load data from the json bytes directly to the struct
28	settings: Game_Settings
29	unmarshal_err := json.unmarshal(data, &settings)
30	if unmarshal_err != nil {
31		fmt.eprintln("Failed to unmarshal the file!")
32		return
33	}
34	fmt.eprintf("Result %v\n", settings)
35
36	// Clear allocated strings
37	delete(settings.window_title)
38	delete(settings.rendering_api)
39}

Other Files

game_settings.json ¶
13 linesSource

{
  
  "window_width": 1920,
  "window_height": 1080,
  "window_title": "My Game!",

  "rendering_api": "opengl",
  "renderer_settings": {
    "msaa": false,
    "depth_testing": true
  }

}

Declarations Used 5