Created
February 13, 2012 10:55
-
-
Save Dimillian/1816017 to your computer and use it in GitHub Desktop.
Quick parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MSJSonParser.m | |
// MySeeen | |
// | |
// Created by Thomas Ricouard on 11/02/12. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// | |
#import "MSJSonParser.h" | |
@implementation MSJSonParser | |
@synthesize delegate; | |
-(id)initWithURL:(NSURL *)URL cacheFileName:(NSString *)filename | |
{ | |
self = [super init]; | |
if (self) { | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
_filename = [documentsDirectory stringByAppendingPathComponent:filename]; | |
_URL = URL; | |
} | |
return self; | |
} | |
-(void)parseFromServer{ | |
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:_URL | |
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData | |
timeoutInterval:60]; | |
_urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; | |
if (_urlConnection) { | |
_asyncData = [[NSMutableData alloc] init]; | |
} | |
} | |
#pragma mark - NSURLConnection delegate | |
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { | |
[_asyncData setLength:0]; | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { | |
[_asyncData appendData:data]; | |
} | |
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { | |
NSLog(@"%@", error); | |
} | |
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { | |
[self connectionDoneSoParse]; | |
} | |
#pragma mark - METHOD | |
-(BOOL)isExistingFile | |
{ | |
NSFileManager *fileM = [NSFileManager defaultManager]; | |
return [fileM fileExistsAtPath:_filename]; | |
} | |
-(void)lazyParsingFromFileThenServer | |
{ | |
if ([self isExistingFile]){ | |
[self parseJsonFromFile]; | |
} | |
[self parseFromServer]; | |
} | |
-(void)connectionDoneSoParse | |
{ | |
NSJSONSerialization *json = [NSJSONSerialization JSONObjectWithData:_asyncData options:NSJSONReadingAllowFragments error:nil]; | |
NSDictionary *dic = (NSDictionary *)json; | |
if ([dic objectForKey:@"errors"]) { | |
NSArray *errors = [dic objectForKey:@"errors"]; | |
MSJSonError *error = [[MSJSonError alloc]initWithDictionnary:[errors objectAtIndex:0]]; | |
[delegate jsonParserDidFinishParsingWithError:error]; | |
} | |
else{ | |
[delegate jsonParserDidFinishParsing:dic]; | |
[self writeJsonToFile:json]; | |
} | |
} | |
-(void)parseJsonFromFile | |
{ | |
NSInputStream *input = [NSInputStream inputStreamWithFileAtPath:_filename]; | |
[input open]; | |
NSJSONSerialization *read = [NSJSONSerialization JSONObjectWithStream:input options:NSJSONReadingAllowFragments error:nil]; | |
NSDictionary *dic = (NSDictionary *)read; | |
[delegate jsonParserDidFinishParsing:dic]; | |
[input close]; | |
} | |
-(void)writeJsonToFile:(NSJSONSerialization *)json | |
{ | |
NSOutputStream *opt = [NSOutputStream outputStreamToFileAtPath:_filename append:NO]; | |
[opt open]; | |
[NSJSONSerialization writeJSONObject:json toStream:opt options:NSJSONReadingAllowFragments error:nil]; | |
[opt close]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment