Normally if I have a struct like this:
struct Box: Codable {
let id: String
/// This is an expression (e.g. `x + 3`) rather than a number.
let height: String
}
It would get encoded as JSON as follows:
{
"id": "box1",
"height": "x + 3"
}
The problem is that I want to introduce a new wrapper type Expression
to make it obvious that I shouldn't be using normal strings in this property:
struct Box: Codable {
let id: String
let height: Expression
}
struct Expression: Codable {
let string: String
}
The API is now more clear with this wrapper type, but the JSON is now nested:
{
"id": "box1",
"height": {
"string": "x + 3"
}
}
I'd like to remove this nesting so it looks like this again:
{
"id": "box1",
"height": "x + 3"
}
I'd prefer not to override anything on Box
since:
- Box may have many other properties and I'd rather not have to maintain the encode/decode functions manually.
- Anywhere else that
Expression
is used should benefit from this non-nested container behavior without me writing more Codable boilerplate.
Therefore, I'd like to only modify Expression
, and get it to output its encoded data without introducing an extra container.
I tried using container.superEncoder()
as mentioned here, but that kept the same hierarchy and renamed "string"
with "super"
.