From 3321bc15bcbee8b91ed180630dc5e71f39cf4c09 Mon Sep 17 00:00:00 2001 From: Daniel Margarido Date: Fri, 3 Jan 2025 20:50:05 +0000 Subject: [PATCH 1/3] Added example of loading a json file with unmarshal --- json/load_json_unmarshal/game_settings.json | 13 ++++++ .../load_json_unmarshal.odin | 41 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 json/load_json_unmarshal/game_settings.json create mode 100644 json/load_json_unmarshal/load_json_unmarshal.odin diff --git a/json/load_json_unmarshal/game_settings.json b/json/load_json_unmarshal/game_settings.json new file mode 100644 index 0000000..adc8abe --- /dev/null +++ b/json/load_json_unmarshal/game_settings.json @@ -0,0 +1,13 @@ +{ + + "window_width": 1920, + "window_height": 1080, + "window_title": "My Game!", + + "rendering_api": "opengl", + "renderer_settings": { + "msaa": false, + "depth_testing": true + } + +} \ No newline at end of file diff --git a/json/load_json_unmarshal/load_json_unmarshal.odin b/json/load_json_unmarshal/load_json_unmarshal.odin new file mode 100644 index 0000000..0d04336 --- /dev/null +++ b/json/load_json_unmarshal/load_json_unmarshal.odin @@ -0,0 +1,41 @@ +package load_json_unmarshal + +import "core:os" +import "core:fmt" +import "core:encoding/json" + +import "core:mem" + +Game_Settings :: struct { + window_width: i32, + window_height: i32, + window_title: string, + rendering_api: string, + renderer_settings: struct{ + msaa: bool, + depth_testing: bool, + }, +} + +main :: proc(){ + // Load in your json file! + data, ok := os.read_entire_file_from_filename("game_settings.json") + if !ok { + fmt.eprintln("Failed to load the file!") + return + } + defer delete(data) // Free the memory at the end + + // Load data from the json bytes directly to the struct + settings: Game_Settings + unmarshal_err := json.unmarshal(data, &settings) + if unmarshal_err != nil { + fmt.eprintln("Failed to unmarshal the file!") + return + } + fmt.eprintf("Result %v\n", settings) + + // Clear allocated strings + delete(settings.window_title) + delete(settings.rendering_api) +} \ No newline at end of file From 8ec5033c0ed718da02d7e1d47a9b98387d7bd461 Mon Sep 17 00:00:00 2001 From: Daniel Margarido Date: Fri, 3 Jan 2025 20:55:50 +0000 Subject: [PATCH 2/3] Added check in the github action for the load_json_unmarshal --- .github/workflows/check.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 44bb4af..43a7f7d 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -45,6 +45,7 @@ jobs: odin check glfw/window $FLAGS odin check json/load_json $FLAGS + odin check json/load_json_unmarshal $FLAGS odin check json/odin_info $FLAGS odin check learn_metal/00-window $FLAGS From 2977351e9c274e0a7c186334eb93d6428486d222 Mon Sep 17 00:00:00 2001 From: Daniel Margarido Date: Sat, 4 Jan 2025 22:36:28 +0000 Subject: [PATCH 3/3] Updated to use tabs instead of spaces for indentation --- .../load_json_unmarshal.odin | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/json/load_json_unmarshal/load_json_unmarshal.odin b/json/load_json_unmarshal/load_json_unmarshal.odin index 0d04336..817aa2d 100644 --- a/json/load_json_unmarshal/load_json_unmarshal.odin +++ b/json/load_json_unmarshal/load_json_unmarshal.odin @@ -4,21 +4,19 @@ import "core:os" import "core:fmt" import "core:encoding/json" -import "core:mem" - Game_Settings :: struct { - window_width: i32, - window_height: i32, - window_title: string, - rendering_api: string, - renderer_settings: struct{ - msaa: bool, - depth_testing: bool, - }, + window_width: i32, + window_height: i32, + window_title: string, + rendering_api: string, + renderer_settings: struct{ + msaa: bool, + depth_testing: bool, + }, } main :: proc(){ - // Load in your json file! + // Load in your json file! data, ok := os.read_entire_file_from_filename("game_settings.json") if !ok { fmt.eprintln("Failed to load the file!") @@ -26,16 +24,16 @@ main :: proc(){ } defer delete(data) // Free the memory at the end - // Load data from the json bytes directly to the struct - settings: Game_Settings - unmarshal_err := json.unmarshal(data, &settings) - if unmarshal_err != nil { - fmt.eprintln("Failed to unmarshal the file!") - return - } - fmt.eprintf("Result %v\n", settings) + // Load data from the json bytes directly to the struct + settings: Game_Settings + unmarshal_err := json.unmarshal(data, &settings) + if unmarshal_err != nil { + fmt.eprintln("Failed to unmarshal the file!") + return + } + fmt.eprintf("Result %v\n", settings) - // Clear allocated strings - delete(settings.window_title) - delete(settings.rendering_api) + // Clear allocated strings + delete(settings.window_title) + delete(settings.rendering_api) } \ No newline at end of file