Skip to content

Instantly share code, notes, and snippets.

@adib
Last active December 12, 2015 09:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adib/4752376 to your computer and use it in GitHub Desktop.
Save adib/4752376 to your computer and use it in GitHub Desktop.
JSON parsing data type support
// JSONSupport.m
// Created by Sasmito Adibowo on 29-08-12.
// Copyright (c) 2012 Basil Salad Software. .
// http://basilsalad.com
//
// Licensed under the BSD License <http://www.opensource.org/licenses/bsd-license>
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
@implementation NSNull (JSONSupport)
-(NSString*) stringValue {
return nil;
}
-(NSNumber*) numberValue
{
return nil;
}
-(float) floatValue
{
return 0;
}
-(double) doubleValue
{
return 0;
}
-(BOOL) boolValue
{
return NO;
}
-(int) intValue
{
return 0;
}
-(long) longValue
{
return 0;
}
-(long long) longLongValue
{
return 0;
}
-(unsigned long long) unsignedLongLongValue
{
return 0;
}
@end
@implementation NSString (JSONSupport)
-(NSString*) stringValue
{
return self;
}
-(unsigned long long) unsignedLongLongValue
{
return strtoull([self UTF8String], NULL,0);
}
-(NSNumber*) numberValue
{
NSString* trimmed = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSRange locationOfDecimal = [trimmed rangeOfString:@"."];
if (locationOfDecimal.location == NSNotFound) {
// integer
NSUInteger length = [trimmed length];
if (length == 0) {
return 0;
}
if (length >= 10) {
// longer than +/- 2 147 483 647
if ([trimmed hasPrefix:@"-"]) {
return [NSNumber numberWithLongLong:[trimmed longLongValue]];
} else {
return [NSNumber numberWithUnsignedLongLong:[trimmed unsignedLongLongValue]];
}
} else if(length >= 5) {
// ensure can be +/- 32 767
if ([trimmed hasPrefix:@"-"]) {
return [NSNumber numberWithInt:[trimmed intValue]];
} else {
return [NSNumber numberWithUnsignedInt:(unsigned int)[trimmed unsignedLongLongValue]];
}
} else if(length >= 3) {
// ensure can be +/- 128
if ([trimmed hasPrefix:@"-"]) {
return [NSNumber numberWithShort:(short)[trimmed intValue]];
} else {
return [NSNumber numberWithUnsignedShort:(unsigned short)[trimmed unsignedLongLongValue]];
}
} else {
if ([trimmed hasPrefix:@"-"]) {
return [NSNumber numberWithChar:(char)[trimmed intValue]];
} else {
return [NSNumber numberWithUnsignedChar:(unsigned char)[trimmed unsignedLongLongValue]];
}
}
} else {
// floating-point
return [NSNumber numberWithDouble:[trimmed doubleValue]];
}
}
@end
@implementation NSNumber (JSONSupport)
-(NSNumber*) numberValue
{
return self;
}
@end
@implementation NSDate (JSONSupport)
+(NSDate*) dateWithFuzzyString:(NSString*) dateString
{
if (!dateString) {
return nil;
}
NSDate* result = nil;
#if !TARGET_OS_IPHONE
result = [NSDate dateWithString:dateString];
if (result) {
return result;
}
result = [NSDate dateWithNaturalLanguageString:dateString];
if (result) {
return result;
}
#endif
// Unicode Technical Standard #35: Date Format Patterns
// http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns (the page is really slow to load)
// http://webcache.googleusercontent.com/search?q=cache:EzQS_WnAi1IJ:unicode.org/reports/tr35/tr35-10.html+cached:http://unicode.org/reports/tr35/tr35-10.html%23Date_Format_Patterns&cd=1&hl=en&ct=clnk&client=safari (alternative from google's cache).
static NSString* const formats[] = {
//Sample date: "Tue May 17 06:18:25 +0000 2011" (used by Twitter)
@"EEE MMM dd HH:mm:ss ZZZ yyyy",
// sample date: Tue, 8 Dec 2009 21:30:43 +0800
@"EEE, d MMM yyyy HH:mm:ss ZZZ",
// sample date: Friday, 1 July 2001, 11:45 AM
@"EEEE, d MMMM yyyy, hh:mm a",
// sample date: 05/22/2007 03:15:23 UTC (Twitter once switched to this format)
// http://groups.google.com/group/twitter-development-talk/browse_thread/thread/0ed59aaaff01661c/80ef61a443768587
@"dd/MM/yyyy HH:mm:ss zzz",
// sample date: "December 10, 2009"
@"MMMM, dd yyyy",
// sample date: "Dec 10, 2009"
@"MMM, dd yyyy",
// sample date: 10-Dec-09
@"dd-MMM-yy",
// sample date: 10 Dec 09
@"dd MMM yy",
};
NSDateFormatter* fmt= [NSDateFormatter new];
fmt.formatterBehavior = NSDateFormatterBehavior10_4;
[fmt setLenient:YES];
for (NSUInteger i=0; i<ARRAY_COUNT(formats);i++) {
fmt.dateFormat = formats[i];
result = [fmt dateFromString:dateString];
if (result) {
return result;
}
}
return nil;
}
-(NSString*) stringValue
{
// print date in MIME date format
// http://tools.ietf.org/html/rfc5322#section-3.3
// sample date: Tue, 8 Dec 2009 21:30:43 +0800
NSDateFormatter* fmt= [NSDateFormatter new];
fmt.dateFormat = @"EEE, d MMM yyyy HH:mm:ss ZZZ"; // sample date: Tue, 8 Dec 2009 21:30:43 +0800
return [fmt stringFromDate:self];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment