Last active
November 12, 2021 17:56
-
-
Save danielgarbien/3c3ee4112e7dcd68b980830dd26b3ccb to your computer and use it in GitHub Desktop.
NDJSON 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
import Foundation | |
import XCTest | |
class NDJSONParserTests: XCTestCase { | |
let ndJson = "{ \"key\": \"value\" }\n" | |
func testSimpleNDJSON() { | |
let twoLines = ndJson + ndJson | |
XCTAssert(parseDataFromString(twoLines).count == 2) | |
} | |
func testEmptyNDJSON() { | |
XCTAssert(parseDataFromString("").count == 0) | |
} | |
func testDoubleNewLineNDJSON() { | |
let doubleNewLines = ndJson + "\n" + ndJson | |
XCTAssert(parseDataFromString(doubleNewLines).count == 2) | |
} | |
func testOneNewLineNDJSON() { | |
let doubleNewLines = "\n" | |
XCTAssert(parseDataFromString(doubleNewLines).count == 0) | |
} | |
func testLeadingNewLineNDJSON() { | |
let leadingNewLine = "\n" + ndJson | |
XCTAssert(parseDataFromString(leadingNewLine).count == 1) | |
} | |
func testNoEndingNewLine() { | |
let noEndingNewLine = "{ \"key\": \"value\" }" | |
XCTAssert(parseDataFromString(noEndingNewLine).count == 1) | |
} | |
func testPerformance() { | |
let huge = Array(count: 100000, repeatedValue: ndJson).reduce("", combine: +) | |
self.measureBlock { | |
self.parseDataFromString(huge) | |
} | |
} | |
private func parseDataFromString(string: String) -> [AnyObject] { | |
let data = string.dataUsingEncoding(NSUTF8StringEncoding)! | |
do { | |
return try NDJSONParser.JSONObjectsWithData(data, options: []) | |
} catch { | |
XCTFail(String(error)) | |
return [] | |
} | |
} | |
} |
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
import Foundation | |
class NDJSONParser { | |
/** | |
Simple NDJSON parser implementation. | |
String from NDJSON data is converted to JSON array of elements formed with lines. This array is then to be deserialized with Foundation's NSJSONSerialization class. | |
Empty lines are disregarded. | |
- parameter data: A data object containing UTF8 encoded text with NDJSON. | |
- parameter opt: Options for reading the JSON passed to NSJSONSerialization. | |
- throws: Errors thrown by NSJSONSerialization. | |
- returns: Array of JSON objects | |
*/ | |
class func JSONObjectsWithData(data: NSData, options opt: NSJSONReadingOptions = []) throws -> [AnyObject] { | |
let nonEmptyLines = String(data: data, encoding: NSUTF8StringEncoding)! | |
.componentsSeparatedByString("\n").filter{ !$0.isEmpty } | |
let string = | |
"[" + nonEmptyLines.joinWithSeparator(",") | |
+ "]" | |
let data = string.dataUsingEncoding(NSUTF8StringEncoding)! | |
return try NSJSONSerialization.JSONObjectWithData(data, options: opt) as! [AnyObject] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
latest swift syntax for the class func: