Commit eab35bd4 authored by Botond Baranyi's avatar Botond Baranyi
Browse files

Extended JSON attribute 'default' to work on empty structures (bug 529443)



Change-Id: Ied956a88e9296a235ea565883aff2e63f74da759
Signed-off-by: Botond Baranyi's avatarBotond Baranyi <botond.baranyi@ericsson.com>
parent 1aa91988
......@@ -3142,6 +3142,21 @@ namespace Common {
err = true;
}
break;
case T_SEQ_T:
case T_SET_T:
if (last->get_nof_comps() != 0) {
error("JSON default values are not available for record/set types with "
"1 or more fields");
break;
}
// else fall through
case T_SEQOF:
case T_SETOF:
if (dval_len != 2 || dval[0] != '{' || dval[1] != '}') {
error("Invalid JSON default value for type `%s'. Only the empty array "
"is allowed.", last->get_typename().c_str());
}
break;
default:
error("JSON default values are not available for type `%s'",
last->get_stringRepr().c_str());
......
......@@ -6539,8 +6539,13 @@ static void defEmptyRecordClass(const struct_def *sdef,
// JSON decode, RT1
src = mputprintf(src,
"int %s::JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer& p_tok, boolean p_silent)\n"
"int %s::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)\n"
"{\n"
" if (NULL != p_td.json->default_value && 0 == p_tok.get_buffer_length()) {\n"
// use the default value
" bound_flag = TRUE;\n"
" return strlen(p_td.json->default_value);\n"
" }\n"
" json_token_t token = JSON_TOKEN_NONE;\n"
" size_t dec_len = p_tok.get_next_token(&token, NULL, NULL);\n"
" if (JSON_TOKEN_ERROR == token) {\n"
......
......@@ -1569,6 +1569,12 @@ void defRecordOfClass1(const struct_of_def *sdef, output_struct *output)
src = mputprintf(src,
"int %s::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)\n"
"{\n"
" if (NULL != p_td.json->default_value && 0 == p_tok.get_buffer_length()) {\n"
// use the default value (currently only the empty array can be set as
// default value for this type)
" set_size(0);\n"
" return strlen(p_td.json->default_value);\n"
" }\n"
" json_token_t token = JSON_TOKEN_NONE;\n"
" size_t dec_len = p_tok.get_next_token(&token, NULL, NULL);\n"
" if (JSON_TOKEN_ERROR == token) {\n"
......
......@@ -1589,6 +1589,12 @@ int Record_Of_Type::JSON_encode_negtest(const Erroneous_descriptor_t* p_err_desc
int Record_Of_Type::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)
{
if (NULL != p_td.json->default_value && 0 == p_tok.get_buffer_length()) {
// use the default value (currently only the empty array can be set as
// default value for this type)
set_size(0);
return strlen(p_td.json->default_value);
}
json_token_t token = JSON_TOKEN_NONE;
size_t dec_len = p_tok.get_next_token(&token, NULL, NULL);
if (JSON_TOKEN_ERROR == token) {
......@@ -7306,8 +7312,13 @@ int Empty_Record_Type::JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&
p_tok.put_next_token(JSON_TOKEN_OBJECT_END, NULL);
}
int Empty_Record_Type::JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer& p_tok, boolean p_silent)
int Empty_Record_Type::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)
{
if (NULL != p_td.json->default_value && 0 == p_tok.get_buffer_length()) {
// use the default value
bound_flag = TRUE;
return strlen(p_td.json->default_value);
}
json_token_t token = JSON_TOKEN_NONE;
size_t dec_len = p_tok.get_next_token(&token, NULL, NULL);
if (JSON_TOKEN_ERROR == token) {
......
......@@ -504,6 +504,23 @@ testcase tc_attribute_as_number_negtest() runs on MTC {
setverdict(pass);
}
// Testing the attribute 'default' on structures
testcase tc_attribute_default_struct() runs on MTC {
var DefaultEmptyStructs val;
var DefaultEmptyStructs exp_val := { {}, {}, {}, {} };
var universal charstring data := "{}";
var integer res := decvalue_unichar(data, val);
if (res != 0 or lengthof(data) != 0) {
setverdict(fail, "Decoding failed. Result: ", res, ", remaining JSON data: ", data);
}
else if (val != exp_val) {
setverdict(fail, "Invalid decoded value. Expected: ", exp_val, ", got: ", val);
}
else {
setverdict(pass);
}
}
control {
execute(tc_NoAttributeOnUpperLevel())
......@@ -529,5 +546,6 @@ control {
execute(tc_attribute_as_value_fields());
execute(tc_attribute_as_number());
execute(tc_attribute_as_number_negtest());
execute(tc_attribute_default_struct());
}
}
......@@ -305,6 +305,19 @@ type union Uni522660 {
integer f1
}
type record DefaultEmptyStructs {
record { } r,
set { } s,
record of integer ro,
set of charstring so
}
with {
variant (r) "JSON: default({})";
variant (s) "JSON: default({})";
variant (ro) "JSON: default({})";
variant (so) "JSON: default({})";
}
} with {
encode "JSON";
extension "anytype integer, charstring, R, RoI, Thing";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment