From 3a656e7b049f365e302d30b07566a266d282d6db Mon Sep 17 00:00:00 2001 From: eine Date: Mon, 13 Jan 2020 01:41:52 +0100 Subject: [PATCH] add jsonGetBooleanArray and jsonGetRealArray --- data/Boards0.json | 2 +- examples/Boards_VUnit.vhdl | 27 ++++++++++------- src/JSON.ctx.vhdl | 2 ++ src/JSON.pkg.vhdl | 60 ++++++++++++++++++++++++++++++++++---- 4 files changed, 73 insertions(+), 18 deletions(-) diff --git a/data/Boards0.json b/data/Boards0.json index 3aa5dd15..c5c3db6b 100644 --- a/data/Boards0.json +++ b/data/Boards0.json @@ -1 +1 @@ - ["test",[true,false,18,null,"hello"],[9,8],3324.34,832432,"world"] + ["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..3abe418c 100644 --- a/src/JSON.ctx.vhdl +++ b/src/JSON.ctx.vhdl @@ -44,7 +44,9 @@ 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.jsonIsBoolean; use JSON.json.jsonIsNull; use JSON.json.jsonIsString; diff --git a/src/JSON.pkg.vhdl b/src/JSON.pkg.vhdl index 11183c6c..e82e0a23 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 jsonGetNumberArrayLength(JSONContext : T_JSON; Path : string) 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,14 +1644,31 @@ package body JSON is return (Element.ElementType = ELEM_TRUE); end function; - -- 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; + -- 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 + variable len: natural:=0; begin - while jsonIsNumber(JSONContext, Path & "/" & to_string(len)) loop + while jsonIsBoolean(JSONContext, Path & "/" & to_string(len)) loop len := len+1; end loop; - return jsonGetIntegerArray(JSONContext, Path, len); + return jsonGetBooleanArray(JSONContext, Path, len); + end function; + + -- 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; + + -- 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 + return jsonGetIntegerArray(JSONContext, Path, jsonGetNumberArrayLength(JSONContext, Path)); end function; -- function to get a integer_vector of a fixed length from the compressed content extracted from a JSON input @@ -1659,6 +1681,32 @@ package body JSON is return return_value; end function; + -- 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 + variable len: natural:=0; + begin + return jsonGetRealArray(JSONContext, Path, jsonGetNumberArrayLength(JSONContext, Path)); + end function; + + -- 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; + + function jsonGetNumberArrayLength(JSONContext : T_JSON; Path : string) return natural is + variable len: natural:=0; + begin + while jsonIsNumber(JSONContext, Path & "/" & to_string(len)) loop + len := len+1; + end loop; + return len; + end function; + function jsonIsBoolean(JSONContext : T_JSON; Path : STRING) return BOOLEAN is constant ElementIndex : T_UINT16 := jsonGetElementIndex(JSONContext, Path); constant Element : T_JSON_INDEX_ELEMENT := JSONContext.Index(ElementIndex);