Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Yes" or specific string map to boolean type? #21

Open
Junywing opened this issue Apr 5, 2013 · 2 comments
Open

"Yes" or specific string map to boolean type? #21

Junywing opened this issue Apr 5, 2013 · 2 comments

Comments

@Junywing
Copy link

Junywing commented Apr 5, 2013

I have JSON like this

"login_success" : "Yes",
"isAdult": "N",
...

and

I want to mapping the result to below property
property BOOL bSuccess;
property BOOL isAudlt;

currently, the result map to the NSString property and then call method if the string is "YES" , bSuccess = YES...

is there any easy way?

@zcharter
Copy link

zcharter commented Apr 8, 2013

There's no way to automatically convert meaningful (but arbitrary) strings into BOOLs. However, you can use the mapping callback to handle this yourself quite easily.

- (void)didMapObjectFromJSON:(id<JTValidJSONResponse>)object {
    NSString *loginSuccessString = object[@"login_success"];
    NSString *isAdultString = object[@"isAdult"];

    BOOL isLoginSuccessful = [loginSuccessString isEqualToString:@"Yes"];
    BOOL isAdult = [isAdultString isEqualToString:@"isAdultString"];
}

You can write your own method to convert strings to BOOLs however you want, but be sure to handle the situation where the key may be missing.

@jamztang
Copy link
Owner

jamztang commented Apr 9, 2013

Thanks @zcharter. You can also write your own stringToBoolean mapping class which conforms to JTValidMappingKey and transform that string to your expected value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants