odin-blend2d

Odin bindings to Blend2D
Log | Files | Refs | README | LICENSE

json_helpers.odin (2108B)


      1 package bindgen
      2 
      3 import "core:encoding/json"
      4 import "core:strings"
      5 import "core:strconv"
      6 
      7 _ :: strings
      8 _ :: strconv
      9 
     10 json_check_bool :: proc(v: json.Value, key: string) -> bool {
     11 	val, _ := json_get(v, key, json.Boolean)
     12 	return val
     13 }
     14 
     15 json_get_string :: proc(v: json.Value, key: string) -> (str: string, ok: bool) {
     16 	return json_get(v, key, json.String)
     17 }
     18 
     19 json_get_array :: proc(v: json.Value, key: string) -> (arr: json.Array, ok: bool) {
     20 	return json_get(v, key, json.Array)
     21 }
     22 
     23 json_get_int :: proc(v: json.Value, key: string) -> (res: int, ok: bool) {
     24 	i, i_ok := json_get(v, key, json.Integer)
     25 	return int(i), i_ok
     26 }
     27 
     28 json_get_object :: proc(v: json.Value, key: string) -> (arr: json.Object, ok: bool) {
     29 	return json_get(v, key, json.Object)
     30 }
     31 
     32 json_get :: proc(v: json.Value, key: string, $T: typeid) -> (res: T, ok: bool) {
     33 	key_iter := key
     34 	cur := v
     35 
     36 	for k in strings.split_iterator(&key_iter, ".") {
     37 		is_index := false
     38 		index: int
     39 		if i, i_ok := strconv.parse_int(k, 10); i_ok {
     40 			is_index = true
     41 			index = i
     42 		}
     43 
     44 		if is_index {
     45 			if arr, is_arr := cur.(json.Array); is_arr {
     46 				if index < 0 || index >= len(arr) {
     47 					return {}, false
     48 				}
     49 
     50 				cur = arr[index]
     51 			} else {
     52 				return {}, false
     53 			}
     54 		} else {
     55 			if obj, is_obj := cur.(json.Object); is_obj {
     56 				if child, child_ok := obj[k]; child_ok {
     57 					cur = child
     58 				} else {
     59 					return {}, false
     60 				}
     61 			} else {
     62 				return {}, false
     63 			}
     64 		}
     65 	}
     66 
     67 	return cur.(T)
     68 }
     69 
     70 json_has :: proc(v: json.Value, key: string) -> (ok: bool) {
     71 	key_iter := key
     72 	cur := v
     73 
     74 	for k in strings.split_iterator(&key_iter, ".") {
     75 		is_index := false
     76 		index: int
     77 		if i, i_ok := strconv.parse_int(k, 10); i_ok {
     78 			is_index = true
     79 			index = i
     80 		}
     81 
     82 		if is_index {
     83 			if arr, is_arr := cur.(json.Array); is_arr {
     84 				if index < 0 || index >= len(arr) {
     85 					return false
     86 				}
     87 
     88 				cur = arr[index]
     89 			} else {
     90 				return false
     91 			}
     92 		} else {
     93 			if obj, is_obj := cur.(json.Object); is_obj {
     94 				if child, child_ok := obj[k]; child_ok {
     95 					cur = child
     96 				} else {
     97 					return false
     98 				}
     99 			} else {
    100 				return false
    101 			}
    102 		}
    103 	}
    104 
    105 	return true
    106 }