static SparseArray readSparseArray0(Parcel in, ClassLoader loader) { int N = in.readInt(); if (N < 0) { return null; } SparseArray sa = new SparseArray(N); readSparseArrayInternal(in, sa, N, loader); return sa; } static void readSparseArrayInternal(Parcel in, SparseArray outVal, int N,
@Override public SparseArray<T> read(JsonReader in) throws IOException { SparseArray<T> map = new SparseArray<>(); in.beginObject(); while (in.hasNext()){ String name = in.nextName(); map.put(Integer.parseInt(name), mAdapter.read(in)); } in.endObject(); return map; }
@Override public SparseArray<Car3> read(JsonReader in) throws IOException { SparseArray<Car3> map = new SparseArray<>(); in.beginObject(); while (in.hasNext()){ String name = in.nextName(); map.put(Integer.parseInt(name), mAdapter.read(in)); } in.endObject(); return map; } }
@Override public void read(JsonReader in, GsonProperty property, Object t) throws IOException { final Class<?> simpleType = property.getType(); final SparseArray sa = new SparseArray(); in.beginObject(); if (simpleType.isPrimitive() || isBoxedClass(simpleType)) { while (in.hasNext()) { int key = Integer.parseInt(in.nextName()); sa.put(key, SupportUtils.readPrimitiveOrItsBox(in, property)); } }else{ TypeAdapter adapter = getTypeAdapter(simpleType); while (in.hasNext()) { int key = Integer.parseInt(in.nextName()); Object val = adapter.read(in); if (val != null) { sa.put(key, val); } } } setValue(property, t, sa); in.endObject(); } }
public TestGsonBean() { doubelSparse = new SparseArray<>(); doubelSparse.put(1, 1.6); doubelSparse.put(2, 2.6); }
/** * clear all key-values . * @return this. */ public SparseArrayPropertyEditor<D, V> clear(){ final int size = mMap.size(); if(size == 0){ return this; } SparseArray<V> newMap = new SparseArray<>(size * 4 /3 + 1); mMap.clearTo(DataMediatorDelegate.getDefault().getSparseArrayDelegate(newMap)); if(mMediator != null){ mMediator._getSparseArrayDispatcher().dispatchClearEntries(mProperty, newMap); } return this; }
@Override public SparseArrayPropertyEditor<IStudent, String> beginCityDataEditor() { if(cityData == null){ cityData = new SparseArray<>(); } return new SparseArrayPropertyEditor<IStudent, String>(this, DataMediatorDelegate.getDefault().getSparseArrayDelegate(cityData), null, null); }
@Override public SparseArrayPropertyEditor<IStudent, String> beginCityDataEditor() { if(cityData == null){ cityData = new SparseArray<>(); } return new SparseArrayPropertyEditor<IStudent, String>(this, DataMediatorDelegate.getDefault().getSparseArrayDelegate(cityData), null, null); }
/** * clear all key-values . * @return this. */ public SparseArrayPropertyEditor<D, V> clear(){ final int size = mMap.size(); if(size == 0){ return this; } SparseArray<V> newMap = new SparseArray<>(size * 4 /3 + 1); mMap.clearTo(DataMediatorDelegate.getDefault().getSparseArrayDelegate(newMap)); if(mMediator != null){ mMediator._getSparseArrayDispatcher().dispatchClearEntries(mProperty, newMap); } return this; }
@Override public SparseArrayPropertyEditor<IStudent, String> beginCityDataEditor() { IStudent target = _getTarget(); SparseArray<String> cityData = target.getCityData(); if(cityData == null){ cityData = new SparseArray<>(); target.setCityData(cityData); } return new SparseArrayPropertyEditor<IStudent, String>(this, DataMediatorDelegate.getDefault().getSparseArrayDelegate(cityData), PROP_cityData, this); }
@Override public SparseArrayPropertyEditor<IStudent, String> beginCityDataEditor() { IStudent target = _getTarget(); SparseArray<String> cityData = target.getCityData(); if(cityData == null){ cityData = new SparseArray<>(); target.setCityData(cityData); } return new SparseArrayPropertyEditor<IStudent, String>(this, DataMediatorDelegate.getDefault().getSparseArrayDelegate(cityData), PROP_cityData, this); }
private static TestGsonBean newTestGsonBean() { List<Car3> list = new ArrayList<>(); SparseArray<Car3> map = new SparseArray<>(); Car3[] arr = new Car3[3]; for(int i = 0 ; i < 3 ; i++){ Car3 car = new Car3(); car.setMark("AUDI"); car.setModel(2014); //2,1 car.setType("DIESEL"); car.setMaker("AUDI GERMANY"); car.setCost(i * 100); car.getColors().add("GREY"); car.getColors().add("BLACK"); car.getColors().add("WHITE"); list.add(car); map.put(i, car); arr[i] = car; } TestGsonBean tl = new TestGsonBean(); tl.setList(list); tl.setCarMap(map); //null时,map不参与序列化输出 tl.setCarsArr(arr); tl.setCar(arr[0]); return tl; }
SparseArray<TestGsonBean> sa = new SparseArray<>(); sa.put(1, newTestGsonBean()); sa.put(2, newTestGsonBean());