AnderGoig / SwiftInstagram

Instagram API client written in Swift
https://git.io/vdNAn
MIT License
580 stars 79 forks source link

JSON parser breaks when parsing ids of locations within photo tags #32

Open fer662 opened 6 years ago

fer662 commented 6 years ago
(lldb) po error
▿ DecodingError
  ▿ dataCorrupted : Context
    ▿ codingPath : 4 elements
      - 0 : CodingKeys(stringValue: "data", intValue: nil)
      ▿ 1 : _JSONKey(stringValue: "Index 4", intValue: 4)
        - stringValue : "Index 4"
        ▿ intValue : Optional<Int>
          - some : 4
      - 2 : CodingKeys(stringValue: "location", intValue: nil)
      - 3 : CodingKeys(stringValue: "id", intValue: nil)
    - debugDescription : "Parsed JSON number <531383683876521> does not fit in Int."
    - underlyingError : nil

Instagram sends strings for ids for pretty much everything, but it seems it sends ints for locations. Huge ints at that. They don't fit in 64 bits and the swift parser doesn't like that at all.

Edited/censored offending JSON. The data within the location node hasn't been altered:

{
    "pagination": {
        "next_max_id": "censored",
        "next_url": "censored"
    },
    "data": [{
        "id": "123123123_123123123",
        "user": {
            "id": "12345678",
            "full_name": "Censored",
            "profile_picture": "censored",
            "username": "censored"
        },
        "images": {
            "thumbnail": {
                "width": 150,
                "height": 150,
                "url": "censored"
            },
            "low_resolution": {
                "width": 320,
                "height": 320,
                "url": "censored"
            },
            "standard_resolution": {
                "width": 640,
                "height": 640,
                "url": "censored"
            }
        },
        "created_time": "1515524886",
        "caption": {
            "id": "12345678",
            "text": "censored",
            "created_time": "1515524886",
            "from": {
                "id": "1297516609",
                "full_name": "Censored",
                "profile_picture": "censored",
                "username": "censored"
            }
        },
        "user_has_liked": false,
        "likes": {
            "count": 17
        },
        "tags": [],
        "filter": "Normal",
        "comments": {
            "count": 0
        },
        "type": "image",
        "link": "censored",
        "location": {
            "latitude": -22.951586524616,
            "longitude": -43.210792243481,
            "name": "Cristo Redentor Rio de Janeiro",
            "id": 531383683876521
        },
        "attribution": null,
        "users_in_photo": []
    }],
    "meta": {
        "code": 200
    }
}
fer662 commented 6 years ago

It can be resolved by using InstagramLocation instead of InstagramLocation in 2 relevant places.

AnderGoig commented 6 years ago

Hi @fer662, I'm afraid I don't understand your solution. Could you give me more details?

fer662 commented 6 years ago

I meant to say InstagramLocation<Int> to InstagramLocation<Decimal> but i might have hit some kind of markup. There!