diff --git a/data/Boards0b.json b/data/Boards0b.json new file mode 100644 index 00000000..c5c3db6b --- /dev/null +++ b/data/Boards0b.json @@ -0,0 +1 @@ + ["test",[true,false,18,null,"hello"],[9,8],3324.34,832432,[2.5, 3.33, 5.25],"world"] diff --git a/examples/Boards_VUnit.vhdl b/examples/Boards_VUnit.vhdl index 4fd8de96..a49dd8c0 100644 --- a/examples/Boards_VUnit.vhdl +++ b/examples/Boards_VUnit.vhdl @@ -14,28 +14,33 @@ end entity; architecture tb of tb_boards is procedure test_board0(JSONContent : T_JSON) is - constant img_arr : integer_vector := jsonGetIntegerArray(JSONContent, "2"); - constant ref_arr : integer_vector := (9,8); + constant int_arr : integer_vector := jsonGetIntegerArray(JSONContent, "2"); + constant bool_arr : boolean_vector := jsonGetBooleanArray(JSONContent, "1"); + constant real_arr : real_vector := jsonGetRealArray(JSONContent, "5"); + constant int_ref_arr : integer_vector := (9,8); + constant bool_ref_arr : boolean_vector := (true,false); + constant real_ref_arr : real_vector := (2.5, 3.33, 5.25); begin assert jsonGetString(JSONContent, "0") = "test" severity failure; - assert jsonGetBoolean(JSONContent, "1/0") severity failure; assert not jsonGetBoolean(JSONContent, "1/1") severity failure; assert positive'value(jsonGetString(JSONContent, "1/2")) = 18 severity failure; assert jsonIsNull(JSONContent, "1/3") severity failure; assert jsonGetString(JSONContent, "1/4") = "hello" severity failure; - assert jsonGetString(JSONContent, "2/0") = "9" severity failure; assert jsonGetString(JSONContent, "2/1") = "8" severity failure; - for i in 0 to img_arr'length-1 loop - assert img_arr(i) = ref_arr(i) severity failure; - end loop; - assert real'value(jsonGetString(JSONContent, "3")) = 3324.34 severity failure; - assert natural'value(jsonGetString(JSONContent, "4")) = 832432 severity failure; - - assert jsonGetString(JSONContent, "5") = "world" severity failure; + assert jsonGetString(JSONContent, "6") = "world" severity failure; + for i in 0 to int_ref_arr'length-1 loop + check_equal(int_arr(i), int_ref_arr(i)); + end loop; + for i in 0 to bool_ref_arr'length-1 loop + check_equal(bool_arr(i), bool_ref_arr(i)); + end loop; + for i in 0 to real_ref_arr'length-1 loop + check_equal(real_arr(i), real_ref_arr(i), max_diff => real_ref_arr(i)*2.0**(-52)); + end loop; end procedure; procedure test_board1(JSONContent : T_JSON) is diff --git a/src/JSON.ctx.vhdl b/src/JSON.ctx.vhdl index ca401e9a..90078729 100644 --- a/src/JSON.ctx.vhdl +++ b/src/JSON.ctx.vhdl @@ -44,7 +44,10 @@ context json_ctx is use JSON.json.jsonGetContent; use JSON.json.jsonGetBoolean; use JSON.json.jsonGetString; + use JSON.json.jsonGetBooleanArray; use JSON.json.jsonGetIntegerArray; + use JSON.json.jsonGetRealArray; + use JSON.json.jsonGetArrayLength; use JSON.json.jsonIsBoolean; use JSON.json.jsonIsNull; use JSON.json.jsonIsString; diff --git a/src/JSON.pkg.vhdl b/src/JSON.pkg.vhdl index 11183c6c..d01d2385 100644 --- a/src/JSON.pkg.vhdl +++ b/src/JSON.pkg.vhdl @@ -105,10 +105,15 @@ package JSON is function jsonGetBoolean(JSONContext : T_JSON; Path : STRING) return BOOLEAN; function jsonGetString(JSONContext : T_JSON; Path : STRING) return STRING; + + function jsonGetBooleanArray(JSONContext : T_JSON; Path : string) return boolean_vector; + function jsonGetBooleanArray(JSONContext : T_JSON; Path : string; Len : positive) return boolean_vector; function jsonGetIntegerArray(JSONContext : T_JSON; Path : string) return integer_vector; function jsonGetIntegerArray(JSONContext : T_JSON; Path : string; Len : positive) return integer_vector; --- function jsonGetRealArray(JSONContext : T_JSON; Path : string) return real_vector; + function jsonGetRealArray(JSONContext : T_JSON; Path : string) return real_vector; + function jsonGetRealArray(JSONContext : T_JSON; Path : string; Len : positive) return real_vector; + function jsonGetArrayLength(JSONContext : T_JSON; Path : string; IsType: character) return natural; function jsonIsBoolean(JSONContext : T_JSON; Path : STRING) return BOOLEAN; function jsonIsNull(JSONContext : T_JSON; Path : STRING) return BOOLEAN; function jsonIsString(JSONContext : T_JSON; Path : STRING) return BOOLEAN; @@ -1639,15 +1644,27 @@ package body JSON is return (Element.ElementType = ELEM_TRUE); end function; + -- function to get a boolean_vector from the compressed content extracted from a JSON input + function jsonGetBooleanArray(JSONContext : T_JSON; Path : string) return boolean_vector is + begin + return jsonGetBooleanArray(JSONContext, Path, jsonGetArrayLength(JSONContext, Path, 'b')); + end; + + -- function to get a boolean_vector of a fixed length from the compressed content extracted from a JSON input + function jsonGetBooleanArray(JSONContext : T_JSON; Path : string; Len : positive) return boolean_vector is + variable return_value : boolean_vector(Len-1 downto 0); + begin + for i in 0 to Len-1 loop + return_value(i) := boolean'value(jsonGetString(JSONContext, Path & "/" & to_string(i))); + end loop; + return return_value; + end; + -- function to get a integer_vector from the compressed content extracted from a JSON input function jsonGetIntegerArray(JSONContext : T_JSON; Path : string) return integer_vector is - variable len: natural := 0; begin - while jsonIsNumber(JSONContext, Path & "/" & to_string(len)) loop - len := len+1; - end loop; - return jsonGetIntegerArray(JSONContext, Path, len); - end function; + return jsonGetIntegerArray(JSONContext, Path, jsonGetArrayLength(JSONContext, Path, 'i')); + end; -- function to get a integer_vector of a fixed length from the compressed content extracted from a JSON input function jsonGetIntegerArray(JSONContext : T_JSON; Path : string; Len : positive) return integer_vector is @@ -1657,7 +1674,37 @@ package body JSON is return_value(i) := to_natural_dec(jsonGetString(JSONContext, Path & "/" & to_string(i))); end loop; return return_value; - end function; + end; + + -- function to get a real_vector from the compressed content extracted from a JSON input + function jsonGetRealArray(JSONContext : T_JSON; Path : string) return real_vector is + begin + return jsonGetRealArray(JSONContext, Path, jsonGetArrayLength(JSONContext, Path, 'r')); + end; + + -- function to get a real_vector of a fixed length from the compressed content extracted from a JSON input + function jsonGetRealArray(JSONContext : T_JSON; Path : string; Len : positive) return real_vector is + variable return_value : real_vector(Len-1 downto 0); + begin + for i in 0 to Len-1 loop + return_value(i) := real'value(jsonGetString(JSONContext, Path & "/" & to_string(i))); + end loop; + return return_value; + end; + + function jsonGetArrayLength(JSONContext : T_JSON; Path : string; IsType: character) return natural is + variable len: natural:=0; + variable val: boolean:=true; + begin + while(val) loop + case IsType is + when 'b' => val := jsonIsBoolean(JSONContext, Path & "/" & to_string(len)); + when others => val := jsonIsNumber(JSONContext, Path & "/" & to_string(len)); + end case; + len := len+1; + end loop; + return len-1; + end; function jsonIsBoolean(JSONContext : T_JSON; Path : STRING) return BOOLEAN is constant ElementIndex : T_UINT16 := jsonGetElementIndex(JSONContext, Path); diff --git a/tests/VUnit/run.py b/tests/VUnit/run.py index bdcf5dc2..b673a804 100644 --- a/tests/VUnit/run.py +++ b/tests/VUnit/run.py @@ -33,7 +33,7 @@ def b16enc(s): tb.get_tests("stringified*")[0].set_generic("tb_cfg", gen_str) tb.get_tests("b16encoded stringified*")[0].set_generic("tb_cfg", b16enc(gen_str)) -tb.get_tests("JSON file*")[0].set_generic("tb_cfg", join(root, 'data', 'Boards0.json')) -tb.get_tests("b16encoded JSON file*")[0].set_generic("tb_cfg", b16enc(join(root, 'data', 'Boards0.json'))) +tb.get_tests("JSON file*")[0].set_generic("tb_cfg", join(root, 'data', 'Boards0b.json')) +tb.get_tests("b16encoded JSON file*")[0].set_generic("tb_cfg", b16enc(join(root, 'data', 'Boards0b.json'))) vu.main()