/** * Created on <%= Time.now.strftime("%Y-%m-%d") %> * Generated by Kilza https://github.com/Jaspion/Kilza */ package <%= @package %>; <% for @import in imports %> <%= @import %> <% end %> public class <%= @name %><% if @implements.length > 0 %> implements <%= implements.join(', ') %><% end %> { <% for @property in @properties %> <%= @property.constants %> <% end %> <% for @property in @properties %> <%= @property.declaration %> <% end %> public <%= @name %>() { } public <%= @name %>(JSONObject jsonObject) { parseObject(jsonObject); } public <%= @name %>(String jsonString) { try { parseString(jsonString); } catch (JSONException e) { e.printStackTrace(); } } protected void parseString(String jsonString) throws JSONException { <% if @properties.length == 1 %> if (!jsonString.trim().startsWith("{")) jsonString = "{\"" + FIELD_<%= @properties.first.name.upcase %> + "\" : " + jsonString + "}"; <% end %> JSONObject jsonObject = new JSONObject(jsonString); parseObject(jsonObject); } protected void parseObject(JSONObject object) { <% for @property in @properties %> <%= @property.parse_json %> <% end %> } <% for @property in @properties %> <%= @property.setter %> <%= @property.getter %> <% end %> <% @eq = [] @hs = [] for @property in @properties newname = @property.name.gsub(/_*(.+)/) { $1 }.capitalize if @property.boolean? @eq.push("((#{@name}) obj).is#{newname}().equals(#{@property.name})") if @property.key? else @eq.push("((#{@name}) obj).get#{newname}().equals(#{@property.name})") if @property.key? end @hs.push("#{@property.name}.hashCode()") if @property.key? end %> <% if @eq.length > 0 %> @Override public boolean equals(Object obj) { if (obj instanceof <%= @name %>) { return <%= @eq.join(" &&\n ") %> ; } return false; } <% end %> <% if @hs.length > 0 %> @Override public int hashCode(){ return (<%= @hs.join(" +\n ") %>); } <% end %> @Override public String toString() { Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); return gson.toJson(this); } <% if parcelable %> private <%= @name %>(Parcel in) { <% for @property in @properties %> <%= @property.read_parcel %> <% end %> } public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { <% for @property in @properties %> <%= @property.write_parcel %> <% end %> } public static final Parcelable.Creator<<%= @name %>> CREATOR = new Parcelable.Creator<<%= @name %>>() { public <%= @name %> createFromParcel(Parcel in) { return new <%= @name %>(in); } public <%= @name %>[] newArray(int size) { return new <%= @name %>[size]; } }; <% end %> }