Skip to content

Commit

Permalink
add jsonGetBooleanArray and jsonGetRealArray
Browse files Browse the repository at this point in the history
  • Loading branch information
eine committed Jan 13, 2020
1 parent c8a6f51 commit 3a656e7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
2 changes: 1 addition & 1 deletion data/Boards0.json
Original file line number Diff line number Diff line change
@@ -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"]
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
2 changes: 2 additions & 0 deletions src/JSON.ctx.vhdl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
60 changes: 54 additions & 6 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 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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 3a656e7

Please sign in to comment.