Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SebastianCiuca opened this issue Mar 5, 2024 · 0 comments
Open

Comments

@SebastianCiuca
Copy link

SebastianCiuca commented Mar 5, 2024

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant