Skip to content

jiarWang/ParcelableKnife

Repository files navigation

ParcelKnife

ParcelKnife:一款通过注解可以由xxxBean自动生成xxxBeanModel implements Parcelable的工具。

描述

Android开发中,通常需要为后台接口返回的JSON数据建立一个xxxBean类来进行JSON数据的解析。

解析工作完成后,客户端便可以使用xxxBean的实例对象进行操作,然而如果想让

该对象在Activity之间或者进程之间通讯的话,xxxBean对象就需要是可序列化对象。

Android SDK 为普通类提供了两种序列化的方式,一种是实现Parcelable接口,另一种则是实现Serializable接口。

在可序列化对象用于网络通讯或磁盘文件的读写时,通常采用实现Serializable的方式比较便捷。其他情况下,官方建议通过实现Parcelable接口的方式实现对象的序列化。

可是!!!实现Parcelable接口的类书写通常重复且复杂。

通过ParcelKnife,你将只需要编写一条注解便能生成一个与xxxBean相关联的Parcelable类(xxxBeanModel

入门

  1. 下载与导入knife.jar

下载knife.jar,并将jar包放入MyApplication/app/libs

  1. 添加注解处理器与Javapoet相关依赖,并应用ParcelKnife
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.squareup:javapoet:1.9.0'
    compile files('libs/knife.jar')
}

由于还没有上传 mavenjcenter库,故暂时只能以此法导入项目

  1. 使用@ParcelKnife(beanTag="YourClassName")

通过在需要序列化的类上添加@ParcelKnife(beanTag="YourClassName"),然后点击"Make Project"便能自动生成xxxBeanModel; 例如:

如下是一个需要序列话的类,该类被用于解析登录接口的返回数据。现在需要一个实现了Parcelable的该类用于进程见通讯,如果直接将3个类直接implements Parcelable则代码阅读困难,且代码可读性极差。 通过使用使用ParcelKnife,编译器将自动为你生成3个实现了Parcelable接口的BeanModel类。ParcelKnife并不会改变量的名称,在其他地方你可以像使用Bean一样使用BeanModel

//: LoginBean.java
//第一处使用
@ParcelKnife(beanTag = "LoginBean")
public class LoginBean {

    public int levelType;
    public ObjectBean object;
    public List<ObjectBean> objectBeanList;
    public List<String> objectBeanArray;
    //第二处使用
    @ParcelKnife(beanTag = "LoginBean$ObjectBean")
    public static class ObjectBean {

        public int key;
        public String code;
        public MemberBean member;
        public String storeName;
        public String storeImage;
        public String status;
        public int levelType;
        public int teamMemberCount;
        public int teamOrderCount;
        //第三处使用
        @ParcelKnife(beanTag = "LoginBean$ObjectBean$MemberBean")
        public static class MemberBean {

            public int key;
            public String code;
            public String mobileNumber;
        }
    }
}

执行"Make Project"后,将自动生成3个BeanModel(目录:ProjectName/app/build/generated/source/apt/debug/xxxxx),; | |--LoginBeanModel.java |--MemberBeanModel.java |--ObjectBeanModel.java

LoginBeanModel.java

/**
 * BeanTag <==> LoginBean */
public class LoginBeanModel implements Parcelable {
  public static final Parcelable.Creator<LoginBeanModel> CREATOR = new Creator<LoginBeanModel>() {
          @Override
          public LoginBeanModel createFromParcel(Parcel in) {
              return new LoginBeanModel(in);
          }

          @Override
          public LoginBeanModel[] newArray(int size) {
              return new LoginBeanModel[size];
          }
      };

  public int levelType;

  public int teamMemberCount;

  public int teamOrderCount;

  public ObjectBeanModel object;

  public List<ObjectBeanModel> objectBeanList;

  public List<String> objectBeanArray;

  LoginBeanModel(Parcel in) {
    levelType = in.readInt();
    teamMemberCount = in.readInt();
    teamOrderCount = in.readInt();
    object = in.readParcelable(ObjectBeanModel.class.getClassLoader());
    objectBeanList = in.createTypedArrayList(ObjectBeanModel.CREATOR);
    objectBeanArray = in.readArrayList(String.class.getClassLoader());
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel parcel, int i) {
    parcel.writeInt(levelType);
    parcel.writeInt(teamMemberCount);
    parcel.writeInt(teamOrderCount);
    parcel.writeParcelable(object, i);
    parcel.writeTypedList(objectBeanList);
    parcel.writeStringList(objectBeanArray);
  }

MemberBeanModel.java

/**
 * BeanTag <==> LoginBean.ObjectBean.MemberBean */
public class MemberBeanModel implements Parcelable {
  public static final Parcelable.Creator<MemberBeanModel> CREATOR = new Creator<MemberBeanModel>() {
          @Override
          public MemberBeanModel createFromParcel(Parcel in) {
              return new MemberBeanModel(in);
          }

          @Override
          public MemberBeanModel[] newArray(int size) {
              return new MemberBeanModel[size];
          }
      };

  public int key;

  public String code;

  public String mobileNumber;

  MemberBeanModel(Parcel in) {
    key = in.readInt();
    code = in.readString();
    mobileNumber = in.readString();
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel parcel, int i) {
    parcel.writeInt(key);
    parcel.writeString(code);
    parcel.writeString(mobileNumber);
  }
}

ObjectBeanModel.java

/**
 * BeanTag <==> LoginBean.ObjectBean */
public class ObjectBeanModel implements Parcelable {
  public static final Parcelable.Creator<ObjectBeanModel> CREATOR = new Creator<ObjectBeanModel>() {
          @Override
          public ObjectBeanModel createFromParcel(Parcel in) {
              return new ObjectBeanModel(in);
          }

          @Override
          public ObjectBeanModel[] newArray(int size) {
              return new ObjectBeanModel[size];
          }
      };

  public int key;

  public String code;

  public MemberBeanModel member;

  public String storeName;

  public String storeImage;

  public String status;

  public int levelType;

  public int teamMemberCount;

  public int teamOrderCount;

  ObjectBeanModel(Parcel in) {
    key = in.readInt();
    code = in.readString();
    member = in.readParcelable(MemberBeanModel.class.getClassLoader());
    storeName = in.readString();
    storeImage = in.readString();
    status = in.readString();
    levelType = in.readInt();
    teamMemberCount = in.readInt();
    teamOrderCount = in.readInt();
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel parcel, int i) {
    parcel.writeInt(key);
    parcel.writeString(code);
    parcel.writeParcelable(member, i);
    parcel.writeString(storeName);
    parcel.writeString(storeImage);
    parcel.writeString(status);
    parcel.writeInt(levelType);
    parcel.writeInt(teamMemberCount);
    parcel.writeInt(teamOrderCount);
  }
}
  1. 说明
  • 由于所有的xxxBeanModel均在同一package下,所以确保在所有需要使用ParcelKnifexxxBean类中类的类名不能有重复。

About

Build your Parcelable-bean Automatically

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages