Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add jsonGetBooleanArray and jsonGetRealArray #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/Boards0b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["test",[true,false,18,null,"hello"],[9,8],3324.34,832432,[2.5, 3.33, 5.25],"world"]
27 changes: 16 additions & 11 deletions examples/Boards_VUnit.vhdl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/JSON.ctx.vhdl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
63 changes: 55 additions & 8 deletions src/JSON.pkg.vhdl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/VUnit/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()