The primary issue isn't with the JSON encoding but with SwiftData (and specifically a model defined using @Model).
RawRepresentable hits the same issue I described. If you do the standard "make a macOS app with SwiftData" flown in a recent Xcode and change the definition of Item to the following, the issue is reproducable
struct Problematic1: Codable {
init(from decoder: any Decoder) throws {
self.stringValue = try decoder.singleValueContainer().decode(String.self)
}
enum CodingKeys: CodingKey {
case stringValue
}
func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(stringValue)
}
init(stringValue: String) {
self.stringValue = stringValue
}
var stringValue: String
}
struct Problematic2: RawRepresentable, Codable {
init(rawValue: String) {
self.rawValue = rawValue
}
var rawValue: String
}
@Model
final class Item {
var timestamp: Date
var oneOptional: Problematic1? = Problematic1(stringValue: "1")// <-- OK
var one: Problematic1 = Problematic1(stringValue: "1") // <-- Causes issue
var twoOptional: Problematic2? = Problematic2(rawValue: "2") // <-- OK
var two: Problematic2 = Problematic2(rawValue: "2") // <-- Causes issue
init(timestamp: Date) {
self.timestamp = timestamp
}
}