Skip to content

Commit

Permalink
Enable customizing date and data property encoding
Browse files Browse the repository at this point in the history
We were unintentionally bypassing the encoder when converting properties to
strings, which broke things like JSONEncoder.dateEncodingStrategy.
  • Loading branch information
tgoyne committed Dec 20, 2023
1 parent a73034c commit 8192624
Show file tree
Hide file tree
Showing 7 changed files with 748 additions and 276 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ x.y.z Release notes (yyyy-MM-dd)
`Sendable` annotations ([PR #8435](https://github.com/realm/realm-swift/pull/8435)).
* `Realm.Error.subscriptionFailed` was reported with the incorrect error
domain, making it impossible to catch (since v10.42.2, [PR #8435](https://github.com/realm/realm-swift/pull/8435)).
* `@Persisted`'s Encodable implementation did not allow the encoder to
customize the encoding of values, which broke things like JSONEncoder's
`dateEncodingStrategy` ([#8425](https://github.com/realm/realm-swift/issues/8425)).

### Compatibility
* Realm Studio: 14.0.1 or later.
Expand Down
3 changes: 2 additions & 1 deletion RealmSwift/Decimal128.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ extension Decimal128: Encodable {
///
/// - Parameter encoder: The encoder to write data to.
public func encode(to encoder: Encoder) throws {
try self.stringValue.encode(to: encoder)
var container = encoder.singleValueContainer()
try container.encode(stringValue)
}
}

Expand Down
3 changes: 2 additions & 1 deletion RealmSwift/ObjectId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ extension ObjectId: Encodable {
///
/// - Parameter encoder: The encoder to write data to.
public func encode(to encoder: Encoder) throws {
try self.stringValue.encode(to: encoder)
var container = encoder.singleValueContainer()
try container.encode(stringValue)
}
}

Expand Down
3 changes: 2 additions & 1 deletion RealmSwift/Optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ extension RealmOptional: Codable where Value: Codable, Value: _RealmSchemaDiscov
}

public func encode(to encoder: Encoder) throws {
try self.value.encode(to: encoder)
var container = encoder.singleValueContainer()
try container.encode(value)
}
}

Expand Down
7 changes: 4 additions & 3 deletions RealmSwift/PersistedProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,14 @@ extension Persisted: Decodable where Value: Decodable {

extension Persisted: Encodable where Value: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch storage {
case .unmanaged(let value, _, _):
try value.encode(to: encoder)
try container.encode(value)
case .unmanagedObserved(let value, _):
try value.encode(to: encoder)
try container.encode(value)
case .unmanagedNoDefault:
try Value._rlmDefaultValue().encode(to: encoder)
try container.encode(Value._rlmDefaultValue())
default:
// We need a reference to the parent object to be able to read from
// a managed property. There's probably a way to do this with some
Expand Down
3 changes: 2 additions & 1 deletion RealmSwift/RealmProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ extension RealmProperty: Codable where Value: Codable {
}

public func encode(to encoder: Encoder) throws {
try self.value.encode(to: encoder)
var container = encoder.singleValueContainer()
try container.encode(value)
}
}

Expand Down
Loading

0 comments on commit 8192624

Please sign in to comment.