Skip to content

Commit a9b6e47

Browse files
committed
add jsonGetBooleanArray and jsonGetRealArray
1 parent c8a6f51 commit a9b6e47

File tree

4 files changed

+73
-18
lines changed

4 files changed

+73
-18
lines changed

data/Boards0.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
["test",[true,false,18,null,"hello"],[9,8],3324.34,832432,"world"]
1+
["test",[true,false,18,null,"hello"],[9,8],3324.34,832432,[2.5, 3.33, 5.25],"world"]

examples/Boards_VUnit.vhdl

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,33 @@ end entity;
1414
architecture tb of tb_boards is
1515

1616
procedure test_board0(JSONContent : T_JSON) is
17-
constant img_arr : integer_vector := jsonGetIntegerArray(JSONContent, "2");
18-
constant ref_arr : integer_vector := (9,8);
17+
constant int_arr : integer_vector := jsonGetIntegerArray(JSONContent, "2");
18+
constant bool_arr : boolean_vector := jsonGetBooleanArray(JSONContent, "1");
19+
constant real_arr : real_vector := jsonGetRealArray(JSONContent, "5");
20+
constant int_ref_arr : integer_vector := (9,8);
21+
constant bool_ref_arr : boolean_vector := (true,false);
22+
constant real_ref_arr : real_vector := (2.5, 3.33, 5.25);
1923
begin
2024
assert jsonGetString(JSONContent, "0") = "test" severity failure;
21-
2225
assert jsonGetBoolean(JSONContent, "1/0") severity failure;
2326
assert not jsonGetBoolean(JSONContent, "1/1") severity failure;
2427
assert positive'value(jsonGetString(JSONContent, "1/2")) = 18 severity failure;
2528
assert jsonIsNull(JSONContent, "1/3") severity failure;
2629
assert jsonGetString(JSONContent, "1/4") = "hello" severity failure;
27-
2830
assert jsonGetString(JSONContent, "2/0") = "9" severity failure;
2931
assert jsonGetString(JSONContent, "2/1") = "8" severity failure;
30-
for i in 0 to img_arr'length-1 loop
31-
assert img_arr(i) = ref_arr(i) severity failure;
32-
end loop;
33-
3432
assert real'value(jsonGetString(JSONContent, "3")) = 3324.34 severity failure;
35-
3633
assert natural'value(jsonGetString(JSONContent, "4")) = 832432 severity failure;
37-
38-
assert jsonGetString(JSONContent, "5") = "world" severity failure;
34+
assert jsonGetString(JSONContent, "6") = "world" severity failure;
35+
for i in 0 to int_ref_arr'length-1 loop
36+
check_equal(int_arr(i), int_ref_arr(i));
37+
end loop;
38+
for i in 0 to bool_ref_arr'length-1 loop
39+
check_equal(bool_arr(i), bool_ref_arr(i));
40+
end loop;
41+
for i in 0 to real_ref_arr'length-1 loop
42+
check_equal(real_arr(i), real_ref_arr(i), max_diff => 2.0**(-51));
43+
end loop;
3944
end procedure;
4045

4146
procedure test_board1(JSONContent : T_JSON) is

src/JSON.ctx.vhdl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ context json_ctx is
4444
use JSON.json.jsonGetContent;
4545
use JSON.json.jsonGetBoolean;
4646
use JSON.json.jsonGetString;
47+
use JSON.json.jsonGetBooleanArray;
4748
use JSON.json.jsonGetIntegerArray;
49+
use JSON.json.jsonGetRealArray;
4850
use JSON.json.jsonIsBoolean;
4951
use JSON.json.jsonIsNull;
5052
use JSON.json.jsonIsString;

src/JSON.pkg.vhdl

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,15 @@ package JSON is
105105

106106
function jsonGetBoolean(JSONContext : T_JSON; Path : STRING) return BOOLEAN;
107107
function jsonGetString(JSONContext : T_JSON; Path : STRING) return STRING;
108+
109+
function jsonGetBooleanArray(JSONContext : T_JSON; Path : string) return boolean_vector;
110+
function jsonGetBooleanArray(JSONContext : T_JSON; Path : string; Len : positive) return boolean_vector;
108111
function jsonGetIntegerArray(JSONContext : T_JSON; Path : string) return integer_vector;
109112
function jsonGetIntegerArray(JSONContext : T_JSON; Path : string; Len : positive) return integer_vector;
110-
-- function jsonGetRealArray(JSONContext : T_JSON; Path : string) return real_vector;
113+
function jsonGetRealArray(JSONContext : T_JSON; Path : string) return real_vector;
114+
function jsonGetRealArray(JSONContext : T_JSON; Path : string; Len : positive) return real_vector;
111115

116+
function jsonGetNumberArrayLength(JSONContext : T_JSON; Path : string) return natural;
112117
function jsonIsBoolean(JSONContext : T_JSON; Path : STRING) return BOOLEAN;
113118
function jsonIsNull(JSONContext : T_JSON; Path : STRING) return BOOLEAN;
114119
function jsonIsString(JSONContext : T_JSON; Path : STRING) return BOOLEAN;
@@ -1639,14 +1644,31 @@ package body JSON is
16391644
return (Element.ElementType = ELEM_TRUE);
16401645
end function;
16411646

1642-
-- function to get a integer_vector from the compressed content extracted from a JSON input
1643-
function jsonGetIntegerArray(JSONContext : T_JSON; Path : string) return integer_vector is
1644-
variable len: natural := 0;
1647+
-- function to get a boolean_vector from the compressed content extracted from a JSON input
1648+
function jsonGetBooleanArray(JSONContext : T_JSON; Path : string) return boolean_vector is
1649+
variable len: natural:=0;
16451650
begin
1646-
while jsonIsNumber(JSONContext, Path & "/" & to_string(len)) loop
1651+
while jsonIsBoolean(JSONContext, Path & "/" & to_string(len)) loop
16471652
len := len+1;
16481653
end loop;
1649-
return jsonGetIntegerArray(JSONContext, Path, len);
1654+
return jsonGetBooleanArray(JSONContext, Path, len);
1655+
end function;
1656+
1657+
-- function to get a boolean_vector of a fixed length from the compressed content extracted from a JSON input
1658+
function jsonGetBooleanArray(JSONContext : T_JSON; Path : string; Len : positive) return boolean_vector is
1659+
variable return_value : boolean_vector(Len-1 downto 0);
1660+
begin
1661+
for i in 0 to Len-1 loop
1662+
return_value(i) := boolean'value(jsonGetString(JSONContext, Path & "/" & to_string(i)));
1663+
end loop;
1664+
return return_value;
1665+
end function;
1666+
1667+
-- function to get a integer_vector from the compressed content extracted from a JSON input
1668+
function jsonGetIntegerArray(JSONContext : T_JSON; Path : string) return integer_vector is
1669+
variable len: natural:=0;
1670+
begin
1671+
return jsonGetIntegerArray(JSONContext, Path, jsonGetNumberArrayLength(JSONContext, Path));
16501672
end function;
16511673

16521674
-- 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
16591681
return return_value;
16601682
end function;
16611683

1684+
-- function to get a real_vector from the compressed content extracted from a JSON input
1685+
function jsonGetRealArray(JSONContext : T_JSON; Path : string) return real_vector is
1686+
variable len: natural:=0;
1687+
begin
1688+
return jsonGetRealArray(JSONContext, Path, jsonGetNumberArrayLength(JSONContext, Path));
1689+
end function;
1690+
1691+
-- function to get a real_vector of a fixed length from the compressed content extracted from a JSON input
1692+
function jsonGetRealArray(JSONContext : T_JSON; Path : string; Len : positive) return real_vector is
1693+
variable return_value : real_vector(Len-1 downto 0);
1694+
begin
1695+
for i in 0 to Len-1 loop
1696+
return_value(i) := real'value(jsonGetString(JSONContext, Path & "/" & to_string(i)));
1697+
end loop;
1698+
return return_value;
1699+
end function;
1700+
1701+
function jsonGetNumberArrayLength(JSONContext : T_JSON; Path : string) return natural is
1702+
variable len: natural:=0;
1703+
begin
1704+
while jsonIsNumber(JSONContext, Path & "/" & to_string(len)) loop
1705+
len := len+1;
1706+
end loop;
1707+
return len;
1708+
end function;
1709+
16621710
function jsonIsBoolean(JSONContext : T_JSON; Path : STRING) return BOOLEAN is
16631711
constant ElementIndex : T_UINT16 := jsonGetElementIndex(JSONContext, Path);
16641712
constant Element : T_JSON_INDEX_ELEMENT := JSONContext.Index(ElementIndex);

0 commit comments

Comments
 (0)