如何序列化第三方非序列化的最终类(例如谷歌的LatLng类)? [英] How to serialize a third-party non-serializable final class (e.g. google's LatLng class)?

查看:315
本文介绍了如何序列化第三方非序列化的最终类(例如谷歌的LatLng类)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google的LatLng课程来自v2 Google Play服务。该特定类是final,不实现 java.io.Serializable 。有什么办法可以让 LatLng 类实现 Serializable

I'm using Google's LatLng class from the v2 Google Play Services. That particular class is final and doesn't implement java.io.Serializable. Is there any way I can make that LatLng class implement Serializable?

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

    // ...
}

我不喜欢我想声明 mLocation 瞬态

推荐答案

它不是 Serializable 但它是 Parcelable ,如果那是一个选项。如果没有,你可以自己处理序列化:

It's not Serializable but it is Parcelable, if that would be an option instead. If not you could handle the 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());
    }
}

这篇关于如何序列化第三方非序列化的最终类(例如谷歌的LatLng类)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆