json - REST API status as integer or as string? -


me , colleague working on rest api. we've been arguing quite lot whether status of resource/item should a string or integer---we both need read, understand , modify resource (using separate applications). general subject, google did not settle argument. wonder experience , way better.

for example, let's have job resource, accesible through uri http://example.com/api/jobs/someid , has following json representation stored in nosql db:

job a: {    "id": "someid",    "name": "somename",    "status": "finished"  // or "created", "failed", "compile_error" } 

so question - maybe should more following?

job b: {    "id": "someid",    "name": "somename",    "status": 0  // or 1, 2, 3, ... } 

in both cases each of have create map, use make sense of status in our application logic. myself leaning towards first one, far more readable... can mix '0' (string) , 0 (number).

however, api consumed machines, readability not important. using numbers has other advantages - accepted when working applications in console , can beneficial when want include arbitrary new failed statuses, say:

  • status == 50 - means have problem network component x,
  • status > 100 - means multiple special cases.

when have numbers, don't need make string names them. way best in opinion? maybe need multiple fields (this make matters bit confusing):

job c: {    "id": "someid",    "name": "somename",    "status": 0, // or 1, 2, 3...    "error_type": "compile_error",    "error_message": "you coding skill has failed. please go away" } 

personally @ handling situation combination of both approaches have mentioned. store statuses integers within database, create enumeration or class of constants map status names numeric status values.

for example (in c#):

public enum statustype {     created = 0,     failed = 1,     compile_error = 2,      // add further statuses here. } 

you convert numeric status stored in database instance of enumeration, , use decision making throughout code.

for example (in c#):

statustype status = (statustype) storedstatus;  if(status == statustype.created) {     // status created. } else {    // handle other statuses here. } 

if you're being pedantic, store these mappings in db.

for access via api, go either way depending on requirements. return result both status number , status text:

object yourobject {     status_code = 0,     status = "failed" } 

you create api retrieve status name code. returning both status code , name in api best performance standpoint.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -