-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0de4304
commit 6b68141
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// Encodable+Helpers.swift | ||
// react-native-ios-modal | ||
// | ||
// Created by Dominic Go on 9/27/24. | ||
// | ||
|
||
import Foundation | ||
import DGSwiftUtilities | ||
|
||
|
||
public extension Encodable { | ||
|
||
/// Encode into JSON and return `Data` | ||
func convertToJsonData() throws -> Data { | ||
let encoder = JSONEncoder(); | ||
encoder.outputFormatting = .prettyPrinted; | ||
encoder.dateEncodingStrategy = .iso8601; | ||
|
||
return try encoder.encode(self); | ||
}; | ||
|
||
func convertToJsonDictionary() throws -> [String: Any] { | ||
let jsonData = try self.convertToJsonData(); | ||
|
||
let jsonObject = try JSONSerialization.jsonObject( | ||
with: jsonData, | ||
options: [] | ||
); | ||
|
||
guard let jsonDict = jsonObject as? [String : Any] else { | ||
throw GenericError( | ||
errorCode: .typeCastFailed, | ||
description: "Unable to convert json object to dictionary" | ||
); | ||
}; | ||
|
||
return jsonDict; | ||
}; | ||
}; |