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

targetSdkVersion 29 does not serialize Location objects properly #1613

Closed
y20k opened this issue Nov 10, 2019 · 2 comments
Closed

targetSdkVersion 29 does not serialize Location objects properly #1613

y20k opened this issue Nov 10, 2019 · 2 comments

Comments

@y20k
Copy link

y20k commented Nov 10, 2019

I observed a problem when I updated from targetSdkVersion 27 to targetSdkVersion 29 in a project. GSON failes to serialize a Location object when using targetSdkVersion 29.

Location JSON string produced with targetSdkVersion 27

"location":{"mAltitude":0.0,"mBearing":0.0,"mBearingAccuracyDegrees":0.0,"mElapsedRealtimeNanos":33676559450037,"mHorizontalAccuracyMeters":20.0,"mLatitude":38.06770166666667,"mLongitude":-122.58285666666667,"mProvider":"gps","mSpeed":0.0,"mSpeedAccuracyMetersPerSecond":0.0,"mTime":1573423211000,"mVerticalAccuracyMeters":0.0}

Location JSON string produced with targetSdkVersion 29

"location":{"mElapsedRealtimeNanos":33918570517227}

Only mElapsedRealtimeNanos get serialized when using 29. Location implements @Parcelable as far as I know. I am using the following code to handle de/serialization:

data class WayPoint(var location: Location = Location(LocationManager.GPS_PROVIDER)): Parcelable {

    constructor(parcel: Parcel) : this(parcel.readParcelable(Location::class.java.classLoader) ?: Location(LocationManager.GPS_PROVIDER)) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeParcelable(location, flags)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<WayPoint> {
        override fun createFromParcel(parcel: Parcel): WayPoint {
            return WayPoint(parcel)
        }

        override fun newArray(size: Int): Array<WayPoint?> {
            return arrayOfNulls(size)
        }
    }
}
@lyubomyr-shaydariv
Copy link
Contributor

You should not serialize/deserialize classes you don't control over. See a similar issue: #1573

@y20k
Copy link
Author

y20k commented Nov 11, 2019

Thanks for the quick answer. I will copy over the values from the Location object to my own WayPoint class. That should result in a shorter JSON string as a side effect.

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

2 participants