GitXplorerGitXplorer
Y

SwiftCodableDictionary

public
5 stars
0 forks
0 issues

Commits

List of commits on branch main.
Verified
834b73a76cfd98dd1dde46d5f41b697ac0625888

Merge pull request #5 from YOCKOW/development

YYOCKOW committed 4 months ago
Verified
d7a1aa642d1c9fe7b1a17a665ddc2724ad8a1a22

Support Swift 6.

YYOCKOW committed 4 months ago
Verified
0f09cec3062dfacc4e91f22753126c3ecbc28252

Merge pull request #4 from YOCKOW/development

YYOCKOW committed 4 years ago
Verified
2edec7b391dc980ec84d2b3e6bde5c0fcfbf3e55

Update XCTestManifests.swift

YYOCKOW committed 4 years ago
Verified
ffd9468cc8ef91757d2ecd6ac70967485a60b1bd

Update ci.yml

YYOCKOW committed 4 years ago
Verified
5e5bd5bcdc2fd36389b8bdd5e393bf2ab1784dda

Merge pull request #3 from YOCKOW/development

YYOCKOW committed 4 years ago

README

The README file for this repository.

What is SwiftCodableDictionary?

Yet another workaround for SR-7788.

Overview

As implemented in the standard library, Dictionary can be en/decoded in/from a keyed container only if the dictionary uses String or Int keys. An unkeyed container is used to encode Dictionary even if Key conforms to Codable, and a keyed container cannot be decoded even if Key can be decoded from a string representation.

CodableDictionary resolves the issue. CodableDictionary can be en/decoded while its Key conforms to CodableDictionaryKey that inherits from Hashable and CodingKey. The point is that Key does not have to conform to Codable.

Usage

import Foundation
import CodableDictionary

enum Key: String, CodableDictionaryKey { 
   case key
   // No additional implementation is required,
   // because `CodableDictionaryKey` provides default implementation.
}

let dictionary: CodableDictionary<Key, String> = [.key: "value"]

let json = String(data: try JSONEncoder().encode(dictionary), encoding: .utf8)!
print(json) // -> {"key":"value"}

let decoded = try JSONDecoder().decode(CodableDictionary<Key, String>.self, from: json.data(using: .utf8)!)
print(dictionary == decoded) // -> true

Requirements

  • Swift 6, 5
  • OS
    • macOS
    • Linux

License

MIT License.
See "LICENSE.txt" for more information.