Skip to content

Commit f3f77bf

Browse files
andrewheardG.Dev.Ssomsak
authored andcommitted
Add types to represent JSON values (google-gemini#112)
1 parent abf6ce2 commit f3f77bf

File tree

2 files changed

+17
-102
lines changed

2 files changed

+17
-102
lines changed

Sources/GoogleAI/JSONValue.swift

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,4 @@ extension JSONValue: Decodable {
6868
}
6969
}
7070

71-
extension JSONValue: Encodable {
72-
public func encode(to encoder: Encoder) throws {
73-
var container = encoder.singleValueContainer()
74-
switch self {
75-
case .null:
76-
try container.encodeNil()
77-
case let .number(numberValue):
78-
// Convert to `Decimal` before encoding for consistent floating-point serialization across
79-
// platforms. E.g., `Double` serializes 3.14159 as 3.1415899999999999 in some cases and
80-
// 3.14159 in others. See
81-
// https://forums.swift.org/t/jsonencoder-encodable-floating-point-rounding-error/41390/4 for
82-
// more details.
83-
try container.encode(Decimal(numberValue))
84-
case let .string(stringValue):
85-
try container.encode(stringValue)
86-
case let .bool(boolValue):
87-
try container.encode(boolValue)
88-
case let .object(objectValue):
89-
try container.encode(objectValue)
90-
case let .array(arrayValue):
91-
try container.encode(arrayValue)
92-
}
93-
}
94-
}
95-
9671
extension JSONValue: Equatable {}

Tests/GoogleAITests/JSONValueTests.swift

Lines changed: 17 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,46 @@ import XCTest
1616
@testable import GoogleGenerativeAI
1717

1818
final class JSONValueTests: XCTestCase {
19-
let decoder = JSONDecoder()
20-
let encoder = JSONEncoder()
21-
22-
let numberKey = "pi"
23-
let numberValue = 3.14159
24-
let numberValueEncoded = "3.14159"
25-
let stringKey = "hello"
26-
let stringValue = "Hello, world!"
27-
28-
override func setUp() {
29-
encoder.outputFormatting = .sortedKeys
30-
}
31-
3219
func testDecodeNull() throws {
3320
let jsonData = try XCTUnwrap("null".data(using: .utf8))
3421

35-
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
22+
let jsonObject = try XCTUnwrap(JSONDecoder().decode(JSONValue.self, from: jsonData))
3623

3724
XCTAssertEqual(jsonObject, .null)
3825
}
3926

4027
func testDecodeNumber() throws {
41-
let jsonData = try XCTUnwrap("\(numberValue)".data(using: .utf8))
28+
let expectedNumber = 3.14159
29+
let jsonData = try XCTUnwrap("\(expectedNumber)".data(using: .utf8))
4230

43-
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
31+
let jsonObject = try XCTUnwrap(JSONDecoder().decode(JSONValue.self, from: jsonData))
4432

45-
XCTAssertEqual(jsonObject, .number(numberValue))
33+
XCTAssertEqual(jsonObject, .number(expectedNumber))
4634
}
4735

4836
func testDecodeString() throws {
49-
let jsonData = try XCTUnwrap("\"\(stringValue)\"".data(using: .utf8))
37+
let expectedString = "hello-world"
38+
let jsonData = try XCTUnwrap("\"\(expectedString)\"".data(using: .utf8))
5039

51-
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
40+
let jsonObject = try XCTUnwrap(JSONDecoder().decode(JSONValue.self, from: jsonData))
5241

53-
XCTAssertEqual(jsonObject, .string(stringValue))
42+
XCTAssertEqual(jsonObject, .string(expectedString))
5443
}
5544

5645
func testDecodeBool() throws {
5746
let expectedBool = true
5847
let jsonData = try XCTUnwrap("\(expectedBool)".data(using: .utf8))
5948

60-
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
49+
let jsonObject = try XCTUnwrap(JSONDecoder().decode(JSONValue.self, from: jsonData))
6150

6251
XCTAssertEqual(jsonObject, .bool(expectedBool))
6352
}
6453

6554
func testDecodeObject() throws {
55+
let numberKey = "pi"
56+
let numberValue = 3.14159
57+
let stringKey = "hello"
58+
let stringValue = "world"
6659
let expectedObject: JSONObject = [
6760
numberKey: .number(numberValue),
6861
stringKey: .string(stringValue),
@@ -75,71 +68,18 @@ final class JSONValueTests: XCTestCase {
7568
"""
7669
let jsonData = try XCTUnwrap(json.data(using: .utf8))
7770

78-
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
71+
let jsonObject = try XCTUnwrap(JSONDecoder().decode(JSONValue.self, from: jsonData))
7972

8073
XCTAssertEqual(jsonObject, .object(expectedObject))
8174
}
8275

8376
func testDecodeArray() throws {
77+
let numberValue = 3.14159
8478
let expectedArray: [JSONValue] = [.null, .number(numberValue)]
8579
let jsonData = try XCTUnwrap("[ null, \(numberValue) ]".data(using: .utf8))
8680

87-
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
81+
let jsonObject = try XCTUnwrap(JSONDecoder().decode(JSONValue.self, from: jsonData))
8882

8983
XCTAssertEqual(jsonObject, .array(expectedArray))
9084
}
91-
92-
func testEncodeNull() throws {
93-
let jsonData = try encoder.encode(JSONValue.null)
94-
95-
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
96-
XCTAssertEqual(json, "null")
97-
}
98-
99-
func testEncodeNumber() throws {
100-
let jsonData = try encoder.encode(JSONValue.number(numberValue))
101-
102-
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
103-
XCTAssertEqual(json, "\(numberValue)")
104-
}
105-
106-
func testEncodeString() throws {
107-
let jsonData = try encoder.encode(JSONValue.string(stringValue))
108-
109-
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
110-
XCTAssertEqual(json, "\"\(stringValue)\"")
111-
}
112-
113-
func testEncodeBool() throws {
114-
let boolValue = true
115-
116-
let jsonData = try encoder.encode(JSONValue.bool(boolValue))
117-
118-
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
119-
XCTAssertEqual(json, "\(boolValue)")
120-
}
121-
122-
func testEncodeObject() throws {
123-
let objectValue: JSONObject = [
124-
numberKey: .number(numberValue),
125-
stringKey: .string(stringValue),
126-
]
127-
128-
let jsonData = try encoder.encode(JSONValue.object(objectValue))
129-
130-
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
131-
XCTAssertEqual(
132-
json,
133-
"{\"\(stringKey)\":\"\(stringValue)\",\"\(numberKey)\":\(numberValueEncoded)}"
134-
)
135-
}
136-
137-
func testEncodeArray() throws {
138-
let arrayValue: [JSONValue] = [.null, .number(numberValue)]
139-
140-
let jsonData = try encoder.encode(JSONValue.array(arrayValue))
141-
142-
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
143-
XCTAssertEqual(json, "[null,\(numberValueEncoded)]")
144-
}
14585
}

0 commit comments

Comments
 (0)