1
0
mirror of https://github.com/TwidereProject/Twidere-Android synced 2024-12-18 03:28:55 +01:00

removed old json serializer

migrated all data models to new serialization method - seems worked very well!
This commit is contained in:
Mariotaku Lee 2015-04-24 00:27:13 +08:00
parent 240aaf1b22
commit b564feddfb
23 changed files with 167 additions and 982 deletions

View File

@ -3,7 +3,6 @@ include ':twidere.component.twitter4j'
include ':twidere.component.twitter4j.streaming'
include ':twidere.component.common'
include ':twidere.library.extension'
include ':twidere.component.jsonserializer'
include ':twidere.component.querybuilder'
include ':twidere.wear'
include ':twidere.donate.nyanwp'

View File

@ -82,6 +82,7 @@ public class ParcelableAccount implements Parcelable {
@JsonField(name = "is_dummy")
public boolean is_dummy;
public ParcelableAccount(final Cursor cursor, final Indices indices) {
is_dummy = false;
screen_name = indices.screen_name != -1 ? cursor.getString(indices.screen_name) : null;
@ -104,15 +105,7 @@ public class ParcelableAccount implements Parcelable {
color = source.readInt();
}
private ParcelableAccount() {
is_dummy = true;
screen_name = null;
name = null;
account_id = -1;
profile_image_url = null;
profile_banner_url = null;
color = 0;
is_activated = false;
public ParcelableAccount() {
}
@Override
@ -334,6 +327,9 @@ public class ParcelableAccount implements Parcelable {
@JsonField(name = "no_version_suffix")
public boolean no_version_suffix;
public ParcelableCredentials() {
}
public ParcelableCredentials(final Cursor cursor, final Indices indices) {
super(cursor, indices);
auth_type = cursor.getInt(indices.auth_type);

View File

@ -106,6 +106,9 @@ public class ParcelableDirectMessage implements Parcelable, Comparable<Parcelabl
@JsonField(name = "media")
public ParcelableMedia[] media;
public ParcelableDirectMessage() {
}
public ParcelableDirectMessage(final ContentValues values) {
text_plain = values.getAsString(DirectMessages.TEXT_PLAIN);
text_html = values.getAsString(DirectMessages.TEXT_HTML);

View File

@ -20,6 +20,7 @@
package org.mariotaku.twidere.model;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
@ -38,6 +39,9 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import twitter4j.CardEntity;
import twitter4j.CardEntity.BindingValue;
@ -64,17 +68,8 @@ public class ParcelableStatus implements Parcelable, Comparable<ParcelableStatus
return new ParcelableStatus[size];
}
};
public static final Comparator<ParcelableStatus> TIMESTAMP_COMPARATOR = new Comparator<ParcelableStatus>() {
@Override
public int compare(final ParcelableStatus object1, final ParcelableStatus object2) {
final long diff = object2.timestamp - object1.timestamp;
if (diff > Integer.MAX_VALUE) return Integer.MAX_VALUE;
if (diff < Integer.MIN_VALUE) return Integer.MIN_VALUE;
return (int) diff;
}
};
@JsonField(name = "id")
public long id;
public static final Comparator<ParcelableStatus> REVERSE_ID_COMPARATOR = new Comparator<ParcelableStatus>() {
@Override
@ -85,120 +80,127 @@ public class ParcelableStatus implements Parcelable, Comparable<ParcelableStatus
return (int) diff;
}
};
@JsonField(name = "id")
public long id;
@JsonField(name = "account_id")
public long account_id;
public long account_id;
@JsonField(name = "timestamp")
public long timestamp;
public long timestamp;
public static final Comparator<ParcelableStatus> TIMESTAMP_COMPARATOR = new Comparator<ParcelableStatus>() {
@Override
public int compare(final ParcelableStatus object1, final ParcelableStatus object2) {
final long diff = object2.timestamp - object1.timestamp;
if (diff > Integer.MAX_VALUE) return Integer.MAX_VALUE;
if (diff < Integer.MIN_VALUE) return Integer.MIN_VALUE;
return (int) diff;
}
};
@JsonField(name = "user_id")
public long user_id;
public long user_id;
@JsonField(name = "retweet_id")
public long retweet_id;
public long retweet_id;
@JsonField(name = "retweeted_by_id")
public long retweeted_by_id;
public long retweeted_by_id;
@JsonField(name = "retweet_timestamp")
public long retweet_timestamp;
public long retweet_timestamp;
@JsonField(name = "retweet_count")
public long retweet_count;
public long retweet_count;
@JsonField(name = "favorite_count")
public long favorite_count;
public long favorite_count;
@JsonField(name = "reply_count")
public long reply_count;
public long reply_count;
@JsonField(name = "descendent_reply_count")
public long descendent_reply_count;
public long descendent_reply_count;
@JsonField(name = "in_reply_to_status_id")
public long in_reply_to_status_id;
public long in_reply_to_status_id;
@JsonField(name = "in_reply_to_user_id")
public long in_reply_to_user_id;
public long in_reply_to_user_id;
@JsonField(name = "my_retweet_id")
public long my_retweet_id;
public long my_retweet_id;
@JsonField(name = "quote_id")
public long quote_id;
public long quote_id;
@JsonField(name = "quote_timestamp")
public long quote_timestamp;
public long quote_timestamp;
@JsonField(name = "quoted_by_user_id")
public long quoted_by_user_id;
public long quoted_by_user_id;
@JsonField(name = "is_gap")
public boolean is_gap;
public boolean is_gap;
@JsonField(name = "is_retweet")
public boolean is_retweet;
public boolean is_retweet;
@JsonField(name = "is_favorite")
public boolean is_favorite;
public boolean is_favorite;
@JsonField(name = "is_possibly_sensitive")
public boolean is_possibly_sensitive;
public boolean is_possibly_sensitive;
@JsonField(name = "user_is_following")
public boolean user_is_following;
public boolean user_is_following;
@JsonField(name = "user_is_protected")
public boolean user_is_protected;
public boolean user_is_protected;
@JsonField(name = "user_is_verified")
public boolean user_is_verified;
public boolean user_is_verified;
@JsonField(name = "is_quote")
public boolean is_quote;
public boolean is_quote;
@JsonField(name = "quoted_by_user_is_protected")
public boolean quoted_by_user_is_protected;
public boolean quoted_by_user_is_protected;
@JsonField(name = "quoted_by_user_is_verified")
public boolean quoted_by_user_is_verified;
public boolean quoted_by_user_is_verified;
@JsonField(name = "retweeted_by_name")
public String retweeted_by_name;
public String retweeted_by_name;
@JsonField(name = "retweeted_by_screen_name")
public String retweeted_by_screen_name;
public String retweeted_by_screen_name;
@JsonField(name = "retweeted_by_profile_image")
public String retweeted_by_profile_image;
public String retweeted_by_profile_image;
@JsonField(name = "text_html")
public String text_html;
public String text_html;
@JsonField(name = "text_plain")
public String text_plain;
public String text_plain;
@JsonField(name = "user_name")
public String user_name;
public String user_name;
@JsonField(name = "user_screen_name")
public String user_screen_name;
public String user_screen_name;
@JsonField(name = "in_reply_to_name")
public String in_reply_to_name;
public String in_reply_to_name;
@JsonField(name = "in_reply_to_screen_name")
public String in_reply_to_screen_name;
public String in_reply_to_screen_name;
@JsonField(name = "source")
public String source;
public String source;
@JsonField(name = "user_profile_image_url")
public String user_profile_image_url;
public String user_profile_image_url;
@JsonField(name = "text_unescaped")
public String text_unescaped;
public String text_unescaped;
@JsonField(name = "card_name")
public String card_name;
public String card_name;
@JsonField(name = "quote_text_html")
public String quote_text_html;
public String quote_text_html;
@JsonField(name = "quote_text_plain")
public String quote_text_plain;
public String quote_text_plain;
@JsonField(name = "quote_text_unescaped")
public String quote_text_unescaped;
public String quote_text_unescaped;
@JsonField(name = "quote_source")
public String quote_source;
public String quote_source;
@JsonField(name = "quoted_by_user_name")
public String quoted_by_user_name;
public String quoted_by_user_name;
@JsonField(name = "quoted_by_user_screen_name")
public String quoted_by_user_screen_name;
public String quoted_by_user_screen_name;
@JsonField(name = "quoted_by_user_profile_image")
public String quoted_by_user_profile_image;
public String quoted_by_user_profile_image;
@JsonField(name = "location")
public ParcelableLocation location;
public ParcelableLocation location;
@JsonField(name = "place_full_name")
public String place_full_name;
public String place_full_name;
@JsonField(name = "mentions")
public ParcelableUserMention[] mentions;
public ParcelableUserMention[] mentions;
@JsonField(name = "media")
public ParcelableMedia[] media;
public ParcelableMedia[] media;
@JsonField(name = "quote_media")
public ParcelableMedia[] quote_media;
public ParcelableMedia[] quote_media;
@JsonField(name = "card")
public ParcelableCardEntity card;
public ParcelableCardEntity card;
public ParcelableStatus(final Cursor c, final CursorIndices idx) {
id = idx.status_id != -1 ? c.getLong(idx.status_id) : -1;
@ -703,12 +705,19 @@ public class ParcelableStatus implements Parcelable, Comparable<ParcelableStatus
public ParcelableUser[] users;
@JsonField(name = "values")
public ParcelableValueItem[] values;
public Map<String, ParcelableBindingValue> values;
public ParcelableCardEntity(Parcel src) {
name = src.readString();
values = src.createTypedArray(ParcelableValueItem.CREATOR);
users = src.createTypedArray(ParcelableUser.CREATOR);
final Bundle bundle = src.readBundle(ParcelableBindingValue.class.getClassLoader());
for (String key : bundle.keySet()) {
if (values == null) {
values = new HashMap<>();
}
final ParcelableBindingValue value = bundle.getParcelable(key);
values.put(key, value);
}
}
public ParcelableCardEntity() {
@ -718,15 +727,7 @@ public class ParcelableStatus implements Parcelable, Comparable<ParcelableStatus
public ParcelableCardEntity(CardEntity card, long account_id) {
name = card.getName();
users = ParcelableUser.fromUsersArray(card.getUsers(), account_id);
final BindingValue[] bindingValues = card.getBindingValues();
if (bindingValues != null) {
values = new ParcelableValueItem[bindingValues.length];
for (int i = 0, j = bindingValues.length; i < j; i++) {
values[i] = new ParcelableValueItem(bindingValues[i]);
}
} else {
values = null;
}
values = ParcelableBindingValue.from(card.getBindingValues());
}
@Override
@ -734,10 +735,28 @@ public class ParcelableStatus implements Parcelable, Comparable<ParcelableStatus
return "ParcelableCardEntity{" +
"name='" + name + '\'' +
", users=" + Arrays.toString(users) +
", values=" + Arrays.toString(values) +
", values=" + values +
'}';
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeTypedArray(users, flags);
final Bundle bundle = new Bundle();
if (values != null) {
for (Entry<String, ParcelableBindingValue> entry : values.entrySet()) {
bundle.putParcelable(entry.getKey(), entry.getValue());
}
}
dest.writeBundle(bundle);
}
public static ParcelableCardEntity fromCardEntity(CardEntity card, long account_id) {
if (card == null) return null;
return new ParcelableCardEntity(card, account_id);
@ -752,173 +771,49 @@ public class ParcelableStatus implements Parcelable, Comparable<ParcelableStatus
}
}
public static ParcelableValueItem getValue(ParcelableCardEntity entity, String key) {
for (ParcelableValueItem item : entity.values) {
if (item.name.equals(key)) return item;
}
return null;
public static ParcelableBindingValue getValue(@Nullable ParcelableCardEntity entity, @Nullable String key) {
if (entity == null || entity.values == null || key == null) return null;
return entity.values.get(key);
}
@JsonObject
public static final class ParcelableImageValue implements Parcelable {
public static final class ParcelableBindingValue implements Parcelable {
public static final Parcelable.Creator<ParcelableImageValue> CREATOR = new Parcelable.Creator<ParcelableImageValue>() {
public static final Parcelable.Creator<ParcelableBindingValue> CREATOR = new Parcelable.Creator<ParcelableBindingValue>() {
@Override
public ParcelableImageValue createFromParcel(final Parcel in) {
return new ParcelableImageValue(in);
public ParcelableBindingValue createFromParcel(final Parcel in) {
return new ParcelableBindingValue(in);
}
@Override
public ParcelableImageValue[] newArray(final int size) {
return new ParcelableImageValue[size];
public ParcelableBindingValue[] newArray(final int size) {
return new ParcelableBindingValue[size];
}
};
@JsonField(name = "width")
public int width;
@JsonField(name = "height")
public int height;
@JsonField(name = "url")
public String url;
public ParcelableImageValue() {
}
public ParcelableImageValue(Parcel in) {
this.width = in.readInt();
this.height = in.readInt();
this.url = in.readString();
}
public ParcelableImageValue(ImageValue value) {
this.width = value.getWidth();
this.height = value.getHeight();
this.url = value.getUrl();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(width);
dest.writeInt(height);
dest.writeString(url);
}
}
@JsonObject
public static final class ParcelableUserValue implements Parcelable {
public static final Parcelable.Creator<ParcelableUserValue> CREATOR = new Parcelable.Creator<ParcelableUserValue>() {
@Override
public ParcelableUserValue createFromParcel(final Parcel in) {
return new ParcelableUserValue(in);
}
@Override
public ParcelableUserValue[] newArray(final int size) {
return new ParcelableUserValue[size];
}
};
@JsonField(name = "id")
public long id;
public ParcelableUserValue() {
}
public ParcelableUserValue(Parcel in) {
this.id = in.readLong();
}
public ParcelableUserValue(UserValue value) {
this.id = value.getUserId();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeTypedArray(values, flags);
dest.writeTypedArray(users, flags);
}
@JsonObject
public static final class ParcelableValueItem implements Parcelable {
public static final Creator<ParcelableValueItem> CREATOR = new Creator<ParcelableValueItem>() {
@Override
public ParcelableValueItem createFromParcel(final Parcel in) {
return new ParcelableValueItem(in);
}
@Override
public ParcelableValueItem[] newArray(final int size) {
return new ParcelableValueItem[size];
}
};
@JsonField(name = "name")
public String name;
@JsonField(name = "type")
public String type;
@JsonField(name = "value")
public Object value;
public String value;
public ParcelableValueItem() {
public ParcelableBindingValue() {
}
public ParcelableValueItem(Parcel in) {
this.name = in.readString();
public ParcelableBindingValue(Parcel in) {
this.type = in.readString();
this.value = in.readValue(ParcelableValueItem.class.getClassLoader());
this.value = in.readString();
}
public ParcelableValueItem(BindingValue bindingValue) {
name = bindingValue.getName();
type = bindingValue.getType();
switch (type) {
case BindingValue.TYPE_STRING:
value = ((StringValue) bindingValue).getValue();
break;
case BindingValue.TYPE_BOOLEAN:
value = ((BooleanValue) bindingValue).getValue();
break;
case BindingValue.TYPE_IMAGE:
value = new ParcelableImageValue((ImageValue) bindingValue);
break;
case BindingValue.TYPE_USER:
value = new ParcelableUserValue((UserValue) bindingValue);
break;
default:
value = null;
break;
public ParcelableBindingValue(BindingValue value) {
this.type = value.getType();
if (BindingValue.TYPE_IMAGE.equals(type)) {
this.value = ((ImageValue) value).getUrl();
} else if (BindingValue.TYPE_STRING.equals(type)) {
this.value = ((StringValue) value).getValue();
} else if (BindingValue.TYPE_BOOLEAN.equals(type)) {
this.value = String.valueOf(((BooleanValue) value).getValue());
} else if (BindingValue.TYPE_USER.equals(type)) {
this.value = String.valueOf(((UserValue) value).getUserId());
}
}
@ -927,23 +822,24 @@ public class ParcelableStatus implements Parcelable, Comparable<ParcelableStatus
return 0;
}
@Override
public String toString() {
return "ParcelableValueItem{" +
"name='" + name + '\'' +
", type='" + type + '\'' +
", value=" + value +
'}';
public static Map<String, ParcelableBindingValue> from(Map<String, BindingValue> bindingValues) {
if (bindingValues == null) return null;
final Map<String, ParcelableBindingValue> map = new HashMap<>();
for (Entry<String, BindingValue> entry : bindingValues.entrySet()) {
map.put(entry.getKey(), new ParcelableBindingValue(entry.getValue()));
}
return map;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(type);
dest.writeValue(value);
dest.writeString(value);
}
}
}
@Override

View File

@ -56,6 +56,9 @@ public class ParcelableStatusUpdate implements Parcelable {
@JsonField(name = "boolean")
public boolean is_possibly_sensitive;
public ParcelableStatusUpdate() {
}
/**
* @deprecated It has too much arguments to call, use
* <b>ParcelableStatusUpdate.Builder</b> instead.

View File

@ -120,6 +120,9 @@ public class ParcelableUser implements Parcelable, Comparable<ParcelableUser> {
@JsonField(name = "is_basic")
public boolean is_basic;
public ParcelableUser() {
}
public ParcelableUser(final long account_id, final long id, final String name,
final String screen_name, final String profile_image_url) {
this.account_id = account_id;

View File

@ -74,6 +74,8 @@ public class ParcelableUserList implements Parcelable, Comparable<ParcelableUser
@JsonField(name = "user_profile_image_url")
public String user_profile_image_url;
public ParcelableUserList() {
}
public ParcelableUserList(final Parcel in) {
position = in.readLong();

View File

@ -22,7 +22,6 @@ public class StatusShortenResult implements Parcelable {
}
};
@JsonField(name = "shortened")
public String shortened;
@ -32,6 +31,9 @@ public class StatusShortenResult implements Parcelable {
@JsonField(name = "error_message")
public String error_message;
public StatusShortenResult() {
}
public StatusShortenResult(final int errorCode, final String errorMessage) {
if (errorCode == 0) throw new IllegalArgumentException("Error code must not be 0");
shortened = null;

View File

@ -28,7 +28,6 @@ public class UploaderMediaItem implements Parcelable {
}
};
@JsonField(name = "path")
public String path;
@JsonField(name = "fd")
@ -36,6 +35,9 @@ public class UploaderMediaItem implements Parcelable {
@JsonField(name = "size")
public long size;
public UploaderMediaItem() {
}
public UploaderMediaItem(final Context context, final ParcelableMediaUpdate media) throws FileNotFoundException {
path = Uri.parse(media.uri).getPath();
final File file = new File(path);

View File

@ -1 +0,0 @@
/build

View File

@ -1,40 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
apply plugin: 'com.android.library'
apply from: rootProject.file('global.gradle')
android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}

View File

@ -1,17 +0,0 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/mariotaku/Tools/android-sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

View File

@ -1,32 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.jsonserializer;
import android.app.Application;
import android.test.ApplicationTestCase;
/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}

View File

@ -1,20 +0,0 @@
<!--
~ Twidere - Twitter client for Android
~
~ Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<manifest package="org.mariotaku.jsonserializer"/>

View File

@ -1,50 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.jsonserializer;
import org.json.JSONArray;
import org.mariotaku.jsonserializer.JSONParcelable.Creator;
/**
* Created by mariotaku on 15/1/1.
*/
public final class JSONArrayParcel {
private final JSONArray jsonArray;
JSONArrayParcel(JSONArray json) {
if (json == null) throw new NullPointerException();
jsonArray = json;
}
public String readString(int index) {
return jsonArray.optString(index);
}
public <T extends JSONParcelable> T readParcelable(int index, Creator<T> creator) {
final JSONParcel parcel = new JSONParcel(jsonArray.optJSONObject(index));
return creator.createFromParcel(parcel);
}
public int size() {
return jsonArray.length();
}
}

View File

@ -1,377 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.jsonserializer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
public final class JSONParcel {
private final JSONObject jsonObject;
JSONParcel() {
this(new JSONObject());
}
JSONParcel(final JSONObject json) {
if (json == null) throw new NullPointerException();
jsonObject = json;
}
public boolean contains(final String key) {
return jsonObject.has(key);
}
public JSONObject getJSON() {
return jsonObject;
}
public boolean isNull(String key) {
return jsonObject.isNull(key);
}
public boolean readBoolean(final String key) {
return jsonObject.optBoolean(key);
}
public boolean readBoolean(final String key, final boolean def) {
return jsonObject.optBoolean(key, def);
}
public double readDouble(final String key) {
return jsonObject.optDouble(key);
}
public double readDouble(final String key, final double def) {
return jsonObject.optDouble(key, def);
}
public float readFloat(final String key, final float def) {
return (float) readDouble(key, def);
}
public int readInt(final String key) {
return jsonObject.optInt(key);
}
public int readInt(final String key, final int def) {
return jsonObject.optInt(key, def);
}
public JSONObject readJSONObject(final String key) {
return jsonObject.optJSONObject(key);
}
public JSONArray readJSONArray(final String key) {
return jsonObject.optJSONArray(key);
}
public JSONArrayParcel readJSONArrayParcel(final String key) {
if (jsonObject.isNull(key)) return null;
return new JSONArrayParcel(readJSONArray(key));
}
public String[] readStringArray(final String key) {
if (jsonObject.isNull(key)) return null;
final JSONArray array = jsonObject.optJSONArray(key);
final String[] stringArray = new String[array.length()];
for (int i = 0, j = array.length(); i < j; i++) {
try {
stringArray[i] = array.getString(i);
} catch (JSONException e) {
return null;
}
}
return stringArray;
}
public long readLong(final String key) {
return jsonObject.optLong(key);
}
public long readLong(final String key, final long def) {
return jsonObject.optLong(key, def);
}
public Object readObject(final String key) {
return jsonObject.opt(key);
}
public <T extends JSONParcelable> T readParcelable(final String key, final JSONParcelable.Creator<T> creator) {
return JSONSerializer.createObject(creator, jsonObject.optJSONObject(key));
}
public <T extends JSONParcelable> T[] readParcelableArray(final String key, final JSONParcelable.Creator<T> creator) {
return JSONSerializer.createArray(creator, jsonObject.optJSONArray(key));
}
public String readString(final String key) {
return readString(key, null);
}
public String readString(final String key, final String def) {
return jsonObject.optString(key, def);
}
public void writeBoolean(final String key, final boolean value) {
try {
jsonObject.put(key, value);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeBooleanArray(final String key, final boolean[] value) {
if (key == null) return;
try {
if (value == null) {
jsonObject.put(key, JSONObject.NULL);
return;
}
final JSONArray array = new JSONArray();
for (final boolean item : value) {
array.put(item);
}
jsonObject.put(key, array);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeDouble(final String key, final double value) {
try {
jsonObject.put(key, value);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeDoubleArray(final String key, final double[] value) {
if (key == null) return;
try {
if (value == null) {
jsonObject.put(key, JSONObject.NULL);
return;
}
final JSONArray array = new JSONArray();
for (final double item : value) {
array.put(item);
}
jsonObject.put(key, array);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeFloat(final String key, final float value) {
writeDouble(key, value);
}
public void writeFloatArray(final String key, final float[] value) {
try {
if (value == null) {
jsonObject.put(key, JSONObject.NULL);
return;
}
final JSONArray array = new JSONArray();
for (final float item : value) {
array.put(item);
}
jsonObject.put(key, array);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeInt(final String key, final int value) {
try {
jsonObject.put(key, value);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeIntArray(final String key, final int[] value) {
if (key == null) return;
try {
if (value == null) {
jsonObject.put(key, JSONObject.NULL);
return;
}
final JSONArray array = new JSONArray();
for (final int item : value) {
array.put(item);
}
jsonObject.put(key, array);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeJSONArray(final String key, final JSONArray value) {
try {
jsonObject.put(key, value);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeJSONObject(final String key, final JSONObject value) {
try {
jsonObject.put(key, value);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeLong(final String key, final long value) {
try {
jsonObject.put(key, value);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeLongArray(final String key, final long[] value) {
if (key == null) return;
try {
if (value == null) {
jsonObject.put(key, JSONObject.NULL);
return;
}
final JSONArray array = new JSONArray();
for (final long item : value) {
array.put(item);
}
jsonObject.put(key, array);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeObject(final String key, final Object value) {
if (value instanceof JSONParcelable) {
writeParcelable(key, (JSONParcelable) value);
return;
}
try {
jsonObject.put(key, value);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeObjectArray(final String key, final Object[] value) {
if (key == null) return;
try {
if (value == null) {
jsonObject.put(key, JSONObject.NULL);
return;
}
final JSONArray array = new JSONArray();
for (final Object item : value) {
if (item instanceof JSONParcelable) {
final JSONObject json = JSONSerializer.toJSONObject((JSONParcelable) item);
array.put(json);
} else {
array.put(item);
}
}
jsonObject.put(key, array);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeObjectList(final String key, final List<Object> value) {
if (key == null) return;
writeObjectArray(key, value.toArray());
}
public <T extends JSONParcelable> void writeParcelable(final String key, final T value) {
if (key == null) return;
try {
if (value == null) {
jsonObject.put(key, JSONObject.NULL);
return;
}
final JSONObject json = JSONSerializer.toJSONObject(value);
jsonObject.put(key, json);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public <T extends JSONParcelable> void writeParcelableArray(final String key, final T[] value) {
if (key == null) return;
try {
if (value == null) {
jsonObject.put(key, JSONObject.NULL);
return;
}
final JSONArray array = new JSONArray();
for (final T item : value) {
final JSONObject json = JSONSerializer.toJSONObject(item);
array.put(json != null ? json : JSONObject.NULL);
}
jsonObject.put(key, array);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeString(final String key, final String value) {
if (key == null) return;
try {
jsonObject.put(key, value);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeStringArray(final String key, final String[] value) {
if (key == null) return;
try {
if (value == null) {
jsonObject.put(key, JSONObject.NULL);
return;
}
final JSONArray array = new JSONArray();
for (final String item : value) {
array.put(item);
}
jsonObject.put(key, array);
} catch (final JSONException e) {
e.printStackTrace();
}
}
public void writeValue(final String key, final Object value) {
if (key == null) return;
try {
jsonObject.put(key, value);
} catch (final JSONException e) {
e.printStackTrace();
}
}
}

View File

@ -1,40 +0,0 @@
/**
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
package org.mariotaku.jsonserializer;
public interface JSONParcelable {
public void writeToParcel(JSONParcel out);
public interface Creator<T extends JSONParcelable> {
public T createFromParcel(JSONParcel in);
public T[] newArray(int size);
}
}

View File

@ -1,148 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.jsonserializer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
public class JSONSerializer {
private static boolean debugMode = false;
public static <T extends JSONParcelable> T[] createArray(final JSONParcelable.Creator<T> creator,
final JSONArray json) {
if (creator == null) throw new NullPointerException("JSON_CREATOR must not be null!");
if (json == null) return null;
final int size = json.length();
final T[] array = creator.newArray(size);
for (int i = 0; i < size; i++) {
array[i] = creator.createFromParcel(new JSONParcel(json.optJSONObject(i)));
}
return array;
}
public static <T extends JSONParcelable> ArrayList<T> createArrayList(final JSONParcelable.Creator<T> creator,
final JSONArray json) {
if (creator == null) throw new NullPointerException("JSON_CREATOR must not be null!");
if (json == null) return null;
final int size = json.length();
final ArrayList<T> list = new ArrayList<T>(size);
for (int i = 0; i < size; i++) {
list.add(creator.createFromParcel(new JSONParcel(json.optJSONObject(i))));
}
return list;
}
public static <T extends JSONParcelable> T createObject(final JSONParcelable.Creator<T> creator,
final JSONObject json) {
if (creator == null) throw new NullPointerException("JSON_CREATOR must not be null!");
if (json == null) return null;
return creator.createFromParcel(new JSONParcel(json));
}
public static <T extends JSONParcelable> byte[] getByteArray(final T parcelable) {
final JSONObject json = toJSONObject(parcelable);
final String string = jsonToString(json);
if (string == null) return null;
return string.getBytes(Charset.defaultCharset());
}
public static <T extends JSONParcelable> byte[] getByteArray(final T[] array) {
final JSONArray json = toJSONArray(array);
final String string = jsonToString(json);
if (string == null) return null;
return string.getBytes(Charset.defaultCharset());
}
public static boolean isDebugMode() {
return debugMode;
}
public static void setIsDebugMode(final boolean debug) {
debugMode = debug;
}
public static <T extends JSONParcelable> byte[] toByteArray(final T parcelable) throws IOException {
final String jsonString = jsonToString(toJSONObject(parcelable));
if (jsonString == null) return null;
return jsonString.getBytes(Charset.defaultCharset());
}
public static <T extends JSONParcelable> byte[] toByteArray(final T[] array) throws IOException {
final String jsonString = jsonToString(toJSONArray(array));
if (jsonString == null) return null;
return jsonString.getBytes(Charset.defaultCharset());
}
public static <T extends JSONParcelable> JSONArray toJSONArray(final T[] array) {
if (array == null) return null;
final JSONArray json = new JSONArray();
for (final T parcelable : array) {
json.put(toJSONObject(parcelable));
}
return json;
}
public static <T extends JSONParcelable> String toJSONArrayString(final T[] array) {
return jsonToString(toJSONArray(array));
}
public static <T extends JSONParcelable> JSONObject toJSONObject(final T parcelable) {
if (parcelable == null) return null;
final JSONObject json = new JSONObject();
parcelable.writeToParcel(new JSONParcel(json));
return json;
}
public static <T extends JSONParcelable> String toJSONObjectString(final T parcelable) {
return jsonToString(toJSONObject(parcelable));
}
static String jsonToString(final JSONArray json) {
if (json == null) return null;
if (debugMode) {
try {
return json.toString(4);
} catch (final JSONException e) {
e.printStackTrace();
}
return json.toString();
} else
return json.toString();
}
static String jsonToString(final JSONObject json) {
if (json == null) return null;
if (debugMode) {
try {
return json.toString(4);
} catch (final JSONException e) {
e.printStackTrace();
}
return json.toString();
} else
return json.toString();
}
}

View File

@ -34,7 +34,7 @@ public interface CardEntity extends Serializable {
BindingValue getBindingValue(String key);
BindingValue[] getBindingValues();
java.util.Map<String, BindingValue> getBindingValues();
public interface BindingValue extends Serializable {

View File

@ -24,6 +24,7 @@ import org.json.JSONObject;
import org.mariotaku.twidere.library.twitter4j.BuildConfig;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@ -52,7 +53,7 @@ public class CardEntityJSONImpl implements CardEntity {
public CardEntityJSONImpl(JSONObject json) throws JSONException, TwitterException {
this.name = json.getString("name");
this.url = json.optString("url");
this.bindingValues = BindingValueImpl.valuesOf(json.getJSONObject("binding_values"));
this.bindingValues = Collections.unmodifiableMap(BindingValueImpl.valuesOf(json.getJSONObject("binding_values")));
if (!json.isNull("users")) {
final JSONObject usersJSON = json.getJSONObject("users");
final Iterator<String> keys = usersJSON.keys();
@ -82,8 +83,8 @@ public class CardEntityJSONImpl implements CardEntity {
}
@Override
public BindingValue[] getBindingValues() {
return bindingValues.values().toArray(new BindingValue[bindingValues.size()]);
public Map<String, BindingValue> getBindingValues() {
return bindingValues;
}

View File

@ -33,6 +33,7 @@ import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.multidex.MultiDexApplication;
import com.bluelinelabs.logansquare.LoganSquare;
import com.nostra13.universalimageloader.cache.disc.DiskCache;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.core.ImageLoader;

View File

@ -23,7 +23,9 @@ import android.support.v4.app.Fragment;
import org.mariotaku.twidere.fragment.support.CardBrowserFragment;
import org.mariotaku.twidere.model.ParcelableStatus.ParcelableCardEntity;
import org.mariotaku.twidere.model.ParcelableStatus.ParcelableCardEntity.ParcelableValueItem;
import org.mariotaku.twidere.model.ParcelableStatus.ParcelableCardEntity.ParcelableBindingValue;
import twitter4j.CardEntity.BindingValue;
/**
* Created by mariotaku on 15/1/1.
@ -41,8 +43,8 @@ public abstract class TwitterCardFragmentFactory {
}
public static Fragment createGenericPlayerFragment(ParcelableCardEntity card) {
final ParcelableValueItem player_url = ParcelableCardEntity.getValue(card, "player_url");
if (player_url == null) return null;
return CardBrowserFragment.show((String) player_url.value);
final ParcelableBindingValue player_url = ParcelableCardEntity.getValue(card, "player_url");
if (player_url == null || !BindingValue.TYPE_STRING.equals(player_url.type)) return null;
return CardBrowserFragment.show(player_url.value);
}
}

View File

@ -24,7 +24,7 @@ import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import org.mariotaku.twidere.model.ParcelableStatus.ParcelableCardEntity;
import org.mariotaku.twidere.model.ParcelableStatus.ParcelableCardEntity.ParcelableValueItem;
import org.mariotaku.twidere.model.ParcelableStatus.ParcelableCardEntity.ParcelableBindingValue;
/**
* Created by mariotaku on 15/1/1.
@ -57,8 +57,8 @@ public class TwitterCardUtils {
public static Point getCardSize(ParcelableCardEntity card) {
final ParcelableValueItem player_width = ParcelableCardEntity.getValue(card, "player_width");
final ParcelableValueItem player_height = ParcelableCardEntity.getValue(card, "player_height");
final ParcelableBindingValue player_width = ParcelableCardEntity.getValue(card, "player_width");
final ParcelableBindingValue player_height = ParcelableCardEntity.getValue(card, "player_height");
if (player_width != null && player_height != null) {
final int width = ParseUtils.parseInt(String.valueOf(player_width.value));
final int height = ParseUtils.parseInt(String.valueOf(player_height.value));