0

I have hired a iOS developer to create an app which will be backed by the REST API. Now I'm stuck with a problem with one output.

There are Public and Private Groups, if group is Private, the API will return following in json format:

privacy":{"value":"1"}, 

and if the group is Public, the API will return following in json format:

"privacy":[]

The iOS developer says that this output is incorrect while on the other hand API developer believe this is correct output. Can anyone please tell me is this output correct to be used in iOS app or it's not correct?

iOS Developer days he can't compare String and Array.

2
  • Yes its correct , iOS developer can check response Object Class and parse accordingly. Not a big issue. This could also be fixed by webdeveloper by using the same response format , i mean array in both case Commented Apr 18, 2016 at 13:03
  • 'correct', 'good idea' and 'good design' are 3 often very different things. this situation can be argued under all 3 of those definitions and all are appropriate. that is a 'bad' API response, it isn't obvious or easy to use, but it is usable... Commented Apr 18, 2016 at 13:10

3 Answers 3

3

Yes it's correct, given there is no such thing as incorrect with JSON, as there is no schema to conform to. As long as it's legal, it's OK.

The iOS developer can test the type of the "privacy" value after it's been deserialised:

id value = jsonDict[@"privacy"];
if ([value isKindOfClass:[NSDictionary class]]) {
    // Value is dictionary
    NSDictionary *dictValue = (NSDictionary *)value;
    NSString *number = dictValue[@"value"];   // This should be a number, not a string!
} else if ([value isKindOfClass:[NSArray class]]) {
    // Value is array

} else {
    // Value is illegal. Report error.
}

I will say that it should be:

{"value":1}

as 1 is a number, not a string.

Sign up to request clarification or add additional context in comments.

Comments

2

Yes, iOS developer can check response.
But there should be consistency in JSON response.
It is not correct way that one API give response in array and other in dictionary.
It should be either array or string that would be preferable for iOS developer.

Output should be:

{  
    "privacy":[{"value":1}]  
}

For validating JSON response you can use http://jsonlint.com/

2 Comments

I think it's reasonable to assume there is inconsistency in web responses anyway, regardless of the format used. Imagine a system-wide error response which is a dictionary with an "error" key. The system can return a whole host of different responses depending on the request, but all responses must be tested to see if it's an error response. This is a pretty common scenario.
@trojanfoe Yes, your concern is also reasonable.
0

The API is designed incorrectly, as it provides various data types for the privacy key (and no schema defines how this should behave). Once it's a dictionary, once it's an empty array. I'd suggest using an array in any case.

Private: privacy : [ {"value" : true} ]

Public: privacy : []

However, it's possible to concatenate array to string and then compare with string (using let stringRepresentation = ",".join(array))

1 Comment

I'd say it's a badly designed API, but it's not incorrect, as @trojanfoe mentions in his answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.