├── .bin
└── generate
├── .github
└── workflows
│ └── Tests.yml
├── .gitignore
├── .swiftpm
└── xcode
│ ├── package.xcworkspace
│ └── contents.xcworkspacedata
│ └── xcshareddata
│ └── xcschemes
│ └── DictionaryCoding.xcscheme
├── .travis.yml
├── DictionaryCoding.podspec
├── DictionaryCoding.xcconfig
├── DictionaryCoding.xcodeproj
├── DictionaryCodingTests_Info.plist
├── DictionaryCoding_Info.plist
├── project.pbxproj
├── project.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcshareddata
│ │ └── WorkspaceSettings.xcsettings
└── xcshareddata
│ └── xcschemes
│ ├── DictionaryCoding-Package.xcscheme
│ └── xcschememanagement.plist
├── LICENSE.md
├── Package.resolved
├── Package.swift
├── README.md
├── Sources
└── DictionaryCoding
│ ├── DictionaryCodingKey.swift
│ ├── DictionaryDecoder.swift
│ ├── DictionaryEncoder.swift
│ └── DictionaryErrors.swift
└── Tests
├── DictionaryCodingTests
├── CombineTests.swift
├── DictionaryDecodingTests.swift
└── DictionaryEncodingTests.swift
└── LinuxMain.swift
/.bin/generate:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | swift package generate-xcodeproj --xcconfig-overrides DictionaryCoding.xcconfig
4 |
--------------------------------------------------------------------------------
/.github/workflows/Tests.yml:
--------------------------------------------------------------------------------
1 | # --------------------------------------------------------------------------------
2 | # This workflow was automatically generated by Action Status 2.0 (377).
3 | # (see https://actionstatus.elegantchaos.com for more details)
4 | # --------------------------------------------------------------------------------
5 |
6 | name: Tests
7 |
8 | on: [push, pull_request]
9 |
10 | jobs:
11 |
12 | macOS-swift-50:
13 | name: macOS (Swift 5.0)
14 | runs-on: macOS-latest
15 | steps:
16 | - name: Checkout
17 | uses: actions/checkout@v1
18 | - name: Make Logs Directory
19 | run: mkdir logs
20 | - name: Xcode Version
21 | run: |
22 | sudo xcode-select -s /Applications/Xcode_11.2.1.app
23 | xcodebuild -version
24 | swift --version
25 | - name: Swift Version
26 | run: swift --version
27 | - name: Build (Release)
28 | run: swift build -c release
29 | - name: Test (Release)
30 | run: swift test --configuration release -Xswiftc -enable-testing
31 | - name: Upload Logs
32 | uses: actions/upload-artifact@v1
33 | if: always()
34 | with:
35 | name: logs
36 | path: logs
37 |
38 |
39 | macOS-swift-nightly:
40 | name: macOS (Swift Development Nightly)
41 | runs-on: macOS-latest
42 | env:
43 | TOOLCHAINS: swift
44 | steps:
45 | - name: Checkout
46 | uses: actions/checkout@v1
47 | - name: Make Logs Directory
48 | run: mkdir logs
49 | - name: Install Toolchain
50 | run: |
51 | branch="development"
52 | wget --quiet https://swift.org/builds/$branch/xcode/latest-build.yml
53 | grep "download:" < latest-build.yml > filtered.yml
54 | sed -e 's/-osx.pkg//g' filtered.yml > stripped.yml
55 | sed -e 's/:[^:\/\/]/YML="/g;s/$/"/g;s/ *=/=/g' stripped.yml > snapshot.sh
56 | source snapshot.sh
57 | echo "Installing Toolchain: $downloadYML"
58 | wget --quiet https://swift.org/builds/$branch/xcode/$downloadYML/$downloadYML-osx.pkg
59 | sudo installer -pkg $downloadYML-osx.pkg -target /
60 | ln -s "/Library/Developer/Toolchains/$downloadYML.xctoolchain/usr/bin" swift-latest
61 | sudo xcode-select -s /Applications/Xcode_12_beta.app
62 | swift --version
63 | - name: Xcode Version
64 | run: |
65 | xcodebuild -version
66 | xcrun swift --version
67 | - name: Swift Version
68 | run: swift --version
69 | - name: Build (Release)
70 | run: export PATH="swift-latest:$PATH"; swift build -c release
71 | - name: Test (Release)
72 | run: export PATH="swift-latest:$PATH"; swift test --configuration release -Xswiftc -enable-testing --enable-test-discovery
73 | - name: Upload Logs
74 | uses: actions/upload-artifact@v1
75 | if: always()
76 | with:
77 | name: logs
78 | path: logs
79 |
80 |
81 | xcode-swift-50:
82 | name: Xcode (Swift 5.0)
83 | runs-on: macOS-latest
84 | steps:
85 | - name: Checkout
86 | uses: actions/checkout@v1
87 | - name: Make Logs Directory
88 | run: mkdir logs
89 | - name: Xcode Version
90 | run: |
91 | sudo xcode-select -s /Applications/Xcode_11.2.1.app
92 | xcodebuild -version
93 | swift --version
94 | - name: XC Pretty
95 | run: sudo gem install xcpretty-travis-formatter
96 | - name: Detect Workspace & Scheme (iOS)
97 | run: |
98 | WORKSPACE="DictionaryCoding.xcworkspace"
99 | if [[ ! -e "$WORKSPACE" ]]
100 | then
101 | WORKSPACE="."
102 | GOTPACKAGE=$(xcodebuild -workspace . -list | (grep DictionaryCoding-Package || true))
103 | if [[ $GOTPACKAGE != "" ]]
104 | then
105 | SCHEME="DictionaryCoding-Package"
106 | else
107 | SCHEME="DictionaryCoding"
108 | fi
109 | else
110 | SCHEME="DictionaryCoding-iOS"
111 | fi
112 | echo "set -o pipefail; export PATH='swift-latest:$PATH'; WORKSPACE='$WORKSPACE'; SCHEME='$SCHEME'" > setup.sh
113 | - name: Build (iOS Release)
114 | run: |
115 | source "setup.sh"
116 | echo "Building workspace $WORKSPACE scheme $SCHEME."
117 | xcodebuild clean build -workspace "$WORKSPACE" -scheme "$SCHEME" -configuration Release CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO | tee logs/xcodebuild-iOS-build-release.log | xcpretty
118 | - name: Test (iOS Release)
119 | run: |
120 | source "setup.sh"
121 | echo "Testing workspace $WORKSPACE scheme $SCHEME."
122 | xcodebuild test -workspace "$WORKSPACE" -scheme "$SCHEME" -destination "name=iPhone 11" -configuration Release CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO ENABLE_TESTABILITY=YES | tee logs/xcodebuild-iOS-test-release.log | xcpretty
123 | - name: Detect Workspace & Scheme (tvOS)
124 | run: |
125 | WORKSPACE="DictionaryCoding.xcworkspace"
126 | if [[ ! -e "$WORKSPACE" ]]
127 | then
128 | WORKSPACE="."
129 | GOTPACKAGE=$(xcodebuild -workspace . -list | (grep DictionaryCoding-Package || true))
130 | if [[ $GOTPACKAGE != "" ]]
131 | then
132 | SCHEME="DictionaryCoding-Package"
133 | else
134 | SCHEME="DictionaryCoding"
135 | fi
136 | else
137 | SCHEME="DictionaryCoding-tvOS"
138 | fi
139 | echo "set -o pipefail; export PATH='swift-latest:$PATH'; WORKSPACE='$WORKSPACE'; SCHEME='$SCHEME'" > setup.sh
140 | - name: Build (tvOS Release)
141 | run: |
142 | source "setup.sh"
143 | echo "Building workspace $WORKSPACE scheme $SCHEME."
144 | xcodebuild clean build -workspace "$WORKSPACE" -scheme "$SCHEME" -configuration Release CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO | tee logs/xcodebuild-tvOS-build-release.log | xcpretty
145 | - name: Test (tvOS Release)
146 | run: |
147 | source "setup.sh"
148 | echo "Testing workspace $WORKSPACE scheme $SCHEME."
149 | xcodebuild test -workspace "$WORKSPACE" -scheme "$SCHEME" -destination "name=Apple TV" -configuration Release CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO ENABLE_TESTABILITY=YES | tee logs/xcodebuild-tvOS-test-release.log | xcpretty
150 | - name: Detect Workspace & Scheme (watchOS)
151 | run: |
152 | WORKSPACE="DictionaryCoding.xcworkspace"
153 | if [[ ! -e "$WORKSPACE" ]]
154 | then
155 | WORKSPACE="."
156 | GOTPACKAGE=$(xcodebuild -workspace . -list | (grep DictionaryCoding-Package || true))
157 | if [[ $GOTPACKAGE != "" ]]
158 | then
159 | SCHEME="DictionaryCoding-Package"
160 | else
161 | SCHEME="DictionaryCoding"
162 | fi
163 | else
164 | SCHEME="DictionaryCoding-watchOS"
165 | fi
166 | echo "set -o pipefail; export PATH='swift-latest:$PATH'; WORKSPACE='$WORKSPACE'; SCHEME='$SCHEME'" > setup.sh
167 | - name: Build (watchOS Release)
168 | run: |
169 | source "setup.sh"
170 | echo "Building workspace $WORKSPACE scheme $SCHEME."
171 | xcodebuild clean build -workspace "$WORKSPACE" -scheme "$SCHEME" -configuration Release CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO | tee logs/xcodebuild-watchOS-build-release.log | xcpretty
172 | - name: Upload Logs
173 | uses: actions/upload-artifact@v1
174 | if: always()
175 | with:
176 | name: logs
177 | path: logs
178 |
179 |
180 | xcode-swift-nightly:
181 | name: Xcode (Swift Development Nightly)
182 | runs-on: macOS-latest
183 | env:
184 | TOOLCHAINS: swift
185 | steps:
186 | - name: Checkout
187 | uses: actions/checkout@v1
188 | - name: Make Logs Directory
189 | run: mkdir logs
190 | - name: Install Toolchain
191 | run: |
192 | branch="development"
193 | wget --quiet https://swift.org/builds/$branch/xcode/latest-build.yml
194 | grep "download:" < latest-build.yml > filtered.yml
195 | sed -e 's/-osx.pkg//g' filtered.yml > stripped.yml
196 | sed -e 's/:[^:\/\/]/YML="/g;s/$/"/g;s/ *=/=/g' stripped.yml > snapshot.sh
197 | source snapshot.sh
198 | echo "Installing Toolchain: $downloadYML"
199 | wget --quiet https://swift.org/builds/$branch/xcode/$downloadYML/$downloadYML-osx.pkg
200 | sudo installer -pkg $downloadYML-osx.pkg -target /
201 | ln -s "/Library/Developer/Toolchains/$downloadYML.xctoolchain/usr/bin" swift-latest
202 | sudo xcode-select -s /Applications/Xcode_12_beta.app
203 | swift --version
204 | - name: Xcode Version
205 | run: |
206 | xcodebuild -version
207 | xcrun swift --version
208 | - name: XC Pretty
209 | run: sudo gem install xcpretty-travis-formatter
210 | - name: Detect Workspace & Scheme (iOS)
211 | run: |
212 | WORKSPACE="DictionaryCoding.xcworkspace"
213 | if [[ ! -e "$WORKSPACE" ]]
214 | then
215 | WORKSPACE="."
216 | GOTPACKAGE=$(xcodebuild -workspace . -list | (grep DictionaryCoding-Package || true))
217 | if [[ $GOTPACKAGE != "" ]]
218 | then
219 | SCHEME="DictionaryCoding-Package"
220 | else
221 | SCHEME="DictionaryCoding"
222 | fi
223 | else
224 | SCHEME="DictionaryCoding-iOS"
225 | fi
226 | echo "set -o pipefail; export PATH='swift-latest:$PATH'; WORKSPACE='$WORKSPACE'; SCHEME='$SCHEME'" > setup.sh
227 | - name: Build (iOS Release)
228 | run: |
229 | source "setup.sh"
230 | echo "Building workspace $WORKSPACE scheme $SCHEME."
231 | xcodebuild clean build -workspace "$WORKSPACE" -scheme "$SCHEME" -configuration Release CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO | tee logs/xcodebuild-iOS-build-release.log | xcpretty
232 | - name: Test (iOS Release)
233 | run: |
234 | source "setup.sh"
235 | echo "Testing workspace $WORKSPACE scheme $SCHEME."
236 | xcodebuild test -workspace "$WORKSPACE" -scheme "$SCHEME" -destination "name=iPhone 11" -configuration Release CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO ENABLE_TESTABILITY=YES | tee logs/xcodebuild-iOS-test-release.log | xcpretty
237 | - name: Detect Workspace & Scheme (tvOS)
238 | run: |
239 | WORKSPACE="DictionaryCoding.xcworkspace"
240 | if [[ ! -e "$WORKSPACE" ]]
241 | then
242 | WORKSPACE="."
243 | GOTPACKAGE=$(xcodebuild -workspace . -list | (grep DictionaryCoding-Package || true))
244 | if [[ $GOTPACKAGE != "" ]]
245 | then
246 | SCHEME="DictionaryCoding-Package"
247 | else
248 | SCHEME="DictionaryCoding"
249 | fi
250 | else
251 | SCHEME="DictionaryCoding-tvOS"
252 | fi
253 | echo "set -o pipefail; export PATH='swift-latest:$PATH'; WORKSPACE='$WORKSPACE'; SCHEME='$SCHEME'" > setup.sh
254 | - name: Build (tvOS Release)
255 | run: |
256 | source "setup.sh"
257 | echo "Building workspace $WORKSPACE scheme $SCHEME."
258 | xcodebuild clean build -workspace "$WORKSPACE" -scheme "$SCHEME" -configuration Release CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO | tee logs/xcodebuild-tvOS-build-release.log | xcpretty
259 | - name: Test (tvOS Release)
260 | run: |
261 | source "setup.sh"
262 | echo "Testing workspace $WORKSPACE scheme $SCHEME."
263 | xcodebuild test -workspace "$WORKSPACE" -scheme "$SCHEME" -destination "name=Apple TV" -configuration Release CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO ENABLE_TESTABILITY=YES | tee logs/xcodebuild-tvOS-test-release.log | xcpretty
264 | - name: Detect Workspace & Scheme (watchOS)
265 | run: |
266 | WORKSPACE="DictionaryCoding.xcworkspace"
267 | if [[ ! -e "$WORKSPACE" ]]
268 | then
269 | WORKSPACE="."
270 | GOTPACKAGE=$(xcodebuild -workspace . -list | (grep DictionaryCoding-Package || true))
271 | if [[ $GOTPACKAGE != "" ]]
272 | then
273 | SCHEME="DictionaryCoding-Package"
274 | else
275 | SCHEME="DictionaryCoding"
276 | fi
277 | else
278 | SCHEME="DictionaryCoding-watchOS"
279 | fi
280 | echo "set -o pipefail; export PATH='swift-latest:$PATH'; WORKSPACE='$WORKSPACE'; SCHEME='$SCHEME'" > setup.sh
281 | - name: Build (watchOS Release)
282 | run: |
283 | source "setup.sh"
284 | echo "Building workspace $WORKSPACE scheme $SCHEME."
285 | xcodebuild clean build -workspace "$WORKSPACE" -scheme "$SCHEME" -configuration Release CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO | tee logs/xcodebuild-watchOS-build-release.log | xcpretty
286 | - name: Upload Logs
287 | uses: actions/upload-artifact@v1
288 | if: always()
289 | with:
290 | name: logs
291 | path: logs
292 |
293 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | /.build
3 | /Packages
4 | xcuserdata/
5 |
--------------------------------------------------------------------------------
/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.swiftpm/xcode/xcshareddata/xcschemes/DictionaryCoding.xcscheme:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
9 |
15 |
21 |
22 |
23 |
29 |
35 |
36 |
37 |
38 |
39 |
44 |
45 |
47 |
53 |
54 |
55 |
56 |
57 |
67 |
68 |
74 |
75 |
81 |
82 |
83 |
84 |
86 |
87 |
90 |
91 |
92 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | os: osx
2 | osx_image: xcode11.2
3 | language: swift
4 | install:
5 | - swift package update
6 | script:
7 | - swift test
8 | notifications:
9 | slack:
10 | secure: TsKpblxP1gz+o1HcJ+2Lw8U3gLfFhWSp6XzJwOOcB9TGfRYVm3kVhy0wzCK8q0V2+Eh8amXhj8oguvbP10GH4Ru+h1bH+p+tp/nfSbh9n7WwC3zArdoNqYxIre7KQ1cuEaoDspRgqKw27/aMBLcbmkzt2b3eAWjpFMnbHNKnIyjz1XNZR2WnQ0IJWvCYVBX/wmQ3d2w7nZ4UTyIh83FwfTYAPQVmlIY7saaTx7/rWit7u8LFGR61cjDgTVbqjJ+vXR+CZqp+KoAPHhhZBchNVv57vH3MjvWYoQ2iPVU3VjvBXTFD700lQBWDdYQ1w7Ew4nzCEr6DHGKO8FvDlna/rW2UDRbQuK5Qn0kPAjMUpTUlJIo+2O7SK6PGRqRCirJ65uOhrJFasWL0kPx4QC8PeUXMVUTYK86Xj17IlwPFIRoe+caqyG30KOCf209Zt0o3FokzUS/FU0Ca9KyejP+US/FF6G9QWc4txlXtTNOV1+jxZWi44AC4G56A1Ik8lCCPjaMH/vz7nbM35oYtkZJ9TPCRXBh1E7j2rQu8Bx6FqKlw1cO7MaxZA5XJF6aOWHWZpNtPkVzluZ3UrILt++QazzTL45Jn/BOlw4F4g6jeZTB26j7Z6VIlyYmoTkywE4IYDovASos4XRFTAGl44TZV3LYz/em4FZKP5OdQ34g34a4=
11 |
--------------------------------------------------------------------------------
/DictionaryCoding.podspec:
--------------------------------------------------------------------------------
1 | Pod::Spec.new do |s|
2 | s.name = 'DictionaryCoding'
3 | s.version = '1.0.7'
4 | s.summary = 'Swift Decoder/Encoder which converts to/from dictionaries.'
5 |
6 | s.description = <<-DESC
7 | This is an implementation of Swift's Encoder/Decoder protocols which uses NSDictionary as its underlying container mechanism.
8 |
9 | It allows you to take a native swift class or struct that confirms to the Codable protocol and convert it to, or initialise it from, a dictionary.
10 | DESC
11 |
12 | s.homepage = 'https://github.com/elegantchaos/DictionaryCoding'
13 | s.license = { :type => 'custom', :file => 'LICENSE.md' }
14 | s.author = { 'Sam Deane' => 'sam@elegantchaos.com' }
15 | s.source = { :git => 'https://github.com/elegantchaos/DictionaryCoding.git', :tag => s.version.to_s }
16 |
17 | s.swift_version = "5.0"
18 | s.osx.deployment_target = "10.12"
19 | s.ios.deployment_target = "10.0"
20 | s.tvos.deployment_target = "10.0"
21 |
22 | s.source_files = 'Sources/DictionaryCoding/**/*'
23 | end
24 |
--------------------------------------------------------------------------------
/DictionaryCoding.xcconfig:
--------------------------------------------------------------------------------
1 | IPHONEOS_DEPLOYMENT_TARGET = 10.0
2 | MACOSX_DEPLOYMENT_TARGET = 10.12
3 | APPLICATION_EXTENSION_API_ONLY = YES
4 |
--------------------------------------------------------------------------------
/DictionaryCoding.xcodeproj/DictionaryCodingTests_Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | CFBundleDevelopmentRegion
5 | en
6 | CFBundleExecutable
7 | $(EXECUTABLE_NAME)
8 | CFBundleIdentifier
9 | $(PRODUCT_BUNDLE_IDENTIFIER)
10 | CFBundleInfoDictionaryVersion
11 | 6.0
12 | CFBundleName
13 | $(PRODUCT_NAME)
14 | CFBundlePackageType
15 | BNDL
16 | CFBundleShortVersionString
17 | 1.0
18 | CFBundleSignature
19 | ????
20 | CFBundleVersion
21 | $(CURRENT_PROJECT_VERSION)
22 | NSPrincipalClass
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/DictionaryCoding.xcodeproj/DictionaryCoding_Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | CFBundleDevelopmentRegion
5 | en
6 | CFBundleExecutable
7 | $(EXECUTABLE_NAME)
8 | CFBundleIdentifier
9 | $(PRODUCT_BUNDLE_IDENTIFIER)
10 | CFBundleInfoDictionaryVersion
11 | 6.0
12 | CFBundleName
13 | $(PRODUCT_NAME)
14 | CFBundlePackageType
15 | FMWK
16 | CFBundleShortVersionString
17 | 1.0
18 | CFBundleSignature
19 | ????
20 | CFBundleVersion
21 | $(CURRENT_PROJECT_VERSION)
22 | NSPrincipalClass
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/DictionaryCoding.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = "1";
4 | objectVersion = "46";
5 | objects = {
6 | "DictionaryCoding::DictionaryCoding" = {
7 | isa = "PBXNativeTarget";
8 | buildConfigurationList = "OBJ_28";
9 | buildPhases = (
10 | "OBJ_31",
11 | "OBJ_36"
12 | );
13 | dependencies = (
14 | );
15 | name = "DictionaryCoding";
16 | productName = "DictionaryCoding";
17 | productReference = "DictionaryCoding::DictionaryCoding::Product";
18 | productType = "com.apple.product-type.framework";
19 | };
20 | "DictionaryCoding::DictionaryCoding::Product" = {
21 | isa = "PBXFileReference";
22 | path = "DictionaryCoding.framework";
23 | sourceTree = "BUILT_PRODUCTS_DIR";
24 | };
25 | "DictionaryCoding::DictionaryCodingPackageTests::ProductTarget" = {
26 | isa = "PBXAggregateTarget";
27 | buildConfigurationList = "OBJ_44";
28 | buildPhases = (
29 | );
30 | dependencies = (
31 | "OBJ_47"
32 | );
33 | name = "DictionaryCodingPackageTests";
34 | productName = "DictionaryCodingPackageTests";
35 | };
36 | "DictionaryCoding::DictionaryCodingTests" = {
37 | isa = "PBXNativeTarget";
38 | buildConfigurationList = "OBJ_49";
39 | buildPhases = (
40 | "OBJ_52",
41 | "OBJ_55"
42 | );
43 | dependencies = (
44 | "OBJ_57"
45 | );
46 | name = "DictionaryCodingTests";
47 | productName = "DictionaryCodingTests";
48 | productReference = "DictionaryCoding::DictionaryCodingTests::Product";
49 | productType = "com.apple.product-type.bundle.unit-test";
50 | };
51 | "DictionaryCoding::DictionaryCodingTests::Product" = {
52 | isa = "PBXFileReference";
53 | path = "DictionaryCodingTests.xctest";
54 | sourceTree = "BUILT_PRODUCTS_DIR";
55 | };
56 | "DictionaryCoding::SwiftPMPackageDescription" = {
57 | isa = "PBXNativeTarget";
58 | buildConfigurationList = "OBJ_38";
59 | buildPhases = (
60 | "OBJ_41"
61 | );
62 | dependencies = (
63 | );
64 | name = "DictionaryCodingPackageDescription";
65 | productName = "DictionaryCodingPackageDescription";
66 | productType = "com.apple.product-type.framework";
67 | };
68 | "OBJ_1" = {
69 | isa = "PBXProject";
70 | attributes = {
71 | LastSwiftMigration = "9999";
72 | LastUpgradeCheck = "9999";
73 | };
74 | buildConfigurationList = "OBJ_2";
75 | compatibilityVersion = "Xcode 3.2";
76 | developmentRegion = "en";
77 | hasScannedForEncodings = "0";
78 | knownRegions = (
79 | "en"
80 | );
81 | mainGroup = "OBJ_5";
82 | productRefGroup = "OBJ_19";
83 | projectDirPath = ".";
84 | targets = (
85 | "DictionaryCoding::DictionaryCoding",
86 | "DictionaryCoding::SwiftPMPackageDescription",
87 | "DictionaryCoding::DictionaryCodingPackageTests::ProductTarget",
88 | "DictionaryCoding::DictionaryCodingTests"
89 | );
90 | };
91 | "OBJ_10" = {
92 | isa = "PBXGroup";
93 | children = (
94 | "OBJ_11",
95 | "OBJ_12",
96 | "OBJ_13",
97 | "OBJ_14"
98 | );
99 | name = "DictionaryCoding";
100 | path = "Sources/DictionaryCoding";
101 | sourceTree = "SOURCE_ROOT";
102 | };
103 | "OBJ_11" = {
104 | isa = "PBXFileReference";
105 | path = "DictionaryCodingKey.swift";
106 | sourceTree = "";
107 | };
108 | "OBJ_12" = {
109 | isa = "PBXFileReference";
110 | path = "DictionaryDecoder.swift";
111 | sourceTree = "";
112 | };
113 | "OBJ_13" = {
114 | isa = "PBXFileReference";
115 | path = "DictionaryEncoder.swift";
116 | sourceTree = "";
117 | };
118 | "OBJ_14" = {
119 | isa = "PBXFileReference";
120 | path = "DictionaryErrors.swift";
121 | sourceTree = "";
122 | };
123 | "OBJ_15" = {
124 | isa = "PBXGroup";
125 | children = (
126 | "OBJ_16"
127 | );
128 | name = "Tests";
129 | path = "";
130 | sourceTree = "SOURCE_ROOT";
131 | };
132 | "OBJ_16" = {
133 | isa = "PBXGroup";
134 | children = (
135 | "OBJ_17",
136 | "OBJ_18"
137 | );
138 | name = "DictionaryCodingTests";
139 | path = "Tests/DictionaryCodingTests";
140 | sourceTree = "SOURCE_ROOT";
141 | };
142 | "OBJ_17" = {
143 | isa = "PBXFileReference";
144 | path = "DictionaryDecodingTests.swift";
145 | sourceTree = "";
146 | };
147 | "OBJ_18" = {
148 | isa = "PBXFileReference";
149 | path = "DictionaryEncodingTests.swift";
150 | sourceTree = "";
151 | };
152 | "OBJ_19" = {
153 | isa = "PBXGroup";
154 | children = (
155 | "DictionaryCoding::DictionaryCodingTests::Product",
156 | "DictionaryCoding::DictionaryCoding::Product"
157 | );
158 | name = "Products";
159 | path = "";
160 | sourceTree = "BUILT_PRODUCTS_DIR";
161 | };
162 | "OBJ_2" = {
163 | isa = "XCConfigurationList";
164 | buildConfigurations = (
165 | "OBJ_3",
166 | "OBJ_4"
167 | );
168 | defaultConfigurationIsVisible = "0";
169 | defaultConfigurationName = "Release";
170 | };
171 | "OBJ_22" = {
172 | isa = "PBXFileReference";
173 | path = "DictionaryCoding.xcworkspace";
174 | sourceTree = "SOURCE_ROOT";
175 | };
176 | "OBJ_23" = {
177 | isa = "PBXFileReference";
178 | path = "LICENSE.md";
179 | sourceTree = "";
180 | };
181 | "OBJ_24" = {
182 | isa = "PBXFileReference";
183 | path = "DictionaryCoding.podspec";
184 | sourceTree = "";
185 | };
186 | "OBJ_25" = {
187 | isa = "PBXFileReference";
188 | path = "DictionaryCoding.xcconfig";
189 | sourceTree = "";
190 | };
191 | "OBJ_26" = {
192 | isa = "PBXFileReference";
193 | path = "README.md";
194 | sourceTree = "";
195 | };
196 | "OBJ_28" = {
197 | isa = "XCConfigurationList";
198 | buildConfigurations = (
199 | "OBJ_29",
200 | "OBJ_30"
201 | );
202 | defaultConfigurationIsVisible = "0";
203 | defaultConfigurationName = "Release";
204 | };
205 | "OBJ_29" = {
206 | isa = "XCBuildConfiguration";
207 | baseConfigurationReference = "OBJ_8";
208 | buildSettings = {
209 | ENABLE_TESTABILITY = "YES";
210 | FRAMEWORK_SEARCH_PATHS = (
211 | "$(inherited)",
212 | "$(PLATFORM_DIR)/Developer/Library/Frameworks"
213 | );
214 | HEADER_SEARCH_PATHS = (
215 | "$(inherited)"
216 | );
217 | INFOPLIST_FILE = "DictionaryCoding.xcodeproj/DictionaryCoding_Info.plist";
218 | IPHONEOS_DEPLOYMENT_TARGET = "10.0";
219 | LD_RUNPATH_SEARCH_PATHS = (
220 | "$(inherited)",
221 | "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx"
222 | );
223 | MACOSX_DEPLOYMENT_TARGET = "10.12";
224 | OTHER_CFLAGS = (
225 | "$(inherited)"
226 | );
227 | OTHER_LDFLAGS = (
228 | "$(inherited)"
229 | );
230 | OTHER_SWIFT_FLAGS = (
231 | "$(inherited)"
232 | );
233 | PRODUCT_BUNDLE_IDENTIFIER = "DictionaryCoding";
234 | PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)";
235 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
236 | SKIP_INSTALL = "YES";
237 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = (
238 | "$(inherited)"
239 | );
240 | SWIFT_VERSION = "5.0";
241 | TARGET_NAME = "DictionaryCoding";
242 | TVOS_DEPLOYMENT_TARGET = "9.0";
243 | WATCHOS_DEPLOYMENT_TARGET = "2.0";
244 | };
245 | name = "Debug";
246 | };
247 | "OBJ_3" = {
248 | isa = "XCBuildConfiguration";
249 | buildSettings = {
250 | CLANG_ENABLE_OBJC_ARC = "YES";
251 | COMBINE_HIDPI_IMAGES = "YES";
252 | COPY_PHASE_STRIP = "NO";
253 | DEBUG_INFORMATION_FORMAT = "dwarf";
254 | DYLIB_INSTALL_NAME_BASE = "@rpath";
255 | ENABLE_NS_ASSERTIONS = "YES";
256 | GCC_OPTIMIZATION_LEVEL = "0";
257 | GCC_PREPROCESSOR_DEFINITIONS = (
258 | "$(inherited)",
259 | "SWIFT_PACKAGE=1",
260 | "DEBUG=1"
261 | );
262 | MACOSX_DEPLOYMENT_TARGET = "10.10";
263 | ONLY_ACTIVE_ARCH = "YES";
264 | OTHER_SWIFT_FLAGS = (
265 | "$(inherited)",
266 | "-DXcode"
267 | );
268 | PRODUCT_NAME = "$(TARGET_NAME)";
269 | SDKROOT = "macosx";
270 | SUPPORTED_PLATFORMS = (
271 | "macosx",
272 | "iphoneos",
273 | "iphonesimulator",
274 | "appletvos",
275 | "appletvsimulator",
276 | "watchos",
277 | "watchsimulator"
278 | );
279 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = (
280 | "$(inherited)",
281 | "SWIFT_PACKAGE",
282 | "DEBUG"
283 | );
284 | SWIFT_OPTIMIZATION_LEVEL = "-Onone";
285 | USE_HEADERMAP = "NO";
286 | };
287 | name = "Debug";
288 | };
289 | "OBJ_30" = {
290 | isa = "XCBuildConfiguration";
291 | baseConfigurationReference = "OBJ_8";
292 | buildSettings = {
293 | ENABLE_TESTABILITY = "YES";
294 | FRAMEWORK_SEARCH_PATHS = (
295 | "$(inherited)",
296 | "$(PLATFORM_DIR)/Developer/Library/Frameworks"
297 | );
298 | HEADER_SEARCH_PATHS = (
299 | "$(inherited)"
300 | );
301 | INFOPLIST_FILE = "DictionaryCoding.xcodeproj/DictionaryCoding_Info.plist";
302 | IPHONEOS_DEPLOYMENT_TARGET = "10.0";
303 | LD_RUNPATH_SEARCH_PATHS = (
304 | "$(inherited)",
305 | "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx"
306 | );
307 | MACOSX_DEPLOYMENT_TARGET = "10.12";
308 | OTHER_CFLAGS = (
309 | "$(inherited)"
310 | );
311 | OTHER_LDFLAGS = (
312 | "$(inherited)"
313 | );
314 | OTHER_SWIFT_FLAGS = (
315 | "$(inherited)"
316 | );
317 | PRODUCT_BUNDLE_IDENTIFIER = "DictionaryCoding";
318 | PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)";
319 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
320 | SKIP_INSTALL = "YES";
321 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = (
322 | "$(inherited)"
323 | );
324 | SWIFT_VERSION = "5.0";
325 | TARGET_NAME = "DictionaryCoding";
326 | TVOS_DEPLOYMENT_TARGET = "9.0";
327 | WATCHOS_DEPLOYMENT_TARGET = "2.0";
328 | };
329 | name = "Release";
330 | };
331 | "OBJ_31" = {
332 | isa = "PBXSourcesBuildPhase";
333 | files = (
334 | "OBJ_32",
335 | "OBJ_33",
336 | "OBJ_34",
337 | "OBJ_35"
338 | );
339 | };
340 | "OBJ_32" = {
341 | isa = "PBXBuildFile";
342 | fileRef = "OBJ_11";
343 | };
344 | "OBJ_33" = {
345 | isa = "PBXBuildFile";
346 | fileRef = "OBJ_12";
347 | };
348 | "OBJ_34" = {
349 | isa = "PBXBuildFile";
350 | fileRef = "OBJ_13";
351 | };
352 | "OBJ_35" = {
353 | isa = "PBXBuildFile";
354 | fileRef = "OBJ_14";
355 | };
356 | "OBJ_36" = {
357 | isa = "PBXFrameworksBuildPhase";
358 | files = (
359 | );
360 | };
361 | "OBJ_38" = {
362 | isa = "XCConfigurationList";
363 | buildConfigurations = (
364 | "OBJ_39",
365 | "OBJ_40"
366 | );
367 | defaultConfigurationIsVisible = "0";
368 | defaultConfigurationName = "Release";
369 | };
370 | "OBJ_39" = {
371 | isa = "XCBuildConfiguration";
372 | buildSettings = {
373 | LD = "/usr/bin/true";
374 | OTHER_SWIFT_FLAGS = (
375 | "-swift-version",
376 | "5",
377 | "-I",
378 | "$(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2",
379 | "-target",
380 | "x86_64-apple-macosx10.10",
381 | "-sdk",
382 | "/Applications/Xcode11.2.1gm.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk",
383 | "-package-description-version",
384 | "5"
385 | );
386 | SWIFT_VERSION = "5.0";
387 | };
388 | name = "Debug";
389 | };
390 | "OBJ_4" = {
391 | isa = "XCBuildConfiguration";
392 | buildSettings = {
393 | CLANG_ENABLE_OBJC_ARC = "YES";
394 | COMBINE_HIDPI_IMAGES = "YES";
395 | COPY_PHASE_STRIP = "YES";
396 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
397 | DYLIB_INSTALL_NAME_BASE = "@rpath";
398 | GCC_OPTIMIZATION_LEVEL = "s";
399 | GCC_PREPROCESSOR_DEFINITIONS = (
400 | "$(inherited)",
401 | "SWIFT_PACKAGE=1"
402 | );
403 | MACOSX_DEPLOYMENT_TARGET = "10.10";
404 | OTHER_SWIFT_FLAGS = (
405 | "$(inherited)",
406 | "-DXcode"
407 | );
408 | PRODUCT_NAME = "$(TARGET_NAME)";
409 | SDKROOT = "macosx";
410 | SUPPORTED_PLATFORMS = (
411 | "macosx",
412 | "iphoneos",
413 | "iphonesimulator",
414 | "appletvos",
415 | "appletvsimulator",
416 | "watchos",
417 | "watchsimulator"
418 | );
419 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = (
420 | "$(inherited)",
421 | "SWIFT_PACKAGE"
422 | );
423 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
424 | USE_HEADERMAP = "NO";
425 | };
426 | name = "Release";
427 | };
428 | "OBJ_40" = {
429 | isa = "XCBuildConfiguration";
430 | buildSettings = {
431 | LD = "/usr/bin/true";
432 | OTHER_SWIFT_FLAGS = (
433 | "-swift-version",
434 | "5",
435 | "-I",
436 | "$(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2",
437 | "-target",
438 | "x86_64-apple-macosx10.10",
439 | "-sdk",
440 | "/Applications/Xcode11.2.1gm.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk",
441 | "-package-description-version",
442 | "5"
443 | );
444 | SWIFT_VERSION = "5.0";
445 | };
446 | name = "Release";
447 | };
448 | "OBJ_41" = {
449 | isa = "PBXSourcesBuildPhase";
450 | files = (
451 | "OBJ_42"
452 | );
453 | };
454 | "OBJ_42" = {
455 | isa = "PBXBuildFile";
456 | fileRef = "OBJ_6";
457 | };
458 | "OBJ_44" = {
459 | isa = "XCConfigurationList";
460 | buildConfigurations = (
461 | "OBJ_45",
462 | "OBJ_46"
463 | );
464 | defaultConfigurationIsVisible = "0";
465 | defaultConfigurationName = "Release";
466 | };
467 | "OBJ_45" = {
468 | isa = "XCBuildConfiguration";
469 | buildSettings = {
470 | };
471 | name = "Debug";
472 | };
473 | "OBJ_46" = {
474 | isa = "XCBuildConfiguration";
475 | buildSettings = {
476 | };
477 | name = "Release";
478 | };
479 | "OBJ_47" = {
480 | isa = "PBXTargetDependency";
481 | target = "DictionaryCoding::DictionaryCodingTests";
482 | };
483 | "OBJ_49" = {
484 | isa = "XCConfigurationList";
485 | buildConfigurations = (
486 | "OBJ_50",
487 | "OBJ_51"
488 | );
489 | defaultConfigurationIsVisible = "0";
490 | defaultConfigurationName = "Release";
491 | };
492 | "OBJ_5" = {
493 | isa = "PBXGroup";
494 | children = (
495 | "OBJ_6",
496 | "OBJ_7",
497 | "OBJ_9",
498 | "OBJ_15",
499 | "OBJ_19",
500 | "OBJ_22",
501 | "OBJ_23",
502 | "OBJ_24",
503 | "OBJ_25",
504 | "OBJ_26"
505 | );
506 | path = "";
507 | sourceTree = "";
508 | };
509 | "OBJ_50" = {
510 | isa = "XCBuildConfiguration";
511 | baseConfigurationReference = "OBJ_8";
512 | buildSettings = {
513 | CLANG_ENABLE_MODULES = "YES";
514 | EMBEDDED_CONTENT_CONTAINS_SWIFT = "YES";
515 | FRAMEWORK_SEARCH_PATHS = (
516 | "$(inherited)",
517 | "$(PLATFORM_DIR)/Developer/Library/Frameworks"
518 | );
519 | HEADER_SEARCH_PATHS = (
520 | "$(inherited)"
521 | );
522 | INFOPLIST_FILE = "DictionaryCoding.xcodeproj/DictionaryCodingTests_Info.plist";
523 | IPHONEOS_DEPLOYMENT_TARGET = "10.0";
524 | LD_RUNPATH_SEARCH_PATHS = (
525 | "$(inherited)",
526 | "@loader_path/../Frameworks",
527 | "@loader_path/Frameworks"
528 | );
529 | MACOSX_DEPLOYMENT_TARGET = "10.12";
530 | OTHER_CFLAGS = (
531 | "$(inherited)"
532 | );
533 | OTHER_LDFLAGS = (
534 | "$(inherited)"
535 | );
536 | OTHER_SWIFT_FLAGS = (
537 | "$(inherited)"
538 | );
539 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = (
540 | "$(inherited)"
541 | );
542 | SWIFT_VERSION = "5.0";
543 | TARGET_NAME = "DictionaryCodingTests";
544 | TVOS_DEPLOYMENT_TARGET = "9.0";
545 | WATCHOS_DEPLOYMENT_TARGET = "2.0";
546 | };
547 | name = "Debug";
548 | };
549 | "OBJ_51" = {
550 | isa = "XCBuildConfiguration";
551 | baseConfigurationReference = "OBJ_8";
552 | buildSettings = {
553 | CLANG_ENABLE_MODULES = "YES";
554 | EMBEDDED_CONTENT_CONTAINS_SWIFT = "YES";
555 | FRAMEWORK_SEARCH_PATHS = (
556 | "$(inherited)",
557 | "$(PLATFORM_DIR)/Developer/Library/Frameworks"
558 | );
559 | HEADER_SEARCH_PATHS = (
560 | "$(inherited)"
561 | );
562 | INFOPLIST_FILE = "DictionaryCoding.xcodeproj/DictionaryCodingTests_Info.plist";
563 | IPHONEOS_DEPLOYMENT_TARGET = "10.0";
564 | LD_RUNPATH_SEARCH_PATHS = (
565 | "$(inherited)",
566 | "@loader_path/../Frameworks",
567 | "@loader_path/Frameworks"
568 | );
569 | MACOSX_DEPLOYMENT_TARGET = "10.12";
570 | OTHER_CFLAGS = (
571 | "$(inherited)"
572 | );
573 | OTHER_LDFLAGS = (
574 | "$(inherited)"
575 | );
576 | OTHER_SWIFT_FLAGS = (
577 | "$(inherited)"
578 | );
579 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = (
580 | "$(inherited)"
581 | );
582 | SWIFT_VERSION = "5.0";
583 | TARGET_NAME = "DictionaryCodingTests";
584 | TVOS_DEPLOYMENT_TARGET = "9.0";
585 | WATCHOS_DEPLOYMENT_TARGET = "2.0";
586 | };
587 | name = "Release";
588 | };
589 | "OBJ_52" = {
590 | isa = "PBXSourcesBuildPhase";
591 | files = (
592 | "OBJ_53",
593 | "OBJ_54"
594 | );
595 | };
596 | "OBJ_53" = {
597 | isa = "PBXBuildFile";
598 | fileRef = "OBJ_17";
599 | };
600 | "OBJ_54" = {
601 | isa = "PBXBuildFile";
602 | fileRef = "OBJ_18";
603 | };
604 | "OBJ_55" = {
605 | isa = "PBXFrameworksBuildPhase";
606 | files = (
607 | "OBJ_56"
608 | );
609 | };
610 | "OBJ_56" = {
611 | isa = "PBXBuildFile";
612 | fileRef = "DictionaryCoding::DictionaryCoding::Product";
613 | };
614 | "OBJ_57" = {
615 | isa = "PBXTargetDependency";
616 | target = "DictionaryCoding::DictionaryCoding";
617 | };
618 | "OBJ_6" = {
619 | isa = "PBXFileReference";
620 | explicitFileType = "sourcecode.swift";
621 | path = "Package.swift";
622 | sourceTree = "";
623 | };
624 | "OBJ_7" = {
625 | isa = "PBXGroup";
626 | children = (
627 | "OBJ_8"
628 | );
629 | name = "Configs";
630 | path = "";
631 | sourceTree = "";
632 | };
633 | "OBJ_8" = {
634 | isa = "PBXFileReference";
635 | path = "DictionaryCoding.xcconfig";
636 | sourceTree = "";
637 | };
638 | "OBJ_9" = {
639 | isa = "PBXGroup";
640 | children = (
641 | "OBJ_10"
642 | );
643 | name = "Sources";
644 | path = "";
645 | sourceTree = "SOURCE_ROOT";
646 | };
647 | };
648 | rootObject = "OBJ_1";
649 | }
650 |
--------------------------------------------------------------------------------
/DictionaryCoding.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
--------------------------------------------------------------------------------
/DictionaryCoding.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEWorkspaceSharedSettings_AutocreateContextsIfNeeded
6 |
7 |
8 |
--------------------------------------------------------------------------------
/DictionaryCoding.xcodeproj/xcshareddata/xcschemes/DictionaryCoding-Package.xcscheme:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
9 |
15 |
21 |
22 |
23 |
24 |
25 |
30 |
31 |
33 |
39 |
40 |
41 |
42 |
43 |
53 |
54 |
60 |
61 |
63 |
64 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/DictionaryCoding.xcodeproj/xcshareddata/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | SchemeUserState
5 |
6 | DictionaryCoding-Package.xcscheme
7 |
8 |
9 | SuppressBuildableAutocreation
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Elegant Chaos Limited
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Package.resolved:
--------------------------------------------------------------------------------
1 | {
2 | "object": {
3 | "pins": [
4 |
5 | ]
6 | },
7 | "version": 1
8 | }
9 |
--------------------------------------------------------------------------------
/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:5.0
2 |
3 | import PackageDescription
4 |
5 | let package = Package(
6 | name: "DictionaryCoding",
7 | platforms: [
8 | .macOS(.v10_12), .iOS(.v10), .tvOS(.v10)
9 | ],
10 | products: [
11 | .library(
12 | name: "DictionaryCoding",
13 | targets: ["DictionaryCoding"]),
14 | ],
15 | dependencies: [
16 | ],
17 | targets: [
18 | .target(
19 | name: "DictionaryCoding",
20 | dependencies: []),
21 | .testTarget(
22 | name: "DictionaryCodingTests",
23 | dependencies: ["DictionaryCoding"]),
24 | ]
25 | )
26 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [comment]: <> (Header Generated by ActionStatus 2.0 - 377)
2 |
3 | [![Test results][tests shield]][actions] [![Latest release][release shield]][releases] [![swift 5.0 shield] ![swift dev shield]][swift] ![Platforms: macOS, iOS, tvOS, watchOS][platforms shield]
4 |
5 | [release shield]: https://img.shields.io/github/v/release/elegantchaos/DictionaryCoding
6 | [platforms shield]: https://img.shields.io/badge/platforms-macOS_iOS_tvOS_watchOS-lightgrey.svg?style=flat "macOS, iOS, tvOS, watchOS"
7 | [tests shield]: https://github.com/elegantchaos/DictionaryCoding/workflows/Tests/badge.svg
8 | [swift 5.0 shield]: https://img.shields.io/badge/swift-5.0-F05138.svg "Swift 5.0"
9 | [swift dev shield]: https://img.shields.io/badge/swift-dev-F05138.svg "Swift dev"
10 |
11 | [swift]: https://swift.org
12 | [releases]: https://github.com/elegantchaos/DictionaryCoding/releases
13 | [actions]: https://github.com/elegantchaos/DictionaryCoding/actions
14 |
15 | [comment]: <> (End of ActionStatus Header)
16 |
17 | [![Test results][tests shield]][actions] [![Latest release][release shield]][releases] [![Swift 5.0][swift shield]][swift] ![Platforms: iOS, macOS, tvOS, watchOS, Linux][platforms shield]
18 |
19 | [swift]: https://swift.org
20 |
21 | [releases]: https://github.com/elegantchaos/DictionaryCoding/releases
22 | [actions]: https://github.com/elegantchaos/DictionaryCoding/actions
23 |
24 | [release shield]: https://img.shields.io/github/v/release/elegantchaos/DictionaryCoding
25 | [swift shield]: https://img.shields.io/badge/swift-5.0-F05138.svg "Swift 5.0"
26 | [platforms shield]: https://img.shields.io/badge/platforms-iOS_macOS_tvOS_watchOS_Linux-lightgrey.svg?style=flat "iOS, macOS, tvOS, watchOS, Linux"
27 | [tests shield]: https://github.com/elegantchaos/DictionaryCoding/workflows/Tests/badge.svg
28 |
29 | # DictionaryCoding
30 |
31 | This is an implementation of Swift's Encoder/Decoder protocols which uses `NSDictionary` as its underlying container mechanism.
32 |
33 | It allows you to take a native swift class or struct that confirms to the Codable protocol and convert it to, or initialise it from, a dictionary.
34 |
35 | A lot of the code is actually taken from the Swift Foundation library's own `JSONEncoder` and `JSONDecoder` classes.
36 |
37 | It turns out that those class actually work by using `NSDictionary` as an intermediate step between JSON and the native type to be encoded/decoded. Unfortunately the underlying `NSDictionary` support isn't exposed by Foundation, which is why I've done so here.
38 |
39 | See [this blog post](http://elegantchaos.com/2018/02/21/decoding-dictionaries-in-swift.html) for a bit more detail!
40 |
41 | ### Build Instructions
42 |
43 | At the moment this module is best built using the Swift Package Manager with `swift build`.
44 |
45 | The unit tests can be run with `swift test`.
46 |
47 | An Xcode project can be generated with `swift package generate-xcodeproj --xcconfig-overrides DictionaryCoding.xcconfig`.
48 |
49 | A CocoaPods `.podspec` file is included. I don't use CocoaPods myself though, so I can't be entirely sure that I haven't broken something (or forgotten to update something).
50 |
51 | Please file issues (or even better, pull requests) for support for other build systems.
52 |
--------------------------------------------------------------------------------
/Sources/DictionaryCoding/DictionaryCodingKey.swift:
--------------------------------------------------------------------------------
1 | //===----------------------------------------------------------------------===//
2 | //
3 | // This source file is largely a copy of code from Swift.org open source project's
4 | // files JSONEncoder.swift and Codeable.swift.
5 | //
6 | // Unfortunately those files do not expose the internal _JSONEncoder and
7 | // _JSONDecoder classes, which are in fact dictionary encoder/decoders and
8 | // precisely what we want...
9 | //
10 | // The original code is copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
11 | // Licensed under Apache License v2.0 with Runtime Library Exception
12 | //
13 | // See https://swift.org/LICENSE.txt for license information
14 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
15 | //
16 | // Modifications and additional code here is copyright (c) 2018 Sam Deane, and
17 | // is licensed under the same terms.
18 | //
19 | //===----------------------------------------------------------------------===//
20 |
21 | import Foundation
22 |
23 | internal struct DictionaryCodingKey : CodingKey {
24 | public var stringValue: String
25 | public var intValue: Int?
26 |
27 | public init?(stringValue: String) {
28 | self.stringValue = stringValue
29 | self.intValue = nil
30 | }
31 |
32 | public init?(intValue: Int) {
33 | self.stringValue = "\(intValue)"
34 | self.intValue = intValue
35 | }
36 |
37 | public init(stringValue: String, intValue: Int?) {
38 | self.stringValue = stringValue
39 | self.intValue = intValue
40 | }
41 |
42 | internal init(index: Int) {
43 | self.stringValue = "Index \(index)"
44 | self.intValue = index
45 | }
46 |
47 | internal static let `super` = DictionaryCodingKey(stringValue: "super")!
48 | }
49 |
50 |
--------------------------------------------------------------------------------
/Sources/DictionaryCoding/DictionaryDecoder.swift:
--------------------------------------------------------------------------------
1 | //===----------------------------------------------------------------------===//
2 | //
3 | // This source file is largely a copy of code from Swift.org open source project's
4 | // files JSONEncoder.swift and Codeable.swift.
5 | //
6 | // Unfortunately those files do not expose the internal _JSONEncoder and
7 | // _JSONDecoder classes, which are in fact dictionary encoder/decoders and
8 | // precisely what we want...
9 | //
10 | // The original code is copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
11 | // Licensed under Apache License v2.0 with Runtime Library Exception
12 | //
13 | // See https://swift.org/LICENSE.txt for license information
14 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
15 | //
16 | // Modifications and additional code here is copyright (c) 2018 Sam Deane, and
17 | // is licensed under the same terms.
18 | //
19 | //===----------------------------------------------------------------------===//
20 |
21 | import Foundation
22 |
23 |
24 | //===----------------------------------------------------------------------===//
25 | // Dictionary Decoder
26 | //===----------------------------------------------------------------------===//
27 | /// `DictionaryDecoder` facilitates the decoding of Dictionary into semantic `Decodable` types.
28 | open class DictionaryDecoder {
29 | // MARK: Options
30 | /// The strategy to use for decoding `Date` values.
31 | public enum DateDecodingStrategy {
32 | /// Defer to `Date` for decoding. This is the default strategy.
33 | case deferredToDate
34 |
35 | /// Decode the `Date` as a UNIX timestamp from a Dictionary number.
36 | case secondsSince1970
37 |
38 | /// Decode the `Date` as UNIX millisecond timestamp from a Dictionary number.
39 | case millisecondsSince1970
40 |
41 | /// Decode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
42 | @available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
43 | case iso8601
44 |
45 | /// Decode the `Date` as a string parsed by the given formatter.
46 | case formatted(DateFormatter)
47 |
48 | /// Decode the `Date` as a custom value decoded by the given closure.
49 | case custom((_ decoder: Decoder) throws -> Date)
50 | }
51 |
52 | /// The strategy to use for decoding `Data` values.
53 | public enum DataDecodingStrategy {
54 | /// Defer to `Data` for decoding.
55 | case deferredToData
56 |
57 | /// Decode the `Data` from a Base64-encoded string. This is the default strategy.
58 | case base64
59 |
60 | /// Decode the `Data` as a custom value decoded by the given closure.
61 | case custom((_ decoder: Decoder) throws -> Data)
62 | }
63 |
64 | /// The strategy to use for non-Dictionary-conforming floating-point values (IEEE 754 infinity and NaN).
65 | public enum NonConformingFloatDecodingStrategy {
66 | /// Throw upon encountering non-conforming values. This is the default strategy.
67 | case `throw`
68 |
69 | /// Decode the values from the given representation strings.
70 | case convertFromString(positiveInfinity: String, negativeInfinity: String, nan: String)
71 | }
72 |
73 | /// The strategy to use when decoding missing keys.
74 | public enum MissingValueDecodingStrategy {
75 | /// Throw upon encountering missing values.
76 | case `throw`
77 |
78 | /// Attempt to use a default value when encountering missing values for standard types.
79 | case useStandardDefault
80 |
81 | /// Attempt to use a default value when encountering missing values.
82 | /// The default value is read from the associated dictionary, keyed by the name of the type.
83 | case useDefault(defaults : [String:Any])
84 | }
85 |
86 | /// The strategy to use for automatically changing the value of keys before decoding.
87 | public enum KeyDecodingStrategy {
88 | /// Use the keys specified by each type. This is the default strategy.
89 | case useDefaultKeys
90 |
91 | /// Convert from "snake_case_keys" to "camelCaseKeys" before attempting to match a key with the one specified by each type.
92 | ///
93 | /// The conversion to upper case uses `Locale.system`, also known as the ICU "root" locale. This means the result is consistent regardless of the current user's locale and language preferences.
94 | ///
95 | /// Converting from snake case to camel case:
96 | /// 1. Capitalizes the word starting after each `_`
97 | /// 2. Removes all `_`
98 | /// 3. Preserves starting and ending `_` (as these are often used to indicate private variables or other metadata).
99 | /// For example, `one_two_three` becomes `oneTwoThree`. `_one_two_three_` becomes `_oneTwoThree_`.
100 | ///
101 | /// - Note: Using a key decoding strategy has a nominal performance cost, as each string key has to be inspected for the `_` character.
102 | case convertFromSnakeCase
103 |
104 | /// Provide a custom conversion from the key in the encoded Dictionary to the keys specified by the decoded types.
105 | /// The full path to the current decoding position is provided for context (in case you need to locate this key within the payload). The returned key is used in place of the last component in the coding path before decoding.
106 | /// If the result of the conversion is a duplicate key, then only one value will be present in the container for the type to decode from.
107 | case custom((_ codingPath: [CodingKey]) -> CodingKey)
108 |
109 | fileprivate static func _convertFromSnakeCase(_ stringKey: String) -> String {
110 | guard !stringKey.isEmpty else { return stringKey }
111 |
112 | // Find the first non-underscore character
113 | guard let firstNonUnderscore = stringKey.firstIndex(where: { $0 != "_" }) else {
114 | // Reached the end without finding an _
115 | return stringKey
116 | }
117 |
118 | // Find the last non-underscore character
119 | var lastNonUnderscore = stringKey.index(before: stringKey.endIndex)
120 | while lastNonUnderscore > firstNonUnderscore && stringKey[lastNonUnderscore] == "_" {
121 | stringKey.formIndex(before: &lastNonUnderscore);
122 | }
123 |
124 | let keyRange = firstNonUnderscore...lastNonUnderscore
125 | let leadingUnderscoreRange = stringKey.startIndex..(_ type: T.Type, from dictionary: NSDictionary) throws -> T {
231 | let decoder = _DictionaryDecoder(referencing: dictionary, options: self.options)
232 | guard let value = try decoder.unbox(dictionary, as: type) else {
233 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: [], debugDescription: "The given data did not contain a top-level value."))
234 | }
235 |
236 | return value
237 | }
238 |
239 | /// Decodes a top-level value of the given type from the given Dictionary representation.
240 | ///
241 | /// - parameter type: The type of the value to decode.
242 | /// - parameter data: The data to decode from.
243 | /// - returns: A value of the requested type.
244 | /// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not valid Dictionary.
245 | /// - throws: An error if any value throws an error during decoding.
246 | open func decode(_ type: T.Type, from dictionary: [String:Any]) throws -> T {
247 | let decoder = _DictionaryDecoder(referencing: dictionary, options: self.options)
248 | guard let value = try decoder.unbox(dictionary, as: type) else {
249 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: [], debugDescription: "The given data did not contain a top-level value."))
250 | }
251 |
252 | return value
253 | }
254 |
255 | }
256 |
257 | // MARK: - _DictionaryDecoder
258 | fileprivate class _DictionaryDecoder : Decoder {
259 | // MARK: Properties
260 | /// The decoder's storage.
261 | fileprivate var storage: _DictionaryDecodingStorage
262 |
263 | /// Options set on the top-level decoder.
264 | fileprivate let options: DictionaryDecoder._Options
265 |
266 | /// The path to the current point in encoding.
267 | fileprivate(set) public var codingPath: [CodingKey]
268 |
269 | /// Contextual user-provided information for use during encoding.
270 | public var userInfo: [CodingUserInfoKey : Any] {
271 | return self.options.userInfo
272 | }
273 |
274 | // MARK: - Initialization
275 | /// Initializes `self` with the given top-level container and options.
276 | fileprivate init(referencing container: Any, at codingPath: [CodingKey] = [], options: DictionaryDecoder._Options) {
277 | self.storage = _DictionaryDecodingStorage()
278 | self.storage.push(container: container)
279 | self.codingPath = codingPath
280 | self.options = options
281 | }
282 |
283 | // MARK: - Decoder Methods
284 | public func container(keyedBy type: Key.Type) throws -> KeyedDecodingContainer {
285 | guard !(self.storage.topContainer is NSNull) else {
286 | throw DecodingError.valueNotFound(KeyedDecodingContainer.self,
287 | DecodingError.Context(codingPath: self.codingPath,
288 | debugDescription: "Cannot get keyed decoding container -- found null value instead."))
289 | }
290 |
291 | guard let topContainer = self.storage.topContainer as? [String : Any] else {
292 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: self.storage.topContainer)
293 | }
294 |
295 | let container = DictionaryCodingKeyedDecodingContainer(referencing: self, wrapping: topContainer)
296 | return KeyedDecodingContainer(container)
297 | }
298 |
299 | public func unkeyedContainer() throws -> UnkeyedDecodingContainer {
300 | guard !(self.storage.topContainer is NSNull) else {
301 | throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self,
302 | DecodingError.Context(codingPath: self.codingPath,
303 | debugDescription: "Cannot get unkeyed decoding container -- found null value instead."))
304 | }
305 |
306 | guard let topContainer = self.storage.topContainer as? [Any] else {
307 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: self.storage.topContainer)
308 | }
309 |
310 | return _DictionaryUnkeyedDecodingContainer(referencing: self, wrapping: topContainer)
311 | }
312 |
313 | public func singleValueContainer() throws -> SingleValueDecodingContainer {
314 | return self
315 | }
316 | }
317 |
318 | // MARK: - Decoding Storage
319 | fileprivate struct _DictionaryDecodingStorage {
320 | // MARK: Properties
321 | /// The container stack.
322 | /// Elements may be any one of the Dictionary types (NSNull, NSNumber, String, Array, [String : Any]).
323 | private(set) fileprivate var containers: [Any] = []
324 |
325 | // MARK: - Initialization
326 | /// Initializes `self` with no containers.
327 | fileprivate init() {}
328 |
329 | // MARK: - Modifying the Stack
330 | fileprivate var count: Int {
331 | return self.containers.count
332 | }
333 |
334 | fileprivate var topContainer: Any {
335 | precondition(self.containers.count > 0, "Empty container stack.")
336 | return self.containers.last!
337 | }
338 |
339 | fileprivate mutating func push(container: Any) {
340 | self.containers.append(container)
341 | }
342 |
343 | fileprivate mutating func popContainer() {
344 | precondition(self.containers.count > 0, "Empty container stack.")
345 | self.containers.removeLast()
346 | }
347 | }
348 |
349 | // MARK: Decoding Containers
350 | fileprivate struct DictionaryCodingKeyedDecodingContainer : KeyedDecodingContainerProtocol {
351 | typealias Key = K
352 |
353 | // MARK: Properties
354 | /// A reference to the decoder we're reading from.
355 | private let decoder: _DictionaryDecoder
356 |
357 | /// A reference to the container we're reading from.
358 | private let container: [String : Any]
359 |
360 | /// The path of coding keys taken to get to this point in decoding.
361 | private(set) public var codingPath: [CodingKey]
362 |
363 | // MARK: - Initialization
364 | /// Initializes `self` by referencing the given decoder and container.
365 | fileprivate init(referencing decoder: _DictionaryDecoder, wrapping container: [String : Any]) {
366 | self.decoder = decoder
367 | switch decoder.options.keyDecodingStrategy {
368 | case .useDefaultKeys:
369 | self.container = container
370 | case .convertFromSnakeCase:
371 | // Convert the snake case keys in the container to camel case.
372 | // If we hit a duplicate key after conversion, then we'll use the first one we saw. Effectively an undefined behavior with Dictionary dictionaries.
373 | self.container = Dictionary(container.map {
374 | key, value in (DictionaryDecoder.KeyDecodingStrategy._convertFromSnakeCase(key), value)
375 | }, uniquingKeysWith: { (first, _) in first })
376 | case .custom(let converter):
377 | self.container = Dictionary(container.map {
378 | key, value in (converter(decoder.codingPath + [DictionaryCodingKey(stringValue: key, intValue: nil)]).stringValue, value)
379 | }, uniquingKeysWith: { (first, _) in first })
380 | }
381 | self.codingPath = decoder.codingPath
382 | }
383 |
384 | // MARK: - KeyedDecodingContainerProtocol Methods
385 | public var allKeys: [Key] {
386 | #if swift(>=4.1)
387 | return self.container.keys.compactMap { Key(stringValue: $0) }
388 | #else
389 | return self.container.keys.flatMap { Key(stringValue: $0) }
390 | #endif
391 | }
392 |
393 | public func contains(_ key: Key) -> Bool {
394 | return self.container[key.stringValue] != nil
395 | }
396 |
397 | internal func notFoundError(key: Key) -> DecodingError {
398 | return DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
399 | }
400 |
401 | internal func nullFoundError(type: T.Type) -> DecodingError {
402 | return DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
403 | }
404 |
405 | private func _errorDescription(of key: CodingKey) -> String {
406 | switch decoder.options.keyDecodingStrategy {
407 | case .convertFromSnakeCase:
408 | // In this case we can attempt to recover the original value by reversing the transform
409 | let original = key.stringValue
410 | let converted = DictionaryEncoder.KeyEncodingStrategy._convertToSnakeCase(original)
411 | if converted == original {
412 | return "\(key) (\"\(original)\")"
413 | } else {
414 | return "\(key) (\"\(original)\"), converted to \(converted)"
415 | }
416 | default:
417 | // Otherwise, just report the converted string
418 | return "\(key) (\"\(key.stringValue)\")"
419 | }
420 | }
421 |
422 | public func decodeNil(forKey key: Key) throws -> Bool {
423 | guard let entry = self.container[key.stringValue] else {
424 | throw notFoundError(key: key)
425 | }
426 |
427 | return entry is NSNull
428 | }
429 |
430 | internal func decode(_ type: T.Type, forKey key: Key) throws -> T {
431 | guard let entry = self.container[key.stringValue] else {
432 | switch (decoder.options.missingValueDecodingStrategy) {
433 | case let .useDefault(defaults):
434 | let defaultKey = "\(type)"
435 | if let def = defaults[defaultKey] as? T {
436 | return def
437 | }
438 | default:
439 | break
440 | }
441 |
442 | throw notFoundError(key: key)
443 | }
444 |
445 | self.decoder.codingPath.append(key)
446 | defer { self.decoder.codingPath.removeLast() }
447 |
448 | guard let value = try self.decoder.unbox(entry, as: type) else {
449 | throw nullFoundError(type: type)
450 | }
451 |
452 | return value
453 | }
454 |
455 | public func nestedContainer(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer {
456 | self.decoder.codingPath.append(key)
457 | defer { self.decoder.codingPath.removeLast() }
458 |
459 | guard let value = self.container[key.stringValue] else {
460 | throw DecodingError.keyNotFound(key,
461 | DecodingError.Context(codingPath: self.codingPath,
462 | debugDescription: "Cannot get \(KeyedDecodingContainer.self) -- no value found for key \(_errorDescription(of: key))"))
463 | }
464 |
465 | guard let dictionary = value as? [String : Any] else {
466 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: value)
467 | }
468 |
469 | let container = DictionaryCodingKeyedDecodingContainer(referencing: self.decoder, wrapping: dictionary)
470 | return KeyedDecodingContainer(container)
471 | }
472 |
473 | public func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer {
474 | self.decoder.codingPath.append(key)
475 | defer { self.decoder.codingPath.removeLast() }
476 |
477 | guard let value = self.container[key.stringValue] else {
478 | throw DecodingError.keyNotFound(key,
479 | DecodingError.Context(codingPath: self.codingPath,
480 | debugDescription: "Cannot get UnkeyedDecodingContainer -- no value found for key \(_errorDescription(of: key))"))
481 | }
482 |
483 | guard let array = value as? [Any] else {
484 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: value)
485 | }
486 |
487 | return _DictionaryUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array)
488 | }
489 |
490 | private func _superDecoder(forKey key: CodingKey) throws -> Decoder {
491 | self.decoder.codingPath.append(key)
492 | defer { self.decoder.codingPath.removeLast() }
493 |
494 | let value: Any = self.container[key.stringValue] ?? NSNull()
495 | return _DictionaryDecoder(referencing: value, at: self.decoder.codingPath, options: self.decoder.options)
496 | }
497 |
498 | public func superDecoder() throws -> Decoder {
499 | return try _superDecoder(forKey: DictionaryCodingKey.super)
500 | }
501 |
502 | public func superDecoder(forKey key: Key) throws -> Decoder {
503 | return try _superDecoder(forKey: key)
504 | }
505 | }
506 |
507 | fileprivate struct _DictionaryUnkeyedDecodingContainer : UnkeyedDecodingContainer {
508 | // MARK: Properties
509 | /// A reference to the decoder we're reading from.
510 | private let decoder: _DictionaryDecoder
511 |
512 | /// A reference to the container we're reading from.
513 | private let container: [Any]
514 |
515 | /// The path of coding keys taken to get to this point in decoding.
516 | private(set) public var codingPath: [CodingKey]
517 |
518 | /// The index of the element we're about to decode.
519 | private(set) public var currentIndex: Int
520 |
521 | // MARK: - Initialization
522 | /// Initializes `self` by referencing the given decoder and container.
523 | fileprivate init(referencing decoder: _DictionaryDecoder, wrapping container: [Any]) {
524 | self.decoder = decoder
525 | self.container = container
526 | self.codingPath = decoder.codingPath
527 | self.currentIndex = 0
528 | }
529 |
530 | // MARK: - UnkeyedDecodingContainer Methods
531 | public var count: Int? {
532 | return self.container.count
533 | }
534 |
535 | public var isAtEnd: Bool {
536 | return self.currentIndex >= self.count!
537 | }
538 |
539 | public mutating func decodeNil() throws -> Bool {
540 | guard !self.isAtEnd else {
541 | throw DecodingError.valueNotFound(Any?.self, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
542 | }
543 |
544 | if self.container[self.currentIndex] is NSNull {
545 | self.currentIndex += 1
546 | return true
547 | } else {
548 | return false
549 | }
550 | }
551 |
552 | public mutating func decode(_ type: Bool.Type) throws -> Bool {
553 | guard !self.isAtEnd else {
554 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
555 | }
556 |
557 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
558 | defer { self.decoder.codingPath.removeLast() }
559 |
560 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Bool.self) else {
561 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
562 | }
563 |
564 | self.currentIndex += 1
565 | return decoded
566 | }
567 |
568 | public mutating func decode(_ type: Int.Type) throws -> Int {
569 | guard !self.isAtEnd else {
570 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
571 | }
572 |
573 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
574 | defer { self.decoder.codingPath.removeLast() }
575 |
576 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int.self) else {
577 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
578 | }
579 |
580 | self.currentIndex += 1
581 | return decoded
582 | }
583 |
584 | public mutating func decode(_ type: Int8.Type) throws -> Int8 {
585 | guard !self.isAtEnd else {
586 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
587 | }
588 |
589 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
590 | defer { self.decoder.codingPath.removeLast() }
591 |
592 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int8.self) else {
593 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
594 | }
595 |
596 | self.currentIndex += 1
597 | return decoded
598 | }
599 |
600 | public mutating func decode(_ type: Int16.Type) throws -> Int16 {
601 | guard !self.isAtEnd else {
602 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
603 | }
604 |
605 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
606 | defer { self.decoder.codingPath.removeLast() }
607 |
608 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int16.self) else {
609 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
610 | }
611 |
612 | self.currentIndex += 1
613 | return decoded
614 | }
615 |
616 | public mutating func decode(_ type: Int32.Type) throws -> Int32 {
617 | guard !self.isAtEnd else {
618 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
619 | }
620 |
621 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
622 | defer { self.decoder.codingPath.removeLast() }
623 |
624 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int32.self) else {
625 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
626 | }
627 |
628 | self.currentIndex += 1
629 | return decoded
630 | }
631 |
632 | public mutating func decode(_ type: Int64.Type) throws -> Int64 {
633 | guard !self.isAtEnd else {
634 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
635 | }
636 |
637 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
638 | defer { self.decoder.codingPath.removeLast() }
639 |
640 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int64.self) else {
641 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
642 | }
643 |
644 | self.currentIndex += 1
645 | return decoded
646 | }
647 |
648 | public mutating func decode(_ type: UInt.Type) throws -> UInt {
649 | guard !self.isAtEnd else {
650 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
651 | }
652 |
653 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
654 | defer { self.decoder.codingPath.removeLast() }
655 |
656 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt.self) else {
657 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
658 | }
659 |
660 | self.currentIndex += 1
661 | return decoded
662 | }
663 |
664 | public mutating func decode(_ type: UInt8.Type) throws -> UInt8 {
665 | guard !self.isAtEnd else {
666 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
667 | }
668 |
669 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
670 | defer { self.decoder.codingPath.removeLast() }
671 |
672 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt8.self) else {
673 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
674 | }
675 |
676 | self.currentIndex += 1
677 | return decoded
678 | }
679 |
680 | public mutating func decode(_ type: UInt16.Type) throws -> UInt16 {
681 | guard !self.isAtEnd else {
682 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
683 | }
684 |
685 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
686 | defer { self.decoder.codingPath.removeLast() }
687 |
688 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt16.self) else {
689 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
690 | }
691 |
692 | self.currentIndex += 1
693 | return decoded
694 | }
695 |
696 | public mutating func decode(_ type: UInt32.Type) throws -> UInt32 {
697 | guard !self.isAtEnd else {
698 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
699 | }
700 |
701 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
702 | defer { self.decoder.codingPath.removeLast() }
703 |
704 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt32.self) else {
705 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
706 | }
707 |
708 | self.currentIndex += 1
709 | return decoded
710 | }
711 |
712 | public mutating func decode(_ type: UInt64.Type) throws -> UInt64 {
713 | guard !self.isAtEnd else {
714 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
715 | }
716 |
717 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
718 | defer { self.decoder.codingPath.removeLast() }
719 |
720 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt64.self) else {
721 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
722 | }
723 |
724 | self.currentIndex += 1
725 | return decoded
726 | }
727 |
728 | public mutating func decode(_ type: Float.Type) throws -> Float {
729 | guard !self.isAtEnd else {
730 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
731 | }
732 |
733 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
734 | defer { self.decoder.codingPath.removeLast() }
735 |
736 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Float.self) else {
737 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
738 | }
739 |
740 | self.currentIndex += 1
741 | return decoded
742 | }
743 |
744 | public mutating func decode(_ type: Double.Type) throws -> Double {
745 | guard !self.isAtEnd else {
746 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
747 | }
748 |
749 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
750 | defer { self.decoder.codingPath.removeLast() }
751 |
752 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Double.self) else {
753 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
754 | }
755 |
756 | self.currentIndex += 1
757 | return decoded
758 | }
759 |
760 | public mutating func decode(_ type: String.Type) throws -> String {
761 | guard !self.isAtEnd else {
762 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
763 | }
764 |
765 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
766 | defer { self.decoder.codingPath.removeLast() }
767 |
768 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: String.self) else {
769 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
770 | }
771 |
772 | self.currentIndex += 1
773 | return decoded
774 | }
775 |
776 | public mutating func decode(_ type: T.Type) throws -> T {
777 | guard !self.isAtEnd else {
778 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
779 | }
780 |
781 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
782 | defer { self.decoder.codingPath.removeLast() }
783 |
784 | guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: type) else {
785 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [DictionaryCodingKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
786 | }
787 |
788 | self.currentIndex += 1
789 | return decoded
790 | }
791 |
792 | public mutating func nestedContainer(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer {
793 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
794 | defer { self.decoder.codingPath.removeLast() }
795 |
796 | guard !self.isAtEnd else {
797 | throw DecodingError.valueNotFound(KeyedDecodingContainer.self,
798 | DecodingError.Context(codingPath: self.codingPath,
799 | debugDescription: "Cannot get nested keyed container -- unkeyed container is at end."))
800 | }
801 |
802 | let value = self.container[self.currentIndex]
803 | guard !(value is NSNull) else {
804 | throw DecodingError.valueNotFound(KeyedDecodingContainer.self,
805 | DecodingError.Context(codingPath: self.codingPath,
806 | debugDescription: "Cannot get keyed decoding container -- found null value instead."))
807 | }
808 |
809 | guard let dictionary = value as? [String : Any] else {
810 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: value)
811 | }
812 |
813 | self.currentIndex += 1
814 | let container = DictionaryCodingKeyedDecodingContainer(referencing: self.decoder, wrapping: dictionary)
815 | return KeyedDecodingContainer(container)
816 | }
817 |
818 | public mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
819 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
820 | defer { self.decoder.codingPath.removeLast() }
821 |
822 | guard !self.isAtEnd else {
823 | throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self,
824 | DecodingError.Context(codingPath: self.codingPath,
825 | debugDescription: "Cannot get nested keyed container -- unkeyed container is at end."))
826 | }
827 |
828 | let value = self.container[self.currentIndex]
829 | guard !(value is NSNull) else {
830 | throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self,
831 | DecodingError.Context(codingPath: self.codingPath,
832 | debugDescription: "Cannot get keyed decoding container -- found null value instead."))
833 | }
834 |
835 | guard let array = value as? [Any] else {
836 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: value)
837 | }
838 |
839 | self.currentIndex += 1
840 | return _DictionaryUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array)
841 | }
842 |
843 | public mutating func superDecoder() throws -> Decoder {
844 | self.decoder.codingPath.append(DictionaryCodingKey(index: self.currentIndex))
845 | defer { self.decoder.codingPath.removeLast() }
846 |
847 | guard !self.isAtEnd else {
848 | throw DecodingError.valueNotFound(Decoder.self,
849 | DecodingError.Context(codingPath: self.codingPath,
850 | debugDescription: "Cannot get superDecoder() -- unkeyed container is at end."))
851 | }
852 |
853 | let value = self.container[self.currentIndex]
854 | self.currentIndex += 1
855 | return _DictionaryDecoder(referencing: value, at: self.decoder.codingPath, options: self.decoder.options)
856 | }
857 | }
858 |
859 | extension _DictionaryDecoder : SingleValueDecodingContainer {
860 | // MARK: SingleValueDecodingContainer Methods
861 | private func expectNonNull(_ type: T.Type) throws {
862 | guard !self.decodeNil() else {
863 | throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.codingPath, debugDescription: "Expected \(type) but found null value instead."))
864 | }
865 | }
866 |
867 | public func decodeNil() -> Bool {
868 | return self.storage.topContainer is NSNull
869 | }
870 |
871 | public func decode(_ type: Bool.Type) throws -> Bool {
872 | try expectNonNull(Bool.self)
873 | return try self.unbox(self.storage.topContainer, as: Bool.self)!
874 | }
875 |
876 | public func decode(_ type: Int.Type) throws -> Int {
877 | try expectNonNull(Int.self)
878 | return try self.unbox(self.storage.topContainer, as: Int.self)!
879 | }
880 |
881 | public func decode(_ type: Int8.Type) throws -> Int8 {
882 | try expectNonNull(Int8.self)
883 | return try self.unbox(self.storage.topContainer, as: Int8.self)!
884 | }
885 |
886 | public func decode(_ type: Int16.Type) throws -> Int16 {
887 | try expectNonNull(Int16.self)
888 | return try self.unbox(self.storage.topContainer, as: Int16.self)!
889 | }
890 |
891 | public func decode(_ type: Int32.Type) throws -> Int32 {
892 | try expectNonNull(Int32.self)
893 | return try self.unbox(self.storage.topContainer, as: Int32.self)!
894 | }
895 |
896 | public func decode(_ type: Int64.Type) throws -> Int64 {
897 | try expectNonNull(Int64.self)
898 | return try self.unbox(self.storage.topContainer, as: Int64.self)!
899 | }
900 |
901 | public func decode(_ type: UInt.Type) throws -> UInt {
902 | try expectNonNull(UInt.self)
903 | return try self.unbox(self.storage.topContainer, as: UInt.self)!
904 | }
905 |
906 | public func decode(_ type: UInt8.Type) throws -> UInt8 {
907 | try expectNonNull(UInt8.self)
908 | return try self.unbox(self.storage.topContainer, as: UInt8.self)!
909 | }
910 |
911 | public func decode(_ type: UInt16.Type) throws -> UInt16 {
912 | try expectNonNull(UInt16.self)
913 | return try self.unbox(self.storage.topContainer, as: UInt16.self)!
914 | }
915 |
916 | public func decode(_ type: UInt32.Type) throws -> UInt32 {
917 | try expectNonNull(UInt32.self)
918 | return try self.unbox(self.storage.topContainer, as: UInt32.self)!
919 | }
920 |
921 | public func decode(_ type: UInt64.Type) throws -> UInt64 {
922 | try expectNonNull(UInt64.self)
923 | return try self.unbox(self.storage.topContainer, as: UInt64.self)!
924 | }
925 |
926 | public func decode(_ type: Float.Type) throws -> Float {
927 | try expectNonNull(Float.self)
928 | return try self.unbox(self.storage.topContainer, as: Float.self)!
929 | }
930 |
931 | public func decode(_ type: Double.Type) throws -> Double {
932 | try expectNonNull(Double.self)
933 | return try self.unbox(self.storage.topContainer, as: Double.self)!
934 | }
935 |
936 | public func decode(_ type: String.Type) throws -> String {
937 | try expectNonNull(String.self)
938 | return try self.unbox(self.storage.topContainer, as: String.self)!
939 | }
940 |
941 | public func decode(_ type: T.Type) throws -> T {
942 | try expectNonNull(type)
943 | return try self.unbox(self.storage.topContainer, as: type)!
944 | }
945 | }
946 |
947 | // MARK: - Concrete Value Representations
948 | extension _DictionaryDecoder {
949 | /// Returns the given value unboxed from a container.
950 | fileprivate func unbox(_ value: Any, as type: Bool.Type) throws -> Bool? {
951 | guard !(value is NSNull) else { return nil }
952 |
953 | if let number = value as? NSNumber {
954 |
955 | if number === kCFBooleanTrue as NSNumber {
956 | return true
957 | } else if number === kCFBooleanFalse as NSNumber {
958 | return false
959 | } else {
960 | return number != 0 // TODO: Add a flag to disallow non-boolean numbers?
961 | }
962 |
963 | /* FIXME: If swift-corelibs-foundation doesn't change to use NSNumber, this code path will need to be included and tested:
964 | } else if let bool = value as? Bool {
965 | return bool
966 | */
967 |
968 | }
969 |
970 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
971 | }
972 |
973 | fileprivate func unbox(_ value: Any, as type: Int.Type) throws -> Int? {
974 | guard !(value is NSNull) else { return nil }
975 |
976 | guard let number = value as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
977 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
978 | }
979 |
980 | let int = number.intValue
981 | guard NSNumber(value: int) == number else {
982 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed Dictionary number <\(number)> does not fit in \(type)."))
983 | }
984 |
985 | return int
986 | }
987 |
988 | fileprivate func unbox(_ value: Any, as type: Int8.Type) throws -> Int8? {
989 | guard !(value is NSNull) else { return nil }
990 |
991 | guard let number = value as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
992 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
993 | }
994 |
995 | let int8 = number.int8Value
996 | guard NSNumber(value: int8) == number else {
997 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed Dictionary number <\(number)> does not fit in \(type)."))
998 | }
999 |
1000 | return int8
1001 | }
1002 |
1003 | fileprivate func unbox(_ value: Any, as type: Int16.Type) throws -> Int16? {
1004 | guard !(value is NSNull) else { return nil }
1005 |
1006 | guard let number = value as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
1007 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1008 | }
1009 |
1010 | let int16 = number.int16Value
1011 | guard NSNumber(value: int16) == number else {
1012 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed Dictionary number <\(number)> does not fit in \(type)."))
1013 | }
1014 |
1015 | return int16
1016 | }
1017 |
1018 | fileprivate func unbox(_ value: Any, as type: Int32.Type) throws -> Int32? {
1019 | guard !(value is NSNull) else { return nil }
1020 |
1021 | guard let number = value as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
1022 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1023 | }
1024 |
1025 | let int32 = number.int32Value
1026 | guard NSNumber(value: int32) == number else {
1027 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed Dictionary number <\(number)> does not fit in \(type)."))
1028 | }
1029 |
1030 | return int32
1031 | }
1032 |
1033 | fileprivate func unbox(_ value: Any, as type: Int64.Type) throws -> Int64? {
1034 | guard !(value is NSNull) else { return nil }
1035 |
1036 | guard let number = value as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
1037 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1038 | }
1039 |
1040 | let int64 = number.int64Value
1041 | guard NSNumber(value: int64) == number else {
1042 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed Dictionary number <\(number)> does not fit in \(type)."))
1043 | }
1044 |
1045 | return int64
1046 | }
1047 |
1048 | fileprivate func unbox(_ value: Any, as type: UInt.Type) throws -> UInt? {
1049 | guard !(value is NSNull) else { return nil }
1050 |
1051 | guard let number = value as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
1052 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1053 | }
1054 |
1055 | let uint = number.uintValue
1056 | guard NSNumber(value: uint) == number else {
1057 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed Dictionary number <\(number)> does not fit in \(type)."))
1058 | }
1059 |
1060 | return uint
1061 | }
1062 |
1063 | fileprivate func unbox(_ value: Any, as type: UInt8.Type) throws -> UInt8? {
1064 | guard !(value is NSNull) else { return nil }
1065 |
1066 | guard let number = value as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
1067 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1068 | }
1069 |
1070 | let uint8 = number.uint8Value
1071 | guard NSNumber(value: uint8) == number else {
1072 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed Dictionary number <\(number)> does not fit in \(type)."))
1073 | }
1074 |
1075 | return uint8
1076 | }
1077 |
1078 | fileprivate func unbox(_ value: Any, as type: UInt16.Type) throws -> UInt16? {
1079 | guard !(value is NSNull) else { return nil }
1080 |
1081 | guard let number = value as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
1082 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1083 | }
1084 |
1085 | let uint16 = number.uint16Value
1086 | guard NSNumber(value: uint16) == number else {
1087 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed Dictionary number <\(number)> does not fit in \(type)."))
1088 | }
1089 |
1090 | return uint16
1091 | }
1092 |
1093 | fileprivate func unbox(_ value: Any, as type: UInt32.Type) throws -> UInt32? {
1094 | guard !(value is NSNull) else { return nil }
1095 |
1096 | guard let number = value as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
1097 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1098 | }
1099 |
1100 | let uint32 = number.uint32Value
1101 | guard NSNumber(value: uint32) == number else {
1102 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed Dictionary number <\(number)> does not fit in \(type)."))
1103 | }
1104 |
1105 | return uint32
1106 | }
1107 |
1108 | fileprivate func unbox(_ value: Any, as type: UInt64.Type) throws -> UInt64? {
1109 | guard !(value is NSNull) else { return nil }
1110 |
1111 | guard let number = value as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
1112 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1113 | }
1114 |
1115 | let uint64 = number.uint64Value
1116 | guard NSNumber(value: uint64) == number else {
1117 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed Dictionary number <\(number)> does not fit in \(type)."))
1118 | }
1119 |
1120 | return uint64
1121 | }
1122 |
1123 | fileprivate func unbox(_ value: Any, as type: Float.Type) throws -> Float? {
1124 | guard !(value is NSNull) else { return nil }
1125 |
1126 | if let number = value as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse {
1127 | // We are willing to return a Float by losing precision:
1128 | // * If the original value was integral,
1129 | // * and the integral value was > Float.greatestFiniteMagnitude, we will fail
1130 | // * and the integral value was <= Float.greatestFiniteMagnitude, we are willing to lose precision past 2^24
1131 | // * If it was a Float, you will get back the precise value
1132 | // * If it was a Double or Decimal, you will get back the nearest approximation if it will fit
1133 | let double = number.doubleValue
1134 | guard abs(double) <= Double(Float.greatestFiniteMagnitude) else {
1135 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed Dictionary number \(number) does not fit in \(type)."))
1136 | }
1137 |
1138 | return Float(double)
1139 |
1140 | /* FIXME: If swift-corelibs-foundation doesn't change to use NSNumber, this code path will need to be included and tested:
1141 | } else if let double = value as? Double {
1142 | if abs(double) <= Double(Float.max) {
1143 | return Float(double)
1144 | }
1145 | overflow = true
1146 | } else if let int = value as? Int {
1147 | if let float = Float(exactly: int) {
1148 | return float
1149 | }
1150 | overflow = true
1151 | */
1152 |
1153 | } else if let string = value as? String,
1154 | case .convertFromString(let posInfString, let negInfString, let nanString) = self.options.nonConformingFloatDecodingStrategy {
1155 | if string == posInfString {
1156 | return Float.infinity
1157 | } else if string == negInfString {
1158 | return -Float.infinity
1159 | } else if string == nanString {
1160 | return Float.nan
1161 | }
1162 | }
1163 |
1164 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1165 | }
1166 |
1167 | fileprivate func unbox(_ value: Any, as type: Double.Type) throws -> Double? {
1168 | guard !(value is NSNull) else { return nil }
1169 |
1170 | if let number = value as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse {
1171 | // We are always willing to return the number as a Double:
1172 | // * If the original value was integral, it is guaranteed to fit in a Double; we are willing to lose precision past 2^53 if you encoded a UInt64 but requested a Double
1173 | // * If it was a Float or Double, you will get back the precise value
1174 | // * If it was Decimal, you will get back the nearest approximation
1175 | return number.doubleValue
1176 |
1177 | /* FIXME: If swift-corelibs-foundation doesn't change to use NSNumber, this code path will need to be included and tested:
1178 | } else if let double = value as? Double {
1179 | return double
1180 | } else if let int = value as? Int {
1181 | if let double = Double(exactly: int) {
1182 | return double
1183 | }
1184 | overflow = true
1185 | */
1186 |
1187 | } else if let string = value as? String,
1188 | case .convertFromString(let posInfString, let negInfString, let nanString) = self.options.nonConformingFloatDecodingStrategy {
1189 | if string == posInfString {
1190 | return Double.infinity
1191 | } else if string == negInfString {
1192 | return -Double.infinity
1193 | } else if string == nanString {
1194 | return Double.nan
1195 | }
1196 | }
1197 |
1198 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1199 | }
1200 |
1201 | fileprivate func unbox(_ value: Any, as type: String.Type) throws -> String? {
1202 | guard !(value is NSNull) else { return nil }
1203 |
1204 | if let url = value as? URL {
1205 | return url.absoluteString
1206 | }
1207 |
1208 | if let uuid = value as? UUID {
1209 | return uuid.uuidString
1210 | }
1211 |
1212 | guard let string = value as? String else {
1213 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1214 | }
1215 |
1216 | return string
1217 | }
1218 |
1219 | fileprivate func unbox(_ value: Any, as type: UUID.Type) throws -> UUID? {
1220 | guard !(value is NSNull) else { return nil }
1221 |
1222 | if let uuid = value as? UUID {
1223 | return uuid
1224 | }
1225 |
1226 | if let string = value as? String {
1227 | return UUID(uuidString: string)
1228 | }
1229 |
1230 | let cfType = CFGetTypeID(value as CFTypeRef) // NB this could be dangerous - we're assuming that it's ok to call CFGetTypeID with the value, which may not be true
1231 | if cfType == CFUUIDGetTypeID() {
1232 | let cfValue = value as! CFUUID
1233 | let string = CFUUIDCreateString(kCFAllocatorDefault, cfValue) as String
1234 | return UUID(uuidString: string)
1235 | }
1236 |
1237 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1238 | }
1239 |
1240 | fileprivate func unbox(_ value: Any, as type: Date.Type) throws -> Date? {
1241 | guard !(value is NSNull) else { return nil }
1242 |
1243 | switch self.options.dateDecodingStrategy {
1244 | case .deferredToDate:
1245 | self.storage.push(container: value)
1246 | defer { self.storage.popContainer() }
1247 | return try Date(from: self)
1248 |
1249 | case .secondsSince1970:
1250 | let double = try self.unbox(value, as: Double.self)!
1251 | return Date(timeIntervalSince1970: double)
1252 |
1253 | case .millisecondsSince1970:
1254 | let double = try self.unbox(value, as: Double.self)!
1255 | return Date(timeIntervalSince1970: double / 1000.0)
1256 |
1257 | case .iso8601:
1258 | if #available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
1259 | let string = try self.unbox(value, as: String.self)!
1260 | guard let date = _iso8601Formatter.date(from: string) else {
1261 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Expected date string to be ISO8601-formatted."))
1262 | }
1263 |
1264 | return date
1265 | } else {
1266 | fatalError("ISO8601DateFormatter is unavailable on this platform.")
1267 | }
1268 |
1269 | case .formatted(let formatter):
1270 | let string = try self.unbox(value, as: String.self)!
1271 | guard let date = formatter.date(from: string) else {
1272 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Date string does not match format expected by formatter."))
1273 | }
1274 |
1275 | return date
1276 |
1277 | case .custom(let closure):
1278 | self.storage.push(container: value)
1279 | defer { self.storage.popContainer() }
1280 | return try closure(self)
1281 | }
1282 | }
1283 |
1284 | fileprivate func unbox(_ value: Any, as type: Data.Type) throws -> Data? {
1285 | guard !(value is NSNull) else { return nil }
1286 |
1287 | switch self.options.dataDecodingStrategy {
1288 | case .deferredToData:
1289 | self.storage.push(container: value)
1290 | defer { self.storage.popContainer() }
1291 | return try Data(from: self)
1292 |
1293 | case .base64:
1294 | guard let string = value as? String else {
1295 | throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1296 | }
1297 |
1298 | guard let data = Data(base64Encoded: string) else {
1299 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Encountered Data is not valid Base64."))
1300 | }
1301 |
1302 | return data
1303 |
1304 | case .custom(let closure):
1305 | self.storage.push(container: value)
1306 | defer { self.storage.popContainer() }
1307 | return try closure(self)
1308 | }
1309 | }
1310 |
1311 | fileprivate func unbox(_ value: Any, as type: Decimal.Type) throws -> Decimal? {
1312 | guard !(value is NSNull) else { return nil }
1313 |
1314 | // Attempt to bridge from NSDecimalNumber.
1315 | if let decimal = value as? Decimal {
1316 | return decimal
1317 | } else {
1318 | let doubleValue = try self.unbox(value, as: Double.self)!
1319 | return Decimal(doubleValue)
1320 | }
1321 | }
1322 |
1323 | fileprivate func unbox(_ value: Any, as type: T.Type) throws -> T? {
1324 | if type == Date.self || type == NSDate.self {
1325 | return try self.unbox(value, as: Date.self) as? T
1326 | } else if type == Data.self || type == NSData.self {
1327 | return try self.unbox(value, as: Data.self) as? T
1328 | } else if type == UUID.self || type == CFUUID.self {
1329 | return try self.unbox(value, as: UUID.self) as? T
1330 | } else if type == URL.self || type == NSURL.self {
1331 | guard let urlString = try self.unbox(value, as: String.self) else {
1332 | return nil
1333 | }
1334 |
1335 | guard let url = URL(string: urlString) else {
1336 | throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath,
1337 | debugDescription: "Invalid URL string."))
1338 | }
1339 |
1340 | return (url as! T)
1341 | } else if type == Decimal.self || type == NSDecimalNumber.self {
1342 | return try self.unbox(value, as: Decimal.self) as? T
1343 | } else {
1344 | self.storage.push(container: value)
1345 | defer { self.storage.popContainer() }
1346 | return try type.init(from: self)
1347 | }
1348 | }
1349 | }
1350 |
1351 | //===----------------------------------------------------------------------===//
1352 | // Shared ISO8601 Date Formatter
1353 | //===----------------------------------------------------------------------===//
1354 | // NOTE: This value is implicitly lazy and _must_ be lazy. We're compiled against the latest SDK (w/ ISO8601DateFormatter), but linked against whichever Foundation the user has. ISO8601DateFormatter might not exist, so we better not hit this code path on an older OS.
1355 | @available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
1356 | internal var _iso8601Formatter: ISO8601DateFormatter = {
1357 | let formatter = ISO8601DateFormatter()
1358 | formatter.formatOptions = .withInternetDateTime
1359 | return formatter
1360 | }()
1361 |
1362 |
1363 | #if canImport(Combine)
1364 | import Combine
1365 |
1366 | extension DictionaryDecoder: TopLevelDecoder {
1367 | public typealias Input = [String: Any]
1368 | }
1369 | #endif
1370 |
--------------------------------------------------------------------------------
/Sources/DictionaryCoding/DictionaryEncoder.swift:
--------------------------------------------------------------------------------
1 | //===----------------------------------------------------------------------===//
2 | //
3 | // This source file is largely a copy of code from Swift.org open source project's
4 | // files JSONEncoder.swift and Codeable.swift.
5 | //
6 | // Unfortunately those files do not expose the internal _JSONEncoder and
7 | // _JSONDecoder classes, which are in fact dictionary encoder/decoders and
8 | // precisely what we want...
9 | //
10 | // The original code is copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
11 | // Licensed under Apache License v2.0 with Runtime Library Exception
12 | //
13 | // See https://swift.org/LICENSE.txt for license information
14 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
15 | //
16 | // Modifications and additional code here is copyright (c) 2018 Sam Deane, and
17 | // is licensed under the same terms.
18 | //
19 | //===----------------------------------------------------------------------===//
20 |
21 | import Foundation
22 |
23 |
24 | //===----------------------------------------------------------------------===//
25 | // Dictionary Encoder
26 | //===----------------------------------------------------------------------===//
27 |
28 | /// `DictionaryEncoder` facilitates the encoding of `Encodable` values into Dictionary.
29 | open class DictionaryEncoder {
30 | // MARK: Options
31 |
32 | /// The strategy to use for encoding `Date` values.
33 | public enum DateEncodingStrategy {
34 | /// Defer to `Date` for choosing an encoding. This is the default strategy.
35 | case deferredToDate
36 |
37 | /// Encode the `Date` as a UNIX timestamp (as a Dictionary number).
38 | case secondsSince1970
39 |
40 | /// Encode the `Date` as UNIX millisecond timestamp (as a Dictionary number).
41 | case millisecondsSince1970
42 |
43 | /// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
44 | @available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
45 | case iso8601
46 |
47 | /// Encode the `Date` as a string formatted by the given formatter.
48 | case formatted(DateFormatter)
49 |
50 | /// Encode the `Date` as a custom value encoded by the given closure.
51 | ///
52 | /// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
53 | case custom((Date, Encoder) throws -> Void)
54 | }
55 |
56 | /// The strategy to use for encoding `Data` values.
57 | public enum DataEncodingStrategy {
58 | /// Defer to `Data` for choosing an encoding.
59 | case deferredToData
60 |
61 | /// Encoded the `Data` as a Base64-encoded string. This is the default strategy.
62 | case base64
63 |
64 | /// Encode the `Data` as a custom value encoded by the given closure.
65 | ///
66 | /// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
67 | case custom((Data, Encoder) throws -> Void)
68 | }
69 |
70 | /// The strategy to use for non-Dictionary-conforming floating-point values (IEEE 754 infinity and NaN).
71 | public enum NonConformingFloatEncodingStrategy {
72 | /// Throw upon encountering non-conforming values. This is the default strategy.
73 | case `throw`
74 |
75 | /// Encode the values using the given representation strings.
76 | case convertToString(positiveInfinity: String, negativeInfinity: String, nan: String)
77 | }
78 |
79 | /// The strategy to use for automatically changing the value of keys before encoding.
80 | public enum KeyEncodingStrategy {
81 | /// Use the keys specified by each type. This is the default strategy.
82 | case useDefaultKeys
83 |
84 | /// Convert from "camelCaseKeys" to "snake_case_keys" before writing a key to Dictionary payload.
85 | ///
86 | /// Capital characters are determined by testing membership in `CharacterSet.uppercaseLetters` and `CharacterSet.lowercaseLetters` (Unicode General Categories Lu and Lt).
87 | /// The conversion to lower case uses `Locale.system`, also known as the ICU "root" locale. This means the result is consistent regardless of the current user's locale and language preferences.
88 | ///
89 | /// Converting from camel case to snake case:
90 | /// 1. Splits words at the boundary of lower-case to upper-case
91 | /// 2. Inserts `_` between words
92 | /// 3. Lowercases the entire string
93 | /// 4. Preserves starting and ending `_`.
94 | ///
95 | /// For example, `oneTwoThree` becomes `one_two_three`. `_oneTwoThree_` becomes `_one_two_three_`.
96 | ///
97 | /// - Note: Using a key encoding strategy has a nominal performance cost, as each string key has to be converted.
98 | case convertToSnakeCase
99 |
100 | /// Provide a custom conversion to the key in the encoded Dictionary from the keys specified by the encoded types.
101 | /// The full path to the current encoding position is provided for context (in case you need to locate this key within the payload). The returned key is used in place of the last component in the coding path before encoding.
102 | /// If the result of the conversion is a duplicate key, then only one value will be present in the result.
103 | case custom((_ codingPath: [CodingKey]) -> CodingKey)
104 |
105 | internal static func _convertToSnakeCase(_ stringKey: String) -> String {
106 | guard stringKey.count > 0 else { return stringKey }
107 |
108 | var words : [Range] = []
109 | // The general idea of this algorithm is to split words on transition from lower to upper case, then on transition of >1 upper case characters to lowercase
110 | //
111 | // myProperty -> my_property
112 | // myURLProperty -> my_url_property
113 | //
114 | // We assume, per Swift naming conventions, that the first character of the key is lowercase.
115 | var wordStart = stringKey.startIndex
116 | var searchRange = stringKey.index(after: wordStart)..1 capital letters. Turn those into a word, stopping at the capital before the lower case character.
139 | let beforeLowerIndex = stringKey.index(before: lowerCaseRange.lowerBound)
140 | words.append(upperCaseRange.lowerBound..(_ value: T) throws -> NSDictionary {
200 | let encoder = _DictionaryEncoder(options: self.options)
201 |
202 | guard let topLevel = try encoder.box_(value) else {
203 | throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) did not encode any values."))
204 | }
205 |
206 | if topLevel is NSNull {
207 | throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) encoded as null Dictionary fragment."))
208 | } else if topLevel is NSNumber {
209 | throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) encoded as number Dictionary fragment."))
210 | } else if topLevel is NSString {
211 | throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) encoded as string Dictionary fragment."))
212 | }
213 |
214 | return topLevel as! NSDictionary
215 | }
216 |
217 | // MARK: - Encoding Values
218 | /// Encodes the given top-level value and returns its Dictionary representation.
219 | ///
220 | /// - parameter value: The value to encode.
221 | /// - returns: A new `Data` value containing the encoded Dictionary data.
222 | /// - throws: `EncodingError.invalidValue` if a non-conforming floating-point value is encountered during encoding, and the encoding strategy is `.throw`.
223 | /// - throws: An error if any value throws an error during encoding.
224 | open func encode(_ value: T) throws -> [String:Any] {
225 | let encoder = _DictionaryEncoder(options: self.options)
226 |
227 | guard let topLevel = try encoder.box_(value) else {
228 | throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) did not encode any values."))
229 | }
230 |
231 | if topLevel is NSNull {
232 | throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) encoded as null Dictionary fragment."))
233 | } else if topLevel is NSNumber {
234 | throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) encoded as number Dictionary fragment."))
235 | } else if topLevel is NSString {
236 | throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) encoded as string Dictionary fragment."))
237 | }
238 |
239 | return topLevel as! [String:Any]
240 | }
241 |
242 | }
243 |
244 | // MARK: - _DictionaryEncoder
245 | fileprivate class _DictionaryEncoder : Encoder {
246 | // MARK: Properties
247 | /// The encoder's storage.
248 | fileprivate var storage: _DictionaryEncodingStorage
249 |
250 | /// Options set on the top-level encoder.
251 | fileprivate let options: DictionaryEncoder._Options
252 |
253 | /// The path to the current point in encoding.
254 | public var codingPath: [CodingKey]
255 |
256 | /// Contextual user-provided information for use during encoding.
257 | public var userInfo: [CodingUserInfoKey : Any] {
258 | return self.options.userInfo
259 | }
260 |
261 | // MARK: - Initialization
262 | /// Initializes `self` with the given top-level encoder options.
263 | fileprivate init(options: DictionaryEncoder._Options, codingPath: [CodingKey] = []) {
264 | self.options = options
265 | self.storage = _DictionaryEncodingStorage()
266 | self.codingPath = codingPath
267 | }
268 |
269 | /// Returns whether a new element can be encoded at this coding path.
270 | ///
271 | /// `true` if an element has not yet been encoded at this coding path; `false` otherwise.
272 | fileprivate var canEncodeNewValue: Bool {
273 | // Every time a new value gets encoded, the key it's encoded for is pushed onto the coding path (even if it's a nil key from an unkeyed container).
274 | // At the same time, every time a container is requested, a new value gets pushed onto the storage stack.
275 | // If there are more values on the storage stack than on the coding path, it means the value is requesting more than one container, which violates the precondition.
276 | //
277 | // This means that anytime something that can request a new container goes onto the stack, we MUST push a key onto the coding path.
278 | // Things which will not request containers do not need to have the coding path extended for them (but it doesn't matter if it is, because they will not reach here).
279 | return self.storage.count == self.codingPath.count
280 | }
281 |
282 | // MARK: - Encoder Methods
283 | public func container(keyedBy: Key.Type) -> KeyedEncodingContainer {
284 | // If an existing keyed container was already requested, return that one.
285 | let topContainer: NSMutableDictionary
286 | if self.canEncodeNewValue {
287 | // We haven't yet pushed a container at this level; do so here.
288 | topContainer = self.storage.pushKeyedContainer()
289 | } else {
290 | guard let container = self.storage.containers.last as? NSMutableDictionary else {
291 | preconditionFailure("Attempt to push new keyed encoding container when already previously encoded at this path.")
292 | }
293 |
294 | topContainer = container
295 | }
296 |
297 | let container = DictionaryCodingKeyedEncodingContainer(referencing: self, codingPath: self.codingPath, wrapping: topContainer)
298 | return KeyedEncodingContainer(container)
299 | }
300 |
301 | public func unkeyedContainer() -> UnkeyedEncodingContainer {
302 | // If an existing unkeyed container was already requested, return that one.
303 | let topContainer: NSMutableArray
304 | if self.canEncodeNewValue {
305 | // We haven't yet pushed a container at this level; do so here.
306 | topContainer = self.storage.pushUnkeyedContainer()
307 | } else {
308 | guard let container = self.storage.containers.last as? NSMutableArray else {
309 | preconditionFailure("Attempt to push new unkeyed encoding container when already previously encoded at this path.")
310 | }
311 |
312 | topContainer = container
313 | }
314 |
315 | return _DictionaryUnkeyedEncodingContainer(referencing: self, codingPath: self.codingPath, wrapping: topContainer)
316 | }
317 |
318 | public func singleValueContainer() -> SingleValueEncodingContainer {
319 | return self
320 | }
321 | }
322 |
323 | // MARK: - Encoding Storage and Containers
324 | fileprivate struct _DictionaryEncodingStorage {
325 | // MARK: Properties
326 | /// The container stack.
327 | /// Elements may be any one of the Dictionary types (NSNull, NSNumber, NSString, NSArray, NSDictionary).
328 | private(set) fileprivate var containers: [NSObject] = []
329 |
330 | // MARK: - Initialization
331 | /// Initializes `self` with no containers.
332 | fileprivate init() {}
333 |
334 | // MARK: - Modifying the Stack
335 | fileprivate var count: Int {
336 | return self.containers.count
337 | }
338 |
339 | fileprivate mutating func pushKeyedContainer() -> NSMutableDictionary {
340 | let dictionary = NSMutableDictionary()
341 | self.containers.append(dictionary)
342 | return dictionary
343 | }
344 |
345 | fileprivate mutating func pushUnkeyedContainer() -> NSMutableArray {
346 | let array = NSMutableArray()
347 | self.containers.append(array)
348 | return array
349 | }
350 |
351 | fileprivate mutating func push(container: NSObject) {
352 | self.containers.append(container)
353 | }
354 |
355 | fileprivate mutating func popContainer() -> NSObject {
356 | precondition(self.containers.count > 0, "Empty container stack.")
357 | return self.containers.popLast()!
358 | }
359 | }
360 |
361 | // MARK: - Encoding Containers
362 | fileprivate struct DictionaryCodingKeyedEncodingContainer : KeyedEncodingContainerProtocol {
363 | typealias Key = K
364 |
365 | // MARK: Properties
366 | /// A reference to the encoder we're writing to.
367 | private let encoder: _DictionaryEncoder
368 |
369 | /// A reference to the container we're writing to.
370 | private let container: NSMutableDictionary
371 |
372 | /// The path of coding keys taken to get to this point in encoding.
373 | private(set) public var codingPath: [CodingKey]
374 |
375 | // MARK: - Initialization
376 | /// Initializes `self` with the given references.
377 | fileprivate init(referencing encoder: _DictionaryEncoder, codingPath: [CodingKey], wrapping container: NSMutableDictionary) {
378 | self.encoder = encoder
379 | self.codingPath = codingPath
380 | self.container = container
381 | }
382 |
383 | // MARK: - Coding Path Operations
384 | private func _converted(_ key: CodingKey) -> CodingKey {
385 | switch encoder.options.keyEncodingStrategy {
386 | case .useDefaultKeys:
387 | return key
388 | case .convertToSnakeCase:
389 | let newKeyString = DictionaryEncoder.KeyEncodingStrategy._convertToSnakeCase(key.stringValue)
390 | return DictionaryCodingKey(stringValue: newKeyString, intValue: key.intValue)
391 | case .custom(let converter):
392 | return converter(codingPath + [key])
393 | }
394 | }
395 |
396 | // MARK: - KeyedEncodingContainerProtocol Methods
397 | public mutating func encodeNil(forKey key: Key) throws {
398 | self.container[_converted(key).stringValue] = NSNull()
399 | }
400 | public mutating func encode(_ value: Bool, forKey key: Key) throws {
401 | self.container[_converted(key).stringValue] = self.encoder.box(value)
402 | }
403 | public mutating func encode(_ value: Int, forKey key: Key) throws {
404 | self.container[_converted(key).stringValue] = self.encoder.box(value)
405 | }
406 | public mutating func encode(_ value: Int8, forKey key: Key) throws {
407 | self.container[_converted(key).stringValue] = self.encoder.box(value)
408 | }
409 | public mutating func encode(_ value: Int16, forKey key: Key) throws {
410 | self.container[_converted(key).stringValue] = self.encoder.box(value)
411 | }
412 | public mutating func encode(_ value: Int32, forKey key: Key) throws {
413 | self.container[_converted(key).stringValue] = self.encoder.box(value)
414 | }
415 | public mutating func encode(_ value: Int64, forKey key: Key) throws {
416 | self.container[_converted(key).stringValue] = self.encoder.box(value)
417 | }
418 | public mutating func encode(_ value: UInt, forKey key: Key) throws {
419 | self.container[_converted(key).stringValue] = self.encoder.box(value)
420 | }
421 | public mutating func encode(_ value: UInt8, forKey key: Key) throws {
422 | self.container[_converted(key).stringValue] = self.encoder.box(value)
423 | }
424 | public mutating func encode(_ value: UInt16, forKey key: Key) throws {
425 | self.container[_converted(key).stringValue] = self.encoder.box(value)
426 | }
427 | public mutating func encode(_ value: UInt32, forKey key: Key) throws {
428 | self.container[_converted(key).stringValue] = self.encoder.box(value)
429 | }
430 | public mutating func encode(_ value: UInt64, forKey key: Key) throws {
431 | self.container[_converted(key).stringValue] = self.encoder.box(value)
432 | }
433 | public mutating func encode(_ value: String, forKey key: Key) throws {
434 | self.container[_converted(key).stringValue] = self.encoder.box(value)
435 | }
436 |
437 | public mutating func encode(_ value: Float, forKey key: Key) throws {
438 | // Since the float may be invalid and throw, the coding path needs to contain this key.
439 | self.encoder.codingPath.append(key)
440 | defer { self.encoder.codingPath.removeLast() }
441 | self.container[_converted(key).stringValue] = try self.encoder.box(value)
442 | }
443 |
444 | public mutating func encode(_ value: Double, forKey key: Key) throws {
445 | // Since the double may be invalid and throw, the coding path needs to contain this key.
446 | self.encoder.codingPath.append(key)
447 | defer { self.encoder.codingPath.removeLast() }
448 | self.container[_converted(key).stringValue] = try self.encoder.box(value)
449 | }
450 |
451 | public mutating func encode(_ value: T, forKey key: Key) throws {
452 | self.encoder.codingPath.append(key)
453 | defer { self.encoder.codingPath.removeLast() }
454 | self.container[_converted(key).stringValue] = try self.encoder.box(value)
455 | }
456 |
457 | public mutating func nestedContainer(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer {
458 | let dictionary = NSMutableDictionary()
459 | self.container[_converted(key).stringValue] = dictionary
460 |
461 | self.codingPath.append(key)
462 | defer { self.codingPath.removeLast() }
463 |
464 | let container = DictionaryCodingKeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: dictionary)
465 | return KeyedEncodingContainer(container)
466 | }
467 |
468 | public mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
469 | let array = NSMutableArray()
470 | self.container[_converted(key).stringValue] = array
471 |
472 | self.codingPath.append(key)
473 | defer { self.codingPath.removeLast() }
474 | return _DictionaryUnkeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: array)
475 | }
476 |
477 | public mutating func superEncoder() -> Encoder {
478 | return _DictionaryReferencingEncoder(referencing: self.encoder, key: DictionaryCodingKey.super, convertedKey: _converted(DictionaryCodingKey.super), wrapping: self.container)
479 | }
480 |
481 | public mutating func superEncoder(forKey key: Key) -> Encoder {
482 | return _DictionaryReferencingEncoder(referencing: self.encoder, key: key, convertedKey: _converted(key), wrapping: self.container)
483 | }
484 | }
485 |
486 | fileprivate struct _DictionaryUnkeyedEncodingContainer : UnkeyedEncodingContainer {
487 | // MARK: Properties
488 | /// A reference to the encoder we're writing to.
489 | private let encoder: _DictionaryEncoder
490 |
491 | /// A reference to the container we're writing to.
492 | private let container: NSMutableArray
493 |
494 | /// The path of coding keys taken to get to this point in encoding.
495 | private(set) public var codingPath: [CodingKey]
496 |
497 | /// The number of elements encoded into the container.
498 | public var count: Int {
499 | return self.container.count
500 | }
501 |
502 | // MARK: - Initialization
503 | /// Initializes `self` with the given references.
504 | fileprivate init(referencing encoder: _DictionaryEncoder, codingPath: [CodingKey], wrapping container: NSMutableArray) {
505 | self.encoder = encoder
506 | self.codingPath = codingPath
507 | self.container = container
508 | }
509 |
510 | // MARK: - UnkeyedEncodingContainer Methods
511 | public mutating func encodeNil() throws { self.container.add(NSNull()) }
512 | public mutating func encode(_ value: Bool) throws { self.container.add(self.encoder.box(value)) }
513 | public mutating func encode(_ value: Int) throws { self.container.add(self.encoder.box(value)) }
514 | public mutating func encode(_ value: Int8) throws { self.container.add(self.encoder.box(value)) }
515 | public mutating func encode(_ value: Int16) throws { self.container.add(self.encoder.box(value)) }
516 | public mutating func encode(_ value: Int32) throws { self.container.add(self.encoder.box(value)) }
517 | public mutating func encode(_ value: Int64) throws { self.container.add(self.encoder.box(value)) }
518 | public mutating func encode(_ value: UInt) throws { self.container.add(self.encoder.box(value)) }
519 | public mutating func encode(_ value: UInt8) throws { self.container.add(self.encoder.box(value)) }
520 | public mutating func encode(_ value: UInt16) throws { self.container.add(self.encoder.box(value)) }
521 | public mutating func encode(_ value: UInt32) throws { self.container.add(self.encoder.box(value)) }
522 | public mutating func encode(_ value: UInt64) throws { self.container.add(self.encoder.box(value)) }
523 | public mutating func encode(_ value: String) throws { self.container.add(self.encoder.box(value)) }
524 |
525 | public mutating func encode(_ value: Float) throws {
526 | // Since the float may be invalid and throw, the coding path needs to contain this key.
527 | self.encoder.codingPath.append(DictionaryCodingKey(index: self.count))
528 | defer { self.encoder.codingPath.removeLast() }
529 | self.container.add(try self.encoder.box(value))
530 | }
531 |
532 | public mutating func encode(_ value: Double) throws {
533 | // Since the double may be invalid and throw, the coding path needs to contain this key.
534 | self.encoder.codingPath.append(DictionaryCodingKey(index: self.count))
535 | defer { self.encoder.codingPath.removeLast() }
536 | self.container.add(try self.encoder.box(value))
537 | }
538 |
539 | public mutating func encode(_ value: T) throws {
540 | self.encoder.codingPath.append(DictionaryCodingKey(index: self.count))
541 | defer { self.encoder.codingPath.removeLast() }
542 | self.container.add(try self.encoder.box(value))
543 | }
544 |
545 | public mutating func nestedContainer(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer {
546 | self.codingPath.append(DictionaryCodingKey(index: self.count))
547 | defer { self.codingPath.removeLast() }
548 |
549 | let dictionary = NSMutableDictionary()
550 | self.container.add(dictionary)
551 |
552 | let container = DictionaryCodingKeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: dictionary)
553 | return KeyedEncodingContainer(container)
554 | }
555 |
556 | public mutating func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
557 | self.codingPath.append(DictionaryCodingKey(index: self.count))
558 | defer { self.codingPath.removeLast() }
559 |
560 | let array = NSMutableArray()
561 | self.container.add(array)
562 | return _DictionaryUnkeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: array)
563 | }
564 |
565 | public mutating func superEncoder() -> Encoder {
566 | return _DictionaryReferencingEncoder(referencing: self.encoder, at: self.container.count, wrapping: self.container)
567 | }
568 | }
569 |
570 | extension _DictionaryEncoder : SingleValueEncodingContainer {
571 | // MARK: - SingleValueEncodingContainer Methods
572 | fileprivate func assertCanEncodeNewValue() {
573 | precondition(self.canEncodeNewValue, "Attempt to encode value through single value container when previously value already encoded.")
574 | }
575 |
576 | public func encodeNil() throws {
577 | assertCanEncodeNewValue()
578 | self.storage.push(container: NSNull())
579 | }
580 |
581 | public func encode(_ value: Bool) throws {
582 | assertCanEncodeNewValue()
583 | self.storage.push(container: self.box(value))
584 | }
585 |
586 | public func encode(_ value: Int) throws {
587 | assertCanEncodeNewValue()
588 | self.storage.push(container: self.box(value))
589 | }
590 |
591 | public func encode(_ value: Int8) throws {
592 | assertCanEncodeNewValue()
593 | self.storage.push(container: self.box(value))
594 | }
595 |
596 | public func encode(_ value: Int16) throws {
597 | assertCanEncodeNewValue()
598 | self.storage.push(container: self.box(value))
599 | }
600 |
601 | public func encode(_ value: Int32) throws {
602 | assertCanEncodeNewValue()
603 | self.storage.push(container: self.box(value))
604 | }
605 |
606 | public func encode(_ value: Int64) throws {
607 | assertCanEncodeNewValue()
608 | self.storage.push(container: self.box(value))
609 | }
610 |
611 | public func encode(_ value: UInt) throws {
612 | assertCanEncodeNewValue()
613 | self.storage.push(container: self.box(value))
614 | }
615 |
616 | public func encode(_ value: UInt8) throws {
617 | assertCanEncodeNewValue()
618 | self.storage.push(container: self.box(value))
619 | }
620 |
621 | public func encode(_ value: UInt16) throws {
622 | assertCanEncodeNewValue()
623 | self.storage.push(container: self.box(value))
624 | }
625 |
626 | public func encode(_ value: UInt32) throws {
627 | assertCanEncodeNewValue()
628 | self.storage.push(container: self.box(value))
629 | }
630 |
631 | public func encode(_ value: UInt64) throws {
632 | assertCanEncodeNewValue()
633 | self.storage.push(container: self.box(value))
634 | }
635 |
636 | public func encode(_ value: String) throws {
637 | assertCanEncodeNewValue()
638 | self.storage.push(container: self.box(value))
639 | }
640 |
641 | public func encode(_ value: Float) throws {
642 | assertCanEncodeNewValue()
643 | try self.storage.push(container: self.box(value))
644 | }
645 |
646 | public func encode(_ value: Double) throws {
647 | assertCanEncodeNewValue()
648 | try self.storage.push(container: self.box(value))
649 | }
650 |
651 | public func encode(_ value: T) throws {
652 | assertCanEncodeNewValue()
653 | try self.storage.push(container: self.box(value))
654 | }
655 | }
656 |
657 | // MARK: - Concrete Value Representations
658 | extension _DictionaryEncoder {
659 | /// Returns the given value boxed in a container appropriate for pushing onto the container stack.
660 | fileprivate func box(_ value: Bool) -> NSObject { return NSNumber(value: value) }
661 | fileprivate func box(_ value: Int) -> NSObject { return NSNumber(value: value) }
662 | fileprivate func box(_ value: Int8) -> NSObject { return NSNumber(value: value) }
663 | fileprivate func box(_ value: Int16) -> NSObject { return NSNumber(value: value) }
664 | fileprivate func box(_ value: Int32) -> NSObject { return NSNumber(value: value) }
665 | fileprivate func box(_ value: Int64) -> NSObject { return NSNumber(value: value) }
666 | fileprivate func box(_ value: UInt) -> NSObject { return NSNumber(value: value) }
667 | fileprivate func box(_ value: UInt8) -> NSObject { return NSNumber(value: value) }
668 | fileprivate func box(_ value: UInt16) -> NSObject { return NSNumber(value: value) }
669 | fileprivate func box(_ value: UInt32) -> NSObject { return NSNumber(value: value) }
670 | fileprivate func box(_ value: UInt64) -> NSObject { return NSNumber(value: value) }
671 | fileprivate func box(_ value: String) -> NSObject { return NSString(string: value) }
672 |
673 | fileprivate func box(_ float: Float) throws -> NSObject {
674 | guard !float.isInfinite && !float.isNaN else {
675 | guard case let .convertToString(positiveInfinity: posInfString,
676 | negativeInfinity: negInfString,
677 | nan: nanString) = self.options.nonConformingFloatEncodingStrategy else {
678 | throw EncodingError._invalidFloatingPointValue(float, at: codingPath)
679 | }
680 |
681 | if float == Float.infinity {
682 | return NSString(string: posInfString)
683 | } else if float == -Float.infinity {
684 | return NSString(string: negInfString)
685 | } else {
686 | return NSString(string: nanString)
687 | }
688 | }
689 |
690 | return NSNumber(value: float)
691 | }
692 |
693 | fileprivate func box(_ double: Double) throws -> NSObject {
694 | guard !double.isInfinite && !double.isNaN else {
695 | guard case let .convertToString(positiveInfinity: posInfString,
696 | negativeInfinity: negInfString,
697 | nan: nanString) = self.options.nonConformingFloatEncodingStrategy else {
698 | throw EncodingError._invalidFloatingPointValue(double, at: codingPath)
699 | }
700 |
701 | if double == Double.infinity {
702 | return NSString(string: posInfString)
703 | } else if double == -Double.infinity {
704 | return NSString(string: negInfString)
705 | } else {
706 | return NSString(string: nanString)
707 | }
708 | }
709 |
710 | return NSNumber(value: double)
711 | }
712 |
713 | fileprivate func box(_ date: Date) throws -> NSObject {
714 | switch self.options.dateEncodingStrategy {
715 | case .deferredToDate:
716 | // Must be called with a surrounding with(pushedKey:) call.
717 | // Dates encode as single-value objects; this can't both throw and push a container, so no need to catch the error.
718 | try date.encode(to: self)
719 | return self.storage.popContainer()
720 |
721 | case .secondsSince1970:
722 | return NSNumber(value: date.timeIntervalSince1970)
723 |
724 | case .millisecondsSince1970:
725 | return NSNumber(value: 1000.0 * date.timeIntervalSince1970)
726 |
727 | case .iso8601:
728 | if #available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
729 | return NSString(string: _iso8601Formatter.string(from: date))
730 | } else {
731 | fatalError("ISO8601DateFormatter is unavailable on this platform.")
732 | }
733 |
734 | case .formatted(let formatter):
735 | return NSString(string: formatter.string(from: date))
736 |
737 | case .custom(let closure):
738 | let depth = self.storage.count
739 | do {
740 | try closure(date, self)
741 | } catch {
742 | // If the value pushed a container before throwing, pop it back off to restore state.
743 | if self.storage.count > depth {
744 | let _ = self.storage.popContainer()
745 | }
746 |
747 | throw error
748 | }
749 |
750 | guard self.storage.count > depth else {
751 | // The closure didn't encode anything. Return the default keyed container.
752 | return NSDictionary()
753 | }
754 |
755 | // We can pop because the closure encoded something.
756 | return self.storage.popContainer()
757 | }
758 | }
759 |
760 | fileprivate func box(_ data: Data) throws -> NSObject {
761 | switch self.options.dataEncodingStrategy {
762 | case .deferredToData:
763 | // Must be called with a surrounding with(pushedKey:) call.
764 | let depth = self.storage.count
765 | do {
766 | try data.encode(to: self)
767 | } catch {
768 | // If the value pushed a container before throwing, pop it back off to restore state.
769 | // This shouldn't be possible for Data (which encodes as an array of bytes), but it can't hurt to catch a failure.
770 | if self.storage.count > depth {
771 | let _ = self.storage.popContainer()
772 | }
773 |
774 | throw error
775 | }
776 |
777 | return self.storage.popContainer()
778 |
779 | case .base64:
780 | return NSString(string: data.base64EncodedString())
781 |
782 | case .custom(let closure):
783 | let depth = self.storage.count
784 | do {
785 | try closure(data, self)
786 | } catch {
787 | // If the value pushed a container before throwing, pop it back off to restore state.
788 | if self.storage.count > depth {
789 | let _ = self.storage.popContainer()
790 | }
791 |
792 | throw error
793 | }
794 |
795 | guard self.storage.count > depth else {
796 | // The closure didn't encode anything. Return the default keyed container.
797 | return NSDictionary()
798 | }
799 |
800 | // We can pop because the closure encoded something.
801 | return self.storage.popContainer()
802 | }
803 | }
804 |
805 | fileprivate func box(_ value: T) throws -> NSObject {
806 | return try self.box_(value) ?? NSDictionary()
807 | }
808 |
809 | // This method is called "box_" instead of "box" to disambiguate it from the overloads. Because the return type here is different from all of the "box" overloads (and is more general), any "box" calls in here would call back into "box" recursively instead of calling the appropriate overload, which is not what we want.
810 | fileprivate func box_(_ value: T) throws -> NSObject? {
811 | if T.self == Date.self || T.self == NSDate.self {
812 | // Respect Date encoding strategy
813 | return try self.box((value as! Date))
814 | } else if T.self == Data.self || T.self == NSData.self {
815 | // Respect Data encoding strategy
816 | return try self.box((value as! Data))
817 | } else if T.self == URL.self || T.self == NSURL.self {
818 | // Encode URLs as single strings.
819 | return self.box((value as! URL).absoluteString)
820 | } else if T.self == Decimal.self || T.self == NSDecimalNumber.self {
821 | // DictionarySerialization can natively handle NSDecimalNumber.
822 | return (value as! NSDecimalNumber)
823 | }
824 |
825 | // The value should request a container from the _DictionaryEncoder.
826 | let depth = self.storage.count
827 | do {
828 | try value.encode(to: self)
829 | } catch {
830 | // If the value pushed a container before throwing, pop it back off to restore state.
831 | if self.storage.count > depth {
832 | let _ = self.storage.popContainer()
833 | }
834 |
835 | throw error
836 | }
837 |
838 | // The top container should be a new container.
839 | guard self.storage.count > depth else {
840 | return nil
841 | }
842 |
843 | return self.storage.popContainer()
844 | }
845 | }
846 |
847 | // MARK: - _DictionaryReferencingEncoder
848 | /// _DictionaryReferencingEncoder is a special subclass of _DictionaryEncoder which has its own storage, but references the contents of a different encoder.
849 | /// It's used in superEncoder(), which returns a new encoder for encoding a superclass -- the lifetime of the encoder should not escape the scope it's created in, but it doesn't necessarily know when it's done being used (to write to the original container).
850 | fileprivate class _DictionaryReferencingEncoder : _DictionaryEncoder {
851 | // MARK: Reference types.
852 | /// The type of container we're referencing.
853 | private enum Reference {
854 | /// Referencing a specific index in an array container.
855 | case array(NSMutableArray, Int)
856 |
857 | /// Referencing a specific key in a dictionary container.
858 | case dictionary(NSMutableDictionary, String)
859 | }
860 |
861 | // MARK: - Properties
862 | /// The encoder we're referencing.
863 | fileprivate let encoder: _DictionaryEncoder
864 |
865 | /// The container reference itself.
866 | private let reference: Reference
867 |
868 | // MARK: - Initialization
869 | /// Initializes `self` by referencing the given array container in the given encoder.
870 | fileprivate init(referencing encoder: _DictionaryEncoder, at index: Int, wrapping array: NSMutableArray) {
871 | self.encoder = encoder
872 | self.reference = .array(array, index)
873 | super.init(options: encoder.options, codingPath: encoder.codingPath)
874 |
875 | self.codingPath.append(DictionaryCodingKey(index: index))
876 | }
877 |
878 | /// Initializes `self` by referencing the given dictionary container in the given encoder.
879 | fileprivate init(referencing encoder: _DictionaryEncoder,
880 | key: CodingKey, convertedKey: CodingKey, wrapping dictionary: NSMutableDictionary) {
881 | self.encoder = encoder
882 | self.reference = .dictionary(dictionary, convertedKey.stringValue)
883 | super.init(options: encoder.options, codingPath: encoder.codingPath)
884 |
885 | self.codingPath.append(key)
886 | }
887 |
888 | // MARK: - Coding Path Operations
889 | fileprivate override var canEncodeNewValue: Bool {
890 | // With a regular encoder, the storage and coding path grow together.
891 | // A referencing encoder, however, inherits its parents coding path, as well as the key it was created for.
892 | // We have to take this into account.
893 | return self.storage.count == self.codingPath.count - self.encoder.codingPath.count - 1
894 | }
895 |
896 | // MARK: - Deinitialization
897 | // Finalizes `self` by writing the contents of our storage to the referenced encoder's storage.
898 | deinit {
899 | let value: Any
900 | switch self.storage.count {
901 | case 0: value = NSDictionary()
902 | case 1: value = self.storage.popContainer()
903 | default: fatalError("Referencing encoder deallocated with multiple containers on stack.")
904 | }
905 |
906 | switch self.reference {
907 | case .array(let array, let index):
908 | array.insert(value, at: index)
909 |
910 | case .dictionary(let dictionary, let key):
911 | dictionary[NSString(string: key)] = value
912 | }
913 | }
914 | }
915 |
916 |
917 | #if canImport(Combine)
918 | import Combine
919 |
920 | extension DictionaryEncoder: TopLevelEncoder {
921 | public typealias Output = [String: Any]
922 | }
923 | #endif
924 |
--------------------------------------------------------------------------------
/Sources/DictionaryCoding/DictionaryErrors.swift:
--------------------------------------------------------------------------------
1 | //===----------------------------------------------------------------------===//
2 | //
3 | // This source file is largely a copy of code from Swift.org open source project's
4 | // files JSONEncoder.swift and Codeable.swift.
5 | //
6 | // Unfortunately those files do not expose the internal _JSONEncoder and
7 | // _JSONDecoder classes, which are in fact dictionary encoder/decoders and
8 | // precisely what we want...
9 | //
10 | // The original code is copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
11 | // Licensed under Apache License v2.0 with Runtime Library Exception
12 | //
13 | // See https://swift.org/LICENSE.txt for license information
14 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
15 | //
16 | // Modifications and additional code here is copyright (c) 2018 Sam Deane, and
17 | // is licensed under the same terms.
18 | //
19 | //===----------------------------------------------------------------------===//
20 |
21 | import Foundation
22 |
23 |
24 | //===----------------------------------------------------------------------===//
25 | // Error Utilities
26 | //===----------------------------------------------------------------------===//
27 |
28 | internal extension EncodingError {
29 | /// Returns a `.invalidValue` error describing the given invalid floating-point value.
30 | ///
31 | ///
32 | /// - parameter value: The value that was invalid to encode.
33 | /// - parameter path: The path of `CodingKey`s taken to encode this value.
34 | /// - returns: An `EncodingError` with the appropriate path and debug description.
35 | static func _invalidFloatingPointValue(_ value: T, at codingPath: [CodingKey]) -> EncodingError {
36 | let valueDescription: String
37 | if value == T.infinity {
38 | valueDescription = "\(T.self).infinity"
39 | } else if value == -T.infinity {
40 | valueDescription = "-\(T.self).infinity"
41 | } else {
42 | valueDescription = "\(T.self).nan"
43 | }
44 |
45 | let debugDescription = "Unable to encode \(valueDescription) directly in Dictionary. Use DictionaryEncoder.NonConformingFloatEncodingStrategy.convertToString to specify how the value should be encoded."
46 | return .invalidValue(value, EncodingError.Context(codingPath: codingPath, debugDescription: debugDescription))
47 | }
48 | }
49 |
50 | internal extension DecodingError {
51 | /// Returns a `.typeMismatch` error describing the expected type.
52 | ///
53 | /// - parameter path: The path of `CodingKey`s taken to decode a value of this type.
54 | /// - parameter expectation: The type expected to be encountered.
55 | /// - parameter reality: The value that was encountered instead of the expected type.
56 | /// - returns: A `DecodingError` with the appropriate path and debug description.
57 | static func _typeMismatch(at path: [CodingKey], expectation: Any.Type, reality: Any) -> DecodingError {
58 | let description = "Expected to decode \(expectation) but found \(_typeDescription(of: reality)) instead."
59 | return .typeMismatch(expectation, Context(codingPath: path, debugDescription: description))
60 | }
61 |
62 | /// Returns a description of the type of `value` appropriate for an error message.
63 | ///
64 | /// - parameter value: The value whose type to describe.
65 | /// - returns: A string describing `value`.
66 | /// - precondition: `value` is one of the types below.
67 | static func _typeDescription(of value: Any) -> String {
68 | if value is NSNull {
69 | return "a null value"
70 | } else if value is NSNumber /* FIXME: If swift-corelibs-foundation isn't updated to use NSNumber, this check will be necessary: || value is Int || value is Double */ {
71 | return "a number"
72 | } else if value is String {
73 | return "a string/data"
74 | } else if value is [Any] {
75 | return "an array"
76 | } else if value is [String : Any] {
77 | return "a dictionary"
78 | } else {
79 | return "\(type(of: value))"
80 | }
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/Tests/DictionaryCodingTests/CombineTests.swift:
--------------------------------------------------------------------------------
1 | #if canImport(Combine)
2 | import Combine
3 | import XCTest
4 | @testable import DictionaryCoding
5 |
6 |
7 | @available(macOS 10.15, iOS 13.0, tvOS 13.0, *)
8 | class CombineTests: XCTestCase {
9 | let people = [
10 | Person(name: "Sam", age: 48, pets: [Pet(name: "Morven"), Pet(name: "Rebus")]),
11 | Person(name: "Jon", age: 30, pets: [Pet(name: "Cougar")])
12 | ]
13 |
14 | let dicts : [[String: Any]] = [
15 | ["name": "Sam", "age": 48, "pets": [["name": "Morven"], ["name": "Rebus"]]],
16 | ["name": "Jon", "age": 30, "pets": [["name": "Cougar"]]]
17 | ]
18 |
19 | let encoder = DictionaryEncoder()
20 | let decoder = DictionaryDecoder()
21 |
22 | func testTopLevelDecoder() throws {
23 | var decoded = [Person]()
24 |
25 | _ = dicts.publisher
26 | .decode(type: Person.self, decoder: decoder)
27 | .assertNoFailure()
28 | .sink { decoded.append($0) }
29 |
30 | XCTAssertEqual(decoded, people)
31 | }
32 |
33 | func testTopLevelEncoder() throws {
34 | var encoded = [[String: Any]]()
35 |
36 | _ = people.publisher
37 | .encode(encoder: encoder)
38 | .assertNoFailure()
39 | .sink { encoded.append($0) }
40 |
41 | XCTAssertEqual(encoded.count, 2)
42 | let sam = encoded[0]
43 | XCTAssertEqual(sam["name"] as? String, "Sam")
44 | XCTAssertEqual(sam["age"] as? Int, 48)
45 | let samPets = sam["pets"] as? [[String: String]]
46 | XCTAssertEqual(samPets, dicts[0]["pets"] as? [[String: String]])
47 | let jon = encoded[1]
48 | XCTAssertEqual(jon["name"] as? String, "Jon")
49 | XCTAssertEqual(jon["age"] as? Int, 30)
50 | let jonPets = jon["pets"] as? [[String: String]]
51 | XCTAssertEqual(jonPets, dicts[1]["pets"] as? [[String: String]])
52 | }
53 |
54 | static var allTests = [
55 | ("testTopLevelDecoder", testTopLevelDecoder),
56 | ("testTopLevelEncoder", testTopLevelEncoder)
57 | ]
58 | }
59 |
60 | #endif
61 |
--------------------------------------------------------------------------------
/Tests/DictionaryCodingTests/DictionaryDecodingTests.swift:
--------------------------------------------------------------------------------
1 | import XCTest
2 | @testable import DictionaryCoding
3 |
4 | class DictionaryDecodingTests: XCTestCase {
5 |
6 | func testDecodingAllTheTypes() throws {
7 | let encoded : [String:Any] = ["uint32": 123456, "data": "dGVzdA==", "int16": -12345, "int64": -123456789, "uint8": 123, "date": 123456.789, "uint": 123456, "int": -123456, "int8": -123, "bool": 1, "int32": -123456, "double": 12345.6789, "uint64": 123456789, "float": 123.456, "uint16": 12345, "string": "blah"]
8 |
9 | let decoder = DictionaryDecoder()
10 | let decoded = try decoder.decode(AllTheTypes.self, from: encoded)
11 |
12 | XCTAssertEqual(decoded.string, "blah")
13 | XCTAssertEqual(decoded.int, -123456)
14 | XCTAssertEqual(decoded.int8, -123)
15 | XCTAssertEqual(decoded.int16, -12345)
16 | XCTAssertEqual(decoded.int32, -123456)
17 | XCTAssertEqual(decoded.int64, -123456789)
18 | }
19 |
20 | func testDecodingNSDictionary() throws {
21 | let pet1 : NSMutableDictionary = NSMutableDictionary()
22 | pet1["name"] = "Morven"
23 | let pet2 : NSMutableDictionary = NSMutableDictionary()
24 | pet2["name"] = "Rebus"
25 | let pets : NSMutableArray = NSMutableArray()
26 | pets.add(pet1)
27 | pets.add(pet2)
28 | let dict : NSMutableDictionary = NSMutableDictionary()
29 | dict["name"] = "Sam"
30 | dict["age"] = 48
31 | dict["pets"] = pets
32 |
33 | let decoder = DictionaryDecoder()
34 | let decoded = try decoder.decode(Person.self, from: dict)
35 |
36 | XCTAssertEqual(decoded.name, "Sam")
37 | XCTAssertEqual(decoded.age, 48)
38 | XCTAssertEqual(decoded.pets.count, 2)
39 | XCTAssertEqual(decoded.pets[0].name, "Morven")
40 | XCTAssertEqual(decoded.pets[1].name, "Rebus")
41 | }
42 |
43 | func testDecodingCFDictionary() throws {
44 | let dict = [ "name" : "Sam", "age" : 48, "pets" : [ ["name" : "Morven"], ["name" : "Rebus"]]] as CFDictionary
45 |
46 | let decoder = DictionaryDecoder()
47 | let decoded = try decoder.decode(Person.self, from: dict)
48 |
49 | XCTAssertEqual(decoded.name, "Sam")
50 | XCTAssertEqual(decoded.age, 48)
51 | XCTAssertEqual(decoded.pets.count, 2)
52 | XCTAssertEqual(decoded.pets[0].name, "Morven")
53 | XCTAssertEqual(decoded.pets[1].name, "Rebus")
54 | }
55 |
56 | func testDecodingSwiftDictionary() throws {
57 | let dict : [String:Any] = [ "name" : "Sam", "age" : 48, "pets" : [ ["name" : "Morven"], ["name" : "Rebus"]]]
58 |
59 | let decoder = DictionaryDecoder()
60 | let decoded = try decoder.decode(Person.self, from: dict)
61 |
62 | XCTAssertEqual(decoded.name, "Sam")
63 | XCTAssertEqual(decoded.age, 48)
64 | XCTAssertEqual(decoded.pets.count, 2)
65 | XCTAssertEqual(decoded.pets[0].name, "Morven")
66 | XCTAssertEqual(decoded.pets[1].name, "Rebus")
67 | }
68 |
69 | func testFailureWithMissingKeys() {
70 | let dict = [ "name" : "Sam", "age" : 48 ] as NSDictionary
71 | let decoder = DictionaryDecoder()
72 | XCTAssertThrowsError(try decoder.decode(Person.self, from: dict))
73 | }
74 |
75 | func testDecodingOptionalValues() throws {
76 | // the dictionary is missing some keys, but decoding shouldn't fail
77 | // as they correspond to properties that are optional in the struct
78 |
79 | let dict : [String:Any] = [ "name" : "Sam" ]
80 |
81 | let decoder = DictionaryDecoder()
82 | let decoded = try decoder.decode(Test.self, from: dict)
83 |
84 | XCTAssertEqual(decoded.name, "Sam")
85 | XCTAssertNil(decoded.label)
86 | }
87 |
88 | func testDecodingWithStandardDefaults() throws {
89 | // the dictionary is missing some keys, but they can be filled in
90 | // using default values if we set the missingValue strategy to .useDefault
91 | let dict : [String:Any] = [:]
92 |
93 | let decoder = DictionaryDecoder()
94 | decoder.missingValueDecodingStrategy = .useStandardDefault
95 |
96 | let decoded = try decoder.decode(AllTheTypes.self, from: dict)
97 |
98 | XCTAssertEqual(decoded.string, "")
99 | XCTAssertEqual(decoded.int, 0)
100 | XCTAssertEqual(decoded.int8, 0)
101 | XCTAssertEqual(decoded.int16, 0)
102 | XCTAssertEqual(decoded.int32, 0)
103 | XCTAssertEqual(decoded.int64, 0)
104 | XCTAssertEqual(decoded.uint, 0)
105 | XCTAssertEqual(decoded.uint8, 0)
106 | XCTAssertEqual(decoded.uint16, 0)
107 | XCTAssertEqual(decoded.uint32, 0)
108 | XCTAssertEqual(decoded.uint64, 0)
109 | XCTAssertEqual(decoded.bool, false)
110 | XCTAssertEqual(decoded.float, 0)
111 | XCTAssertEqual(decoded.double, 0)
112 | }
113 |
114 | func testDecodingWithDefaults() throws {
115 | // the dictionary is missing some keys, but they can be filled in
116 | // using default values if we set the missingValue strategy to .useDefault
117 | struct Test : Codable {
118 | let name : String
119 | let label : String
120 | let age : Int
121 | let flag : Bool
122 | let value : Double
123 | }
124 |
125 | let dict : [String:Any] = [ "name" : "Sam" ]
126 |
127 | let decoder = DictionaryDecoder()
128 |
129 | let defaults : [String:Any] = [ "String" : "default", "Int" : 123, "Bool" : true, "Double" : 123.456 ]
130 | decoder.missingValueDecodingStrategy = .useDefault(defaults: defaults)
131 |
132 | let decoded = try decoder.decode(Test.self, from: dict)
133 |
134 | XCTAssertEqual(decoded.name, "Sam")
135 | XCTAssertEqual(decoded.label, "default")
136 | XCTAssertEqual(decoded.age, 123)
137 | XCTAssertEqual(decoded.flag, true)
138 | XCTAssertEqual(decoded.value, 123.456)
139 | }
140 |
141 | func testDecodingStringFromURL() throws {
142 | // if we're expecting a string, but are given a URL, we should be able to cope
143 | struct Test : Decodable {
144 | let value : String
145 | }
146 |
147 | let decoder = DictionaryDecoder()
148 |
149 | let encoded1 : [String:Any] = ["value" : URL(fileURLWithPath: "/path")]
150 | let decoded1 = try decoder.decode(Test.self, from: encoded1)
151 | XCTAssertEqual(decoded1.value, "file:///path")
152 |
153 | let encoded2 : [String:Any] = ["value" : NSURL(fileURLWithPath: "/path")]
154 | let decoded2 = try decoder.decode(Test.self, from: encoded2)
155 | XCTAssertEqual(decoded2.value, "file:///path")
156 | }
157 |
158 | func testDecodingStringFromUUID() throws {
159 | // if we're expecting a string, but are given a UUID, we should be able to cope
160 | struct Test : Decodable {
161 | let value : String
162 | }
163 |
164 | let decoder = DictionaryDecoder()
165 |
166 | let uuid = UUID()
167 | let encoded : [String:Any] = ["value" : uuid]
168 | let decoded = try decoder.decode(Test.self, from: encoded)
169 | XCTAssertEqual(decoded.value, uuid.uuidString)
170 | }
171 |
172 | func testDecodingUUID() throws {
173 | // if we're expecting a UUID, but are given a String or a CFUUID, we should be able to cope
174 | struct Test : Decodable {
175 | let value : UUID
176 | }
177 |
178 | let decoder = DictionaryDecoder()
179 |
180 | let uuid = UUID()
181 | let encoded1 : [String:Any] = ["value" : uuid]
182 | let decoded1 = try decoder.decode(Test.self, from: encoded1)
183 | XCTAssertEqual(decoded1.value, uuid)
184 |
185 | let encoded2 : [String:Any] = ["value" : uuid.uuidString]
186 | let decoded2 = try decoder.decode(Test.self, from: encoded2)
187 | XCTAssertEqual(decoded2.value, uuid)
188 |
189 | let encoded3 : [String:Any] = ["value" : CFUUIDCreateFromString(nil, uuid.uuidString as CFString)!]
190 | let decoded3 = try decoder.decode(Test.self, from: encoded3)
191 | XCTAssertEqual(decoded3.value, uuid)
192 |
193 | // test for crashes when given other slightly random types...
194 | XCTAssertThrowsError(try decoder.decode(Test.self, from: ["value" : 123]))
195 | XCTAssertThrowsError(try decoder.decode(Test.self, from: ["value" : 123.456]))
196 | XCTAssertThrowsError(try decoder.decode(Test.self, from: ["value" : true]))
197 | XCTAssertThrowsError(try decoder.decode(Test.self, from: ["value" : URL(fileURLWithPath: "/test")]))
198 | }
199 |
200 | func testDecodingURL() throws {
201 | // if we're expecting a URL, we should be able to cope with getting a string, URL or NSURL
202 | // if we're expecting a UUID, but are given a String or a CFUUID, we should be able to cope
203 | struct Test : Decodable {
204 | let value : URL
205 | }
206 |
207 | let decoder = DictionaryDecoder()
208 |
209 | let url = URL(string: "http://elegantchaos.com")!
210 | let decoded1 = try decoder.decode(Test.self, from: ["value" : url])
211 | XCTAssertEqual(decoded1.value, url)
212 |
213 | let decoded2 = try decoder.decode(Test.self, from: ["value" : url.absoluteString])
214 | XCTAssertEqual(decoded2.value, url)
215 |
216 | let decoded3 = try decoder.decode(Test.self, from: ["value" : NSURL(string: url.absoluteString)!])
217 | XCTAssertEqual(decoded3.value, url)
218 | }
219 |
220 |
221 | static var allTests = [
222 | ("testDecodingAllTheTypes", testDecodingAllTheTypes),
223 | ("testDecodingNSDictionary", testDecodingNSDictionary),
224 | ("testDecodingCFDictionary", testDecodingCFDictionary),
225 | ("testFailureWithMissingKeys", testFailureWithMissingKeys),
226 | ("testDecodingOptionalValues", testDecodingOptionalValues),
227 | ("testDecodingWithDefaults", testDecodingWithDefaults),
228 | ("testDecodingStringFromURL", testDecodingStringFromURL),
229 | ("testDecodingStringFromUUID", testDecodingStringFromUUID),
230 | ("testDecodingUUID", testDecodingUUID),
231 | ("testDecodingURL", testDecodingURL),
232 | ]
233 | }
234 |
--------------------------------------------------------------------------------
/Tests/DictionaryCodingTests/DictionaryEncodingTests.swift:
--------------------------------------------------------------------------------
1 | import XCTest
2 | @testable import DictionaryCoding
3 |
4 | struct Pet : Codable, Equatable {
5 | let name : String
6 | }
7 |
8 | struct Person : Codable, Equatable {
9 | let name : String
10 | let age : Int
11 | let pets : [Pet]
12 | }
13 |
14 | struct Test : Codable {
15 | let name : String
16 | let label : String?
17 | }
18 |
19 | struct AllTheTypes : Codable {
20 | let string : String
21 | let int : Int
22 | let int8 : Int8
23 | let int16 : Int16
24 | let int32 : Int32
25 | let int64 : Int64
26 | let uint : UInt
27 | let uint8 : UInt8
28 | let uint16 : UInt16
29 | let uint32 : UInt32
30 | let uint64 : UInt64
31 | let float : Float
32 | let double : Double
33 | let bool : Bool
34 | let date : Date
35 | let data : Data
36 | }
37 |
38 | struct JustDate : Codable {
39 | let date : Date
40 | }
41 |
42 | struct JustData : Codable {
43 | let data : Data
44 | }
45 |
46 | class DictionaryEncodingTests: XCTestCase {
47 | func testEncodingDateFormats() throws {
48 | let date = JustDate(date: Date(timeIntervalSinceReferenceDate: 123456.789))
49 | let encoder = DictionaryEncoder()
50 | let encoded1 = try encoder.encode(date) as [String:Any]
51 | XCTAssertEqual(encoded1["date"] as? TimeInterval, 123456.789)
52 |
53 | encoder.dateEncodingStrategy = .iso8601
54 | let encoded2 = try encoder.encode(date) as [String:Any]
55 | XCTAssertEqual(encoded2["date"] as? String, "2001-01-02T10:17:36Z")
56 |
57 | encoder.dateEncodingStrategy = .millisecondsSince1970
58 | let encoded3 = try encoder.encode(date) as [String:Any]
59 | XCTAssertEqual(encoded3["date"] as? Double, 978430656789.0)
60 |
61 | encoder.dateEncodingStrategy = .secondsSince1970
62 | let encoded4 = try encoder.encode(date) as [String:Any]
63 | XCTAssertEqual(encoded4["date"] as? Double, 978430656.78900003)
64 |
65 | encoder.dateEncodingStrategy = .deferredToDate
66 | let encoded5 = try encoder.encode(date) as [String:Any]
67 | XCTAssertEqual(encoded5["date"] as? TimeInterval, 123456.789)
68 |
69 | let formatter = DateFormatter()
70 | formatter.locale = Locale(identifier: "en_US")
71 | formatter.setLocalizedDateFormatFromTemplate("MMMMd")
72 | encoder.dateEncodingStrategy = .formatted(formatter)
73 | let encoded6 = try encoder.encode(date) as [String:Any]
74 | XCTAssertEqual(encoded6["date"] as? String, "January 2")
75 |
76 | var customEncoderCalled = false
77 | encoder.dateEncodingStrategy = .custom({ (date, encoder) in
78 | customEncoderCalled = true
79 | try "some custom encoding".encode(to: encoder)
80 | })
81 | let encoded7 = try encoder.encode(date) as [String:Any]
82 | XCTAssertEqual(encoded7["date"] as? String, "some custom encoding")
83 | XCTAssertEqual(customEncoderCalled, true)
84 | }
85 |
86 | func testEncodingDataFormats() throws {
87 | let data = JustData(data: "blah".data(using: String.Encoding.utf8)!)
88 | let encoder = DictionaryEncoder()
89 | encoder.dataEncodingStrategy = .base64
90 | let encoded1 = try encoder.encode(data) as [String:Any]
91 | XCTAssertEqual(encoded1["data"] as? String, "YmxhaA==")
92 |
93 | encoder.dataEncodingStrategy = .deferredToData
94 | let encoded2 = try encoder.encode(data) as [String:Any]
95 | XCTAssertEqual(encoded2["data"] as! [Int8], [98, 108, 97, 104])
96 |
97 | var customEncoderCalled = false
98 | encoder.dataEncodingStrategy = .custom({ (date, encoder) in
99 | customEncoderCalled = true
100 | try "some custom encoding".encode(to: encoder)
101 | })
102 | let encoded3 = try encoder.encode(data) as [String:Any]
103 | XCTAssertEqual(encoded3["data"] as? String, "some custom encoding")
104 | XCTAssertEqual(customEncoderCalled, true)
105 | }
106 |
107 | func testEncodingAllTheTypes() throws {
108 | let date = Date(timeIntervalSinceReferenceDate: 123456.789)
109 | let test = AllTheTypes(
110 | string: "blah",
111 | int: -123456, int8: -123, int16: -12345, int32: -123456, int64: -123456789,
112 | uint: 123456, uint8: 123, uint16: 12345, uint32: 123456, uint64: 123456789,
113 | float: 123.456, double: 12345.6789,
114 | bool: true,
115 | date: date,
116 | data: "test".data(using: String.Encoding.utf8)!
117 | )
118 | let encoder = DictionaryEncoder()
119 | let encoded = try encoder.encode(test) as [String:Any]
120 | XCTAssertEqual(encoded["string"] as? String, "blah")
121 | XCTAssertEqual(encoded["int"] as? Int, -123456)
122 | XCTAssertEqual(encoded["int8"] as? Int8, -123)
123 | XCTAssertEqual(encoded["int16"] as? Int16, -12345)
124 | XCTAssertEqual(encoded["int32"] as? Int32, -123456)
125 | XCTAssertEqual(encoded["int64"] as? Int64, -123456789)
126 | XCTAssertEqual(encoded["uint"] as? UInt, 123456)
127 | XCTAssertEqual(encoded["uint8"] as? UInt8, 123)
128 | XCTAssertEqual(encoded["uint16"] as? UInt16, 12345)
129 | XCTAssertEqual(encoded["uint32"] as? UInt32, 123456)
130 | XCTAssertEqual(encoded["uint64"] as? UInt64, 123456789)
131 | XCTAssertEqual(encoded["float"] as? Float, 123.456)
132 | XCTAssertEqual(encoded["double"] as? Double, 12345.6789)
133 | XCTAssertEqual(encoded["bool"] as? Bool, true)
134 | XCTAssertEqual(encoded["date"] as? Double, 123456.789)
135 | XCTAssertEqual(encoded["data"] as? String, "dGVzdA==")
136 | }
137 |
138 | func testEncodingAsNSDictionary() throws {
139 | let test = Person(name: "Sam", age: 48, pets:[Pet(name: "Morven"), Pet(name: "Rebus")])
140 | let encoder = DictionaryEncoder()
141 | let encoded = try encoder.encode(test) as NSDictionary
142 | XCTAssertEqual(encoded["name"] as? String, "Sam")
143 | XCTAssertEqual(encoded["age"] as? Int, 48)
144 | let pets = encoded["pets"] as! [NSDictionary]
145 | XCTAssertEqual(pets[0]["name"] as? String, "Morven")
146 | XCTAssertEqual(pets[1]["name"] as? String, "Rebus")
147 | }
148 |
149 | func testEncodingAsSwiftDictionary() throws {
150 | let test = Person(name: "Sam", age: 48, pets:[Pet(name: "Morven"), Pet(name: "Rebus")])
151 | let encoder = DictionaryEncoder()
152 | let encoded = try encoder.encode(test) as [String:Any]
153 | XCTAssertEqual(encoded["name"] as? String, "Sam")
154 | XCTAssertEqual(encoded["age"] as? Int, 48)
155 | let pets = encoded["pets"] as! [NSDictionary]
156 | XCTAssertEqual(pets[0]["name"] as? String, "Morven")
157 | XCTAssertEqual(pets[1]["name"] as? String, "Rebus")
158 | }
159 |
160 | func testEncodingOptionalValues() throws {
161 | // the struct's optional values should not get written into the dictionary
162 | // if they are nil
163 |
164 | let test = Test(name: "Sam", label: nil)
165 | let encoder = DictionaryEncoder()
166 | let encoded = try encoder.encode(test) as NSDictionary
167 | XCTAssertEqual(encoded["name"] as? String, "Sam")
168 | XCTAssertEqual(encoded.allKeys.count, 1)
169 | }
170 |
171 | func testEncodingURL() throws {
172 | struct Test : Encodable {
173 | let value : URL
174 | }
175 |
176 | let string = "http://elegantchaos.com"
177 | let test = Test(value: URL(string: string)!)
178 | let encoder = DictionaryEncoder()
179 | let encoded = try encoder.encode(test) as [String:Any]
180 |
181 | // currently URLs are encoded as strings
182 | XCTAssertEqual(encoded["value"] as? String, string)
183 | }
184 |
185 | static var allTests = [
186 | ("testEncodingDataFormats", testEncodingDataFormats),
187 | ("testEncodingDateFormats", testEncodingDateFormats),
188 | ("testEncodingAllTheTypes", testEncodingAllTheTypes),
189 | ("testEncodingAsNSDictionary", testEncodingAsNSDictionary),
190 | ("testEncodingAsSwiftDictionary", testEncodingAsSwiftDictionary),
191 | ("testEncodingOptionalValues", testEncodingOptionalValues),
192 | ("testEncodingURL", testEncodingURL),
193 | ]
194 | }
195 |
--------------------------------------------------------------------------------
/Tests/LinuxMain.swift:
--------------------------------------------------------------------------------
1 | import XCTest
2 | @testable import DictionaryTests
3 |
4 | XCTMain([
5 | testCase(DictionaryTests.allTests),
6 | ])
7 |
--------------------------------------------------------------------------------