Skip to content

Commit

Permalink
⭐️ Impl: Encodable+Helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 27, 2024
1 parent 0de4304 commit 6b68141
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ios/Temp/Encodable+Helpers.swift
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;
};
};

0 comments on commit 6b68141

Please sign in to comment.