forked from v-somov/JSONWebToken.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Implement Compact JSON Coders
- Loading branch information
Showing
5 changed files
with
30 additions
and
2 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
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
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,9 @@ | ||
class CompactJSONDecoder: JSONDecoder { | ||
override func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable { | ||
guard let string = String(data: data, encoding: .ascii) else { | ||
fatalError() | ||
} | ||
|
||
return try super.decode(type, from: base64decode(string)!) | ||
} | ||
} |
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,9 @@ | ||
class CompactJSONEncoder: JSONEncoder { | ||
override func encode<T : Encodable>(_ value: T) throws -> Data { | ||
return try encodeString(value).data(using: .ascii) ?? Data() | ||
} | ||
|
||
func encodeString<T: Encodable>(_ value: T) throws -> String { | ||
return base64encode(try super.encode(value)) | ||
} | ||
} |
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