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

Route object encode and decode #555

Closed
nastasiupta opened this issue Jun 26, 2021 · 4 comments
Closed

Route object encode and decode #555

nastasiupta opened this issue Jun 26, 2021 · 4 comments

Comments

@nastasiupta
Copy link

I want to save the object Route object to server, and recreate the Route object with the data from server in a future session.
I'm encoding the data as follows:

let encoder = JSONEncoder()
let legJson = try? encoder.encode(route)
data["json"] = legJson

I saw the following example from documentation to decode:
https://docs.mapbox.com/ios/navigation/examples/route-deserialization/

let legData = data["json"] as? [String: Any]
let origin = startPlace.location.coordinate2D
let destination = destinationPlace.location.coordinate2D
let routeOptions = NavigationRouteOptions(coordinates: [origin, destination])
let jsonData = try! JSONSerialization.data(withJSONObject: legData)
let decoder = JSONDecoder()
decoder.userInfo[.options] = routeOptions
let route: Route? = try? decoder.decode(Route.self, from: jsonData)

Route object is nil after this...
Also another thing to mention in Firestore I can see the data type as Blob

Versions:
pod 'Mapbox-iOS-SDK', '6.3.0'
pod 'MapboxDirections', '1.2.0'
pod 'MapboxNavigation', '1.4.1'
Xcode: 12.5.1

  • On an older version of Mapbox we had a property for Route object that could provide a JSON... but as I see during the SDKs upgrade, this one was removed. Is there another approach to get the JSON for Route object?
@1ec5
Copy link
Contributor

1ec5 commented Jun 29, 2021

Route object is nil after this...

Please replace the try? with a do/catch block; the caught error will indicate what the decoding implementation thinks is malformed about the JSON data.

On an older version of Mapbox we had a property for Route object that could provide a JSON... but as I see during the SDKs upgrade, this one was removed. Is there another approach to get the JSON for Route object?

JSONDecoder is the way to convert JSON to a Route. There’s also a convenience initializer that takes each individual piece of data in structured form.

@nastasiupta
Copy link
Author

nastasiupta commented Jul 4, 2021

@1ec5 I printed out some details...

    do {
        let route: Route? = try decoder.decode(Route.self, from: jsonData)
    } catch let error {
        print(jsonData.count)
        print(error)
        print(error.localizedDescription)
    }

3306228
keyNotFound(CodingKeys(stringValue: "legs", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "legs", intValue: nil) ("legs").", underlyingError: nil))
The data couldn’t be read because it is missing.
2192811
keyNotFound(CodingKeys(stringValue: "legs", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "legs", intValue: nil) ("legs").", underlyingError: nil))
The data couldn’t be read because it is missing.
347050
keyNotFound(CodingKeys(stringValue: "legs", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "legs", intValue: nil) ("legs").", underlyingError: nil))
The data couldn’t be read because it is missing.
1243495
keyNotFound(CodingKeys(stringValue: "legs", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "legs", intValue: nil) ("legs").", underlyingError: nil))
The data couldn’t be read because it is missing.

I have 4 different Route objects and looks like it fails for all of them...

@nastasiupta
Copy link
Author

@S2Ler @1ec5
Also, I wanted to ask why the JSON property was removed from the Route object and if the JSON property will be removed from Android SDK as well.

@1ec5
Copy link
Contributor

1ec5 commented Jul 7, 2021

No value associated with key CodingKeys(stringValue: "legs", intValue: nil) ("legs").", underlyingError: nil))

This means the JSON object you tried to decode is missing a legs property. When you decode a Route, it needs to match the format of the Directions API response. If you find it difficult to follow that documentation, consider manually converting your JSON data to a Route using Route(legs:shape:distance:expectedTravelTime:).

Also, I wanted to ask why the JSON property was removed from the Route object

#382 rewrote the library in pure Swift, enabling us to take advantage of the Swift standard library’s Codable protocol instead of maintaining custom JSON parsing code that was rife with implicitly unwrapped optionals. Route(json:legs:distance:expectedTravelTime:coordinates:speechLocale:options:) and the json property were entirely redundant to Codable at that point, and the json property took up significant memory, so we removed them. To convert a Route object to JSON, use JSONEncoder.

the JSON property will be removed from Android SDK as well.

Java doesn’t have an analogue to Codable, so the considerations above don’t apply. I’m unaware of any plans to change JSON facilities in mapbox-java. If that ever happens, you’ll see it take place in that repository, and I’m sure there will be a suitable replacement, because the navigation SDK depends on the ability to round-trip JSON for various tasks.

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

3 participants