Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBL-4855: Converting between integer number in JSON string and Couchbase Lite Test #3186

Merged
merged 3 commits into from
Sep 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Swift/Tests/DocumentTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,35 @@ class DocumentTest: CBLTestCase {

// MARK: toJSON

// CBL-4855
func testJSONNumber () throws {
// assigned type [String: Any] -> key1 Int and key2 Double
let dict1 : [String : Any] = ["key1": 12345, "key2": 10.5]
XCTAssert(type(of: dict1["key1"]!) == Int.self)
let mdoc1 = MutableDocument(id: "doc1", data: dict1)
try self.defaultCollection!.save(document: mdoc1)
let doc1 = try self.defaultCollection!.document(id: "doc1")!
let json1 = doc1.toJSON()
XCTAssertEqual("{\"key1\":12345,\"key2\":10.5}", json1)

// no type -> Swift will assign a type to suit both -> Double
let dict2 = ["key1": 12345, "key2": 10.5]
XCTAssert(type(of: dict2["key1"]!) == Double.self)
let mdoc2 = MutableDocument(id: "doc2", data: dict2)
try self.defaultCollection!.save(document: mdoc2)
let doc2 = try self.defaultCollection!.document(id: "doc2")!
let json2 = doc2.toJSON()
XCTAssertEqual("{\"key1\":12345.0,\"key2\":10.5}", json2)

// JSONSerialization - assigned type [String: Any]
let dict3 = try JSONSerialization.jsonObject(with: "{\"key1\": 12345, \"key2\": 10.5 }".data(using: .utf8)!, options: []) as! [String : Any]
let mdoc3 = MutableDocument(id: "doc3", data: dict3)
try self.defaultCollection!.save(document: mdoc3)
let doc3 = try self.defaultCollection!.document(id: "doc3")!
let json3 = doc3.toJSON()
XCTAssertEqual("{\"key1\":12345,\"key2\":10.5}", json3)
}

func testDocumentToJSON() throws {
let json = try getRickAndMortyJSON()
var mDoc = try MutableDocument(id: "doc", json: json)
Expand Down
Loading