0

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?

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
  • Is it necessary to send along the type info? JSON already has that baked in: literals true and false are booleans, literals surrounded with double quotes are strings, and numeric literals are of type number. Just curious if that would work for your use case. – Richard Gieg Jan 26 '20 at 10:10
  • @RichardGieg - the idea is that if in the future I will have more data-types. the client won't have to check each if field if it has value, it will know where to look for it. – Roee Gavirel Jan 26 '20 at 11:06

0 Answers0