alibaba / alibaba/HandyJSON

Is there a root key for deserialization (HelpingMapper / mapping)?

Open
#489 0 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

I'm trying to tidy up a model, and I'm attempting to gather a few properties into a structure. Basically, I'm trying to "pull" a few properties up a level. The problem is that, because the key for 'graphics' doesn't exist in the JSON (see examples below), the mapper won't even execute the TransformOf decoder (edit: this is my assumption).

**Is there a key that maps to the main / entire JSON so that I can deserialize from that?**

**Is there another way for me to achieve this?**

JSON example:
```
{
"structure": {
"imagePath": "somePath",
"colorCode": "someCode"
},
"count": 20
}
```

Struct example:

```
struct Graphics: HandyJSON {
var imagePath: String = ""
var color: String = ""
var code: Int = 0

mutating func mapping(mapper: HelpingMapper) {
mapper <<<
color <-- "structure.colorCode"
mapper <<<
imagePath <-- "structure.imagePath"
}
}

struct MainModel: HandyJSON {
var graphics = Graphics()

mutating func mapping(mapper: HelpingMapper) {
mapper <<<
graphics <-- TransformOf(
fromJSON: {
Page.Graphics.deserialize(from: $0)
},
toJSON: { _ in [:] }
)
}
}
```

What I meant by
> a key that maps to the main / entire JSON

is something like:
```
graphics <-- ("mainJSONKey", TransformOf(
fromJSON: {
Page.Graphics.deserialize(from: $0)
},
toJSON: { _ in [:] }
))
```

Also tried the following, without success:

```
mapper <<<
graphics.imagePath <-- "structure.imagePath"

```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing the HelpingMapper and TransformOf entry points described in the issue, focusing on how missing JSON keys are handled during deserialization. Check whether root-level or nested-property mapping is already supported and compare the behavior with existing mapping documentation or tests. Done should be a documented answer or a clearly scoped implementation path for mapping the shown JSON into Graphics.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.