Skip to content

Commit

Permalink
allow json null in JSONValue
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Oct 4, 2023
1 parent e6fbaa6 commit 02f6286
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions TinodeSDK/model/JSONValue.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// JSONValue.swift
//
// Copyright © 2019-2022 Tinode LLC. All rights reserved.
// Copyright © 2019-2023 Tinode LLC. All rights reserved.
//

import Foundation
Expand All @@ -17,6 +17,7 @@ public enum JSONValue: Codable, Equatable {
case dict([String: JSONValue])
case array([JSONValue])
case bytes(Data)
case null

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
Expand All @@ -37,12 +38,16 @@ public enum JSONValue: Codable, Equatable {
try container.encode(v)
case .bytes(let v):
try container.encode(v)
case .null:
try container.encodeNil()
}
}

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let value = try? container.decode(String.self) {
if container.decodeNil() {
self = .null
} else if let value = try? container.decode(String.self) {
self = .string(value)
} else if let value = try? container.decode(Int.self) {
self = .int(value)
Expand Down Expand Up @@ -121,4 +126,8 @@ public enum JSONValue: Codable, Equatable {
}
return nil
}

public func isNil() -> Bool {
return self == .null
}
}

0 comments on commit 02f6286

Please sign in to comment.