Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable customizing date and data property encoding #8443

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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