如何编码包装器类型,以使其无法在容器中嵌套?

发布于 2025-02-08 17:10:18 字数 1067 浏览 2 评论 0原文

通常,如果我有这样的结构:

struct Box: Codable {
  let id: String

  /// This is an expression (e.g. `x + 3`) rather than a number.
  let height: String
}

它将被编码为JSON,如下所示:

{
  "id": "box1",
  "height": "x + 3"
}

问题是我想引入一个新的包装器类型expression,以使我显然不应该使用正常该属性中的字符串:

struct Box: Codable {
  let id: String
  let height: Expression
}

struct Expression: Codable {
  let string: String
}

现在使用此包装器类型更清楚API,但是现在嵌套了JSON:

{
  "id": "box1",
  "height": {
    "string": "x + 3"
  }
}

我想删除此嵌套,因此看起来再次这样:

{
  "id": "box1",
  "height": "x + 3"
}

我宁愿不要在上覆盖任何内容box以来:

  1. 框可能还有许多其他属性,我宁愿不必手动维护编码/解码功能。
  2. 使用表达式的其他任何地方都应从这种非巢容器行为中受益,而我在不编写更编码的样板的情况下。

因此,我只想修改expression,并将其输出其编码数据而不引入额外的容器。


我尝试使用container.superencoder() as 此处提到的,但这保持了相同的层次结构,并重命名为“字符串” “ super” 。

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:

  1. Box may have many other properties and I'd rather not have to maintain the encode/decode functions manually.
  2. 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".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷心人i 2025-02-15 17:10:18

首先,请注意,代码的标准实现如下:

extension Expression: Codable {
  init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.string = try container.decode(String.self, forKey: .string)
  }

  func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(self.string, forKey: .string)
  }

  enum CodingKeys: CodingKey {
    case string
  }
}

请注意,它如何添加容器内部器/解码器。

取而代之的是,您要做的是与直接发送的编码器/解码器进行交互:

extension Expression: Codable {
  func encode(to encoder: Encoder) throws {
    try string.encode(to: encoder)
  }

  init(from decoder: Decoder) throws {
    string = try String(from: decoder)
  }
}

这将编码/解码对象,好像自定义包装器不存在:

{
  "id": "box1",
  "height": "x + 3"
}

First, notice that the standard implementation of Codable is as follows:

extension Expression: Codable {
  init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.string = try container.decode(String.self, forKey: .string)
  }

  func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(self.string, forKey: .string)
  }

  enum CodingKeys: CodingKey {
    case string
  }
}

Notice how it adds a container insider the encoder/decoder.

Instead, what you want to do is interact with the encoder/decoder that is sent directly:

extension Expression: Codable {
  func encode(to encoder: Encoder) throws {
    try string.encode(to: encoder)
  }

  init(from decoder: Decoder) throws {
    string = try String(from: decoder)
  }
}

This will encode/decode the object as though the custom wrapper doesn't exist:

{
  "id": "box1",
  "height": "x + 3"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文