Skip to content

Commit

Permalink
Enable customizing date and data property encoding (#8443)
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 authored and dianaafanador3 committed Jan 18, 2024
1 parent bbedbc0 commit b3c486b
Show file tree
Hide file tree
Showing 7 changed files with 748 additions and 278 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ x.y.z Release notes (yyyy-MM-dd)
* None.

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-swift/issues/????), since v?.?.?)
* None.
* `@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)).

<!-- ### Breaking Changes - ONLY INCLUDE FOR NEW MAJOR version -->

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 b3c486b

Please sign in to comment.