I have a model in the DB with the following structure
- id
- key
- value_type (can be 'String', 'Integer' and 'Boolean')
- string_value
- int_value
- bool_value
Where only one of string_value
, int_value
or bool_value
have value while the other 2 are nulls depends on the value of value_type
. for example:
id:1, key:'key1', value_type:'String', string_value:'random string', int_value:null, bool_value: null
id:2, key:'key2', value_type:'Integer', string_value:NULL, int_value:777, bool_value:NULL
id:3, key:'key3', value_type:'Boolean', string_value:NULL, int_value:NULL, bool_value:false
I want to open an endpoint to allow GET for entities of this type and consider the two following JSON structure for the return object:
Option 1:
{
id:<id>,
key:<key>,
value_type:<value_type>,
value:<a string representation of the value>
}
for instance:
{
id:2,
key:"key2",
value_type:"Integer",
value:"777"
}
Option 2:
{
id:<id>,
key:<key>,
value_type:<value_type>,
string_value:<string_value>,
integer_value:<integer_value>,
boolean_value:<boolean_value>
}
for instance:
{
id:2,
key:"key2",
value_type:"Integer",
string_value:null,
integer_value:777
boolean_value:null
}
The first option is cleaner while the second one is more typesafe. Is there any standard or consideration to take before making a choice?