You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -125,15 +129,39 @@ public struct LosslessBooleanStrategy<Value: LosslessStringCodable>: LosslessDec
125
129
/// This is useful when data may return unpredictable values when a consumer is expecting a certain type. For instance,
126
130
/// if an API sends SKUs as either an `Int` or `String`, then a `@LosslessValue` can ensure the types are always decoded
127
131
/// as `String`s.
132
+
///
133
+
/// ```
134
+
/// struct Product: Codable {
135
+
/// @LosslessValue var sku: String
136
+
/// @LosslessValue var id: String
137
+
/// }
138
+
///
139
+
/// // json: { "sku": 87, "id": 123 }
140
+
/// let value = try JSONDecoder().decode(Product.self, from: json)
141
+
/// // value.sku == "87"
142
+
/// // value.id == "123"
143
+
/// ```
128
144
publictypealiasLosslessValue<T>=LosslessValueCodable<LosslessDefaultStrategy<T>> where T: LosslessStringCodable
129
145
130
146
/// Decodes Codable values into their respective preferred types.
131
147
///
132
148
/// `@LosslessBoolValue` attempts to decode Codable types into their respective preferred types while preserving the data.
133
149
///
134
150
/// - Note:
151
+
/// This uses a `LosslessBooleanStrategy` in order to prioritize boolean values, and as such, some integer values will be lossy.
152
+
///
153
+
/// For instance, if you decode `{ "some_type": 1 }` then `some_type` will be `true` and not `1`. If you do not want this
154
+
/// behavior then use `@LosslessValue` or create a custom `LosslessDecodingStrategy`.
155
+
///
156
+
/// ```
157
+
/// struct Example: Codable {
158
+
/// @LosslessBoolValue var foo: Bool
159
+
/// @LosslessValue var bar: Int
160
+
/// }
135
161
///
136
-
/// This differs from `@LosslessValue` in that it strongly prefers to keep the boolean value above all else, and some integer values will be lossy. For instance,
137
-
/// if you decode `{ "some_type": 1 }` then `some_type` will be `true` and not `1`. If you do not want this behavior then stick with `@LosslessValue` or create
138
-
/// your own custom `LosslessDecodingStrategy` type.
162
+
/// // json: { "foo": 1, "bar": 2 }
163
+
/// let value = try JSONDecoder().decode(Fixture.self, from: json)
164
+
/// // value.foo == true
165
+
/// // value.bar == 2
166
+
/// ```
139
167
publictypealiasLosslessBoolValue<T>=LosslessValueCodable<LosslessBooleanStrategy<T>> where T: LosslessStringCodable
0 commit comments