json/load_json

Code

load_json.odin ¶
50 linesSource

1package load_json
2
3import "core:fmt"
4import "core:encoding/json"
5
6import "core:os"
7
8main :: proc() {
9	// Load in your json file!
10	data, read_err := os.read_entire_file("game_settings.json", context.allocator)
11	if read_err != nil {
12		fmt.eprintfln("Failed to load the file: %v", read_err)
13		return
14	}
15	defer delete(data) // Free the memory at the end
16	
17	// Parse the json file.
18	json_data, err := json.parse(data)
19	if err != .None {
20		fmt.eprintln("Failed to parse the json file.")
21		fmt.eprintln("Error:", err)
22		return
23	}
24	defer json.destroy_value(json_data)
25
26	// Access the Root Level Object
27	root := json_data.(json.Object)
28
29	fmt.println("Root:")
30	fmt.println(
31		"window_width:",
32		root["window_width"],
33		"window_height:",
34		root["window_height"],
35		"window_title:",
36		root["window_title"],
37	)
38	fmt.println("rendering_api:", root["rendering_api"])
39
40	// Store the value.
41	window_width := root["window_width"].(json.Float)
42	fmt.println("window_width:", window_width)
43
44	fmt.println("")
45
46	fmt.println("Renderer Settings:")
47	renderer_settings := root["renderer_settings"].(json.Object)
48	fmt.println("msaa:", renderer_settings["msaa"])
49	fmt.println("depth_testing:", renderer_settings["depth_testing"])
50}

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 8