-1

I am experiencing an interesting issue regarding JSONModel and the conversion of the string T to a BOOl. I am getting passed a string value T in the JSON and need to convert it to a BOOL.

Using JSONModel, the conversion works on 64-bit devices, correctly converting T to a 1 (obviously some magic going on here). However, on a 32-bit device, the T is incorrectly converted to a 0. I am having trouble figuring out why this is happening and how to fix it.

I know that the JSONValueTransformer BOOLFromNSString is called for 32-bit devices, and returns NO, but it is not called for 64-bit devices.

Does anyone know more about the 32-bit/64-bit architecture and why this would happen? How can this be fixed so the correct BOOL value is returned on 32-bit and 64-bit devices?

tentmaking
  • 2,076
  • 4
  • 30
  • 53
  • I'm confused. What does the character `'T'` have to do with JSON or BOOL? – Hot Licks Nov 04 '14 at 18:51
  • The 'T' stand for True or YES. That is what is getting passed to me in the JSON. – tentmaking Nov 04 '14 at 19:10
  • `'T'` does *not* stand for `true` in JSON. The string "T" might be passed by someone to mean "true", but it's not part of the JSON standard. – Hot Licks Nov 04 '14 at 19:12
  • I did not say it stood for true in JSON. I said that is what is getting passed to me in the JSON. In the case, and only this case, it represents true. I thought that was pretty clear in the question, or at least common sense to a developer. Guess not. – tentmaking Nov 04 '14 at 19:23
  • Show us a bit of the JSON with the T in it. – Hot Licks Nov 04 '14 at 19:24
  • The question clearly states that I am using JSONModel and JSONValueTransformer to do the conversion. I am not doing anything to convert the values myself. The question also clearly asks, "How can this be fixed so the correct BOOL value is returned.." The JSON is very simple, SomeJsonValue = T; – tentmaking Nov 04 '14 at 19:28

1 Answers1

1

In the project that you have linked, the BOOLFromNSString method is as follows:

-(NSNumber*)BOOLFromNSString:(NSString*)string
{
  if (string != nil && 
    ([string caseInsensitiveCompare:@"true"] == NSOrderedSame ||
    [string caseInsensitiveCompare:@"yes"] == NSOrderedSame)) {
    return [NSNumber numberWithBool:YES];
  }
  return [NSNumber numberWithBool: ([string intValue]==0)?NO:YES];
}

This means that it is expected to return YES for the following case-insensitive values: true, yes, [any number that isn't 0].

The fact that it returns YES for T on any platform is magic, not "correct". You should use one of the expected values.


Edit: Your subclass:

#import "JSONModelTransformations/JSONValueTransformer.h"

@interface MyParser : JSONValueTransformer
@end

@implementation MyParser
- (NSNumber *)BOOLFromNSString:(NSString *)string {
  if (string != nil && [string caseInsensitiveCompare:@"t"] == NSOrderedSame) {
    return [NSNumber numberWithBool:YES];
  }
  return [super BOOLFromNSString:string];
}
@end
Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
  • Yes, it is magic, but the magic also produces the correct result. I cannot alter the JSON passed to me, I am stuck with dealing with the T. – tentmaking Nov 04 '14 at 19:11
  • Then I suggest you create your own parse function (subclass and override `BOOLFromNSString:`) instead of relying on magic. – Ian MacDonald Nov 04 '14 at 19:13
  • A Boolean value, in JSON, is passed as `true` or `false`, without quotes. As such, it will be parsed as a Boolean value, not as a string. (In iOS that Boolean value will be parsed as an NSNumber with a value `0` or `1`.) – Hot Licks Nov 04 '14 at 19:15
  • Except in this particular case, the OP has clearly stated that it is passed as "T" and not a Boolean value. – Ian MacDonald Nov 04 '14 at 19:17
  • 1
    That is clearly *not* impossible. JSON is a plain text that can be written in a simple text editor. – Ian MacDonald Nov 04 '14 at 19:19
  • 1
    Data types are not specified in JSON format. Having an entry `"myKey": true` is just as valid as `"myKey": "T"` as far as the format is concerned. What the OP would like to be able to do is parse the `"T"` value as though it were a Boolean. – Ian MacDonald Nov 04 '14 at 19:22
  • The JSON is passing me a string, T or F. That is why I am using BOOLFromNSString. I am not having any luck overriding JSONValueTransformer methods. – tentmaking Nov 04 '14 at 19:23
  • 1
    The question was regarding https://github.com/icanzilb/JSONModel , not "any reasonable JSON parser". Please read the question before commenting. – Ian MacDonald Nov 04 '14 at 19:50