0

I'm new to iOS, I was able to write the NSDictionary objects into file like below example

{
"msg":"Hello",
"from":"X",
"date":"12/1/2014"
}
{
"msg":"new to IOS",
"from":"home",
"date":"23/2/2014"
}

I know it is an array of objects I need to be using NSArray, but I have prevented since my one NSDictionary object consumes lot of memory, I wanted to serialise and deserialize one object at a time. I came across SBJson4StreamParser internally does such functionality, but I was facing issues with implementation, I also tried with native NSJsonSeriailzation but options were limited for such type of parsing, can anyone help with this.

Stig Brautaset
  • 2,602
  • 1
  • 22
  • 39
virtplay
  • 550
  • 7
  • 18
  • Possible duplicate of [Is it possible to parse an NSInputStream using SBJson4?](http://stackoverflow.com/questions/23434401/is-it-possible-to-parse-an-nsinputstream-using-sbjson4) Certainly [my answer in that thread will work here too](http://stackoverflow.com/a/27330264/5950). – Stig Brautaset Jan 15 '15 at 00:03
  • The above is not an array of objects, it's invalid JSON consisting of two objects written back-to-back with no other JSON structuring. – Hot Licks Jan 15 '15 at 00:45
  • @HotLicks SBJson v3 and 4 can parse streams of documents like that and call a delegate method (or block in v4) every time it's parsed a complete document. – Stig Brautaset Jan 15 '15 at 20:57
  • @StigBrautaset - Right. But the entire stream is not valid JSON. – Hot Licks Jan 15 '15 at 21:00
  • @HotLicks that is correct. SBJson could help you even if you had just a mammoth top-level array though; it can unwrap the top-level array and feed you each of the 2nd-level documents inside as if it was a full document. – Stig Brautaset Jan 16 '15 at 08:36

2 Answers2

0

I would suggest another possibility: let's suppose is messages your NSArray of dictionary object, then you can serialize it:

[[NSUserDefaults standardUserDefaults] setObject:self.messages forKey:@"com.yourdomain.messages"];
[[NSUserDefaults standardUserDefaults] synchronize];

and deserialize

self.messages = [[NSUserDefaults standardUserDefaults] arrayForKey:@"com.yourdomain.messages"];

References: Apple doc

I hope this can help you.

bl4stwave
  • 141
  • 5
0

SBJson can help here, by reading the file chunkwise with an NSInputStream and feeding it into the parser like this:

id parser = [SBJson4Parser multiRootParserWithBlock:block
                                       errorHandler:eh];

id is = [NSInputStream inputStreamWithFileAtPath:filePath];
[is open];

// Buffer to read from the input stream
uint8_t buf[1024];

// Read from input stream until empty, or an error;
// better error handling is left as an exercise for the reader
while (0 > [is read:buffer maxLength: sizeof buffer]) {
    SBJson4ParserStatus status = [parser parse:data];
    NSLog(@"Status: %u",status);
    // Handle parser errors here
}
[is close];

Note that you still have to read and parse the whole file to guarantee that you find a particular entry. There is no way to process just a specific entry this way.

Stig Brautaset
  • 2,602
  • 1
  • 22
  • 39