【问题标题】:How to serialize a third-party non-serializable final class (e.g. google's LatLng class)?如何序列化第三方不可序列化的最终类(例如谷歌的 LatLng 类)?
【发布时间】:2013-01-09 01:38:04
【问题描述】:

我正在使用来自 v2 Google Play 服务的Google's LatLng class。那个特定的类是最终的并且没有实现java.io.Serializable。有什么办法可以让LatLng 类实现Serializable

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

    // ...
}

我不想声明mLocation transient

【问题讨论】:

  • 这对我来说目前是个大问题,我不想让我的变量瞬态。我只需要传入两个双打(lat 和 lng),然后将它们变成我想使用它们的位置或 latlng。

标签: java serialization


【解决方案1】:

不是Serializable,而是Parcelable,如果可以的话。如果没有,您可以自己处理序列化:

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());
    }
}

【讨论】:

    【解决方案2】:

    你可以看看ObjectOutputStream

    首先,您必须为您的对象创建一个插入式替换:

        public class SerializableLatLng implements Serializable {
    
        //use whatever you need from LatLng
    
        public SerializableLatLng(LatLng latLng) {
            //construct your object from base class
        }   
    
        //this is where the translation happens
        private Object readResolve() throws ObjectStreamException {
            return new LatLng(...);
        }
    
    }
    

    然后创建一个合适的ObjectOutputSTream

    public class SerializableLatLngOutputStream extends ObjectOutputStream {
    
        public SerializableLatLngOutputStream(OutputStream out) throws IOException {
            super(out);
            enableReplaceObject(true);
        }
    
        protected SerializableLatLngOutputStream() throws IOException, SecurityException {
            super();
            enableReplaceObject(true);
        }
    
        @Override
        protected Object replaceObject(Object obj) throws IOException {
            if (obj instanceof LatLng) {
                return new SerializableLatLng((LatLng) obj);
            } else return super.replaceObject(obj);
        }
    
    }
    

    那么你就必须在序列化的时候使用这些流

    private static byte[] serialize(Object o) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new SerializableLatLngOutputStream(baos); 
        oos.writeObject(o);
        oos.flush();
        oos.close();
        return baos.toByteArray();
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 1970-01-01
      • 2014-11-03
      • 2011-12-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多