NSError *error = nil;
NSUInteger bufferSize = 3 * 1024 * 1024; // 3 MB buffer
NSMutableData *mutableData = [NSMutableData dataWithLength:bufferSize];
uint8_t *buffer = (uint8_t *)[mutableData mutableBytes];
NSOutputStream *stream = [NSOutputStream outputStreamToBuffer:buffer capacity:bufferSize];
[stream open];
NSInteger totalBytesWritten = [NSJSONSerialization writeJSONObject:resultObject toStream:stream options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
NSLog(@"JSON Serialization Error: %@", [error localizedDescription]);
}
[stream close];
NSMutableData* mutableJsonData = [NSMutableData dataWithBytes:buffer length:totalBytesWritten];
memset(buffer, 0, totalBytesWritten);