alibaba / alibaba/HandyJSON

您好,我有个奇葩的 JSON -> Model 不知道能否可以用到 HandyJSON?

Open
#200 15 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
4.3k
Forks
677
PR merge metrics
No merged PRs in 30d

Description

您好,我现在后台返回的 JSON 中,有个 type 字段,这个字段的值是可能很多种类型的,有可能是字典、也有可能是数组,我打算这样设计这个 Model:
```swift
struct CYModel: HandyJSON {
var name: String?
var type: Any? // 有可能是 AType,有可能是 [BType] 或者 CType 等等
}

struct AType: HandyJSON {
var text: String?
}

struct BType: HandyJSON {
var identifier: String?
var fine: Bool?
}

struct CType: HandyJSON {
var age: Int?
}
```

如上,上面的 type 字段有可能是 AType,有可能是 [BType] 或者 CType 等等,我一开始想将这个 type 的类型声明为一个枚举的(因为可以知道具体有哪几种),即大致希望这样:
```swift
enum CYType: Any, HandyJSONEnum {
case a
case b
case c
}
```

但是由于 type 的类型不是固定某一类型(String、Int 之类等等),所以无法正确编译,个人也找不到如何解决这个问题,请问 HandyJSON 能处理这类「奇葩」问题吗?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the CYModel declaration and its polymorphic type field, then review how HandyJSON handles Any and HandyJSONEnum values. Determine whether the shown AType, BType, and CType alternatives are supported; done means documenting a supported modeling approach or confirming the limitation and required scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.