Skip to content

Commit 4c0c5e5

Browse files
agnosticdevLukasa
andauthored
issue-1891: API Addition for JSONSerialization in NIOFoundationCompat (#1906)
Motivation: Issue for #1891 to add JSONSerialization in NIOFoundationCompat. Modifications: Adds: JSONSerialization+ByteBuffer.swift JSONSerialization+ByteBufferTest.swift JSONSerialization+ByteBufferTest+XCTest.swift Result: Support for transforming byteBuffer data to a Foundation object. Co-authored-by: Cory Benfield <[email protected]>
1 parent dc9edbb commit 4c0c5e5

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftNIO open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the SwiftNIO project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import NIO
16+
import Foundation
17+
18+
extension JSONSerialization {
19+
20+
/// Attempts to derive a Foundation object from a ByteBuffer and return it as `T`.
21+
///
22+
/// - parameters:
23+
/// - buffer: The ByteBuffer being used to derive the Foundation type.
24+
/// - options: The reading option used when the parser derives the Foundation type from the ByteBuffer.
25+
/// - returns: The Foundation value if successful or `nil` if there was an issue creating the Foundation type.
26+
@inlinable
27+
public static func jsonObject(with buffer: ByteBuffer,
28+
options opt: JSONSerialization.ReadingOptions = []) throws -> Any {
29+
return try JSONSerialization.jsonObject(with: Data(buffer: buffer), options: opt)
30+
}
31+
}

Tests/LinuxMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class LinuxMainRunnerImpl: LinuxMainRunner {
9191
testCase(IdleStateHandlerTest.allTests),
9292
testCase(IntegerBitPackingTests.allTests),
9393
testCase(IntegerTypesTest.allTests),
94+
testCase(JSONSerializationByteBufferTest.allTests),
9495
testCase(LinuxTest.allTests),
9596
testCase(MarkedCircularBufferTests.allTests),
9697
testCase(MessageToByteEncoderTest.allTests),
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftNIO open source project
4+
//
5+
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
//
15+
// JSONSerialization+ByteBufferTest+XCTest.swift
16+
//
17+
import XCTest
18+
19+
///
20+
/// NOTE: This file was generated by generate_linux_tests.rb
21+
///
22+
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
23+
///
24+
25+
extension JSONSerializationByteBufferTest {
26+
27+
@available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings")
28+
static var allTests : [(String, (JSONSerializationByteBufferTest) -> () throws -> Void)] {
29+
return [
30+
("testSerializationRoundTrip", testSerializationRoundTrip),
31+
]
32+
}
33+
}
34+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftNIO open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the SwiftNIO project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Foundation
16+
import XCTest
17+
import NIO
18+
import NIOFoundationCompat
19+
20+
class JSONSerializationByteBufferTest: XCTestCase {
21+
22+
func testSerializationRoundTrip() {
23+
24+
let array = ["String1", "String2", "String3"]
25+
let dictionary = ["key1": "val1", "key2": "val2", "key3": "val3"]
26+
27+
var dataArray = Data()
28+
var dataDictionary = Data()
29+
30+
XCTAssertTrue(JSONSerialization.isValidJSONObject(array), "Array object cannot be converted to JSON")
31+
XCTAssertTrue(JSONSerialization.isValidJSONObject(dictionary), "Dictionary object cannot be converted to JSON")
32+
33+
XCTAssertNoThrow(dataArray = try JSONSerialization.data(withJSONObject: array, options: .prettyPrinted))
34+
XCTAssertNoThrow(dataDictionary = try JSONSerialization.data(withJSONObject: dictionary, options: .prettyPrinted))
35+
36+
let arrayByteBuffer = ByteBuffer(data: dataArray)
37+
let dictByteBuffer = ByteBuffer(data: dataDictionary)
38+
39+
var foundationArray: [String] = []
40+
var foundationDict: [String: String] = [:]
41+
42+
// Mutable containers comparison.
43+
XCTAssertNoThrow(foundationArray = try JSONSerialization.jsonObject(with: arrayByteBuffer, options: .mutableContainers) as! [String])
44+
XCTAssertEqual(foundationArray, array)
45+
46+
XCTAssertNoThrow(foundationDict = try JSONSerialization.jsonObject(with: dictByteBuffer, options: .mutableContainers) as! [String : String])
47+
XCTAssertEqual(foundationDict, dictionary)
48+
49+
// Mutable leaves comparison.
50+
XCTAssertNoThrow(foundationArray = try JSONSerialization.jsonObject(with: arrayByteBuffer, options: .mutableLeaves) as! [String])
51+
XCTAssertEqual(foundationArray, array)
52+
53+
XCTAssertNoThrow(foundationDict = try JSONSerialization.jsonObject(with: dictByteBuffer, options: .mutableLeaves) as! [String : String])
54+
XCTAssertEqual(foundationDict, dictionary)
55+
}
56+
}

0 commit comments

Comments
 (0)