Java – how to serialize the last class that cannot be serialized by a third party (such as Google’s latlng class)?

I use Google's latlng class in V2 Google play service That particular class is final and does not implement Java io. Serializable. Is there any way to make latlng class serializable?

public class MyDummyClass implements java.io.Serializable {
    private com.google.android.gms.maps.model.LatLng mLocation;

    // ...
}

I don't want to declare Mlocation short

Solution

It is not serializable, but it is wrapperable if this will be an option If not, you can handle serialization yourself:

public class MyDummyClass implements java.io.Serialiazable {
    // mark it transient so defaultReadObject()/defaultWriteObject() ignore it
    private transient com.google.android.gms.maps.model.LatLng mLocation;

    // ...

    private void writeObject(ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();
        out.writeDouble(mLocation.latitude);
        out.writeDouble(mLocation.longitude);
    }

    private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
        in.defaultReadObject();
        mLocation = new LatLng(in.readDouble(),in.readDouble());
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>