Skip to content
This repository was archived by the owner on Jul 14, 2021. It is now read-only.

Commit 30c6579

Browse files
committed
Add support for FastJson.
1 parent a17ea67 commit 30c6579

File tree

18 files changed

+360
-96
lines changed

18 files changed

+360
-96
lines changed

app/src/main/java/com/tmall/wireless/virtualviewdemo/ComponentActivity.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.support.annotation.Nullable;
2929
import android.text.TextUtils;
3030

31+
import com.alibaba.fastjson.JSON;
3132
import com.tmall.wireless.virtualviewdemo.preview.PreviewActivity;
3233

3334
import org.json.JSONException;
@@ -55,7 +56,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5556
mTemplateName = getIntent().getStringExtra("name");
5657
String data = getIntent().getStringExtra("data");
5758
if (!TextUtils.isEmpty(data)) {
58-
mJsonData = getJSONDataFromAsset(data);
59+
mJsonData = getFastJsonDataFromAsset(data);
5960
}
6061

6162
setTitle(mTemplateName);
@@ -81,4 +82,21 @@ private JSONObject getJSONDataFromAsset(String name) {
8182
return null;
8283
}
8384

85+
private com.alibaba.fastjson.JSONObject getFastJsonDataFromAsset(String name) {
86+
try {
87+
InputStream inputStream = getAssets().open(name);
88+
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream));
89+
StringBuilder sb = new StringBuilder();
90+
String str;
91+
while ((str = inputStreamReader.readLine()) != null) {
92+
sb.append(str);
93+
}
94+
inputStreamReader.close();
95+
return JSON.parseObject(sb.toString());
96+
} catch (IOException e) {
97+
e.printStackTrace();
98+
return null;
99+
}
100+
}
101+
84102
}

app/src/main/java/com/tmall/wireless/virtualviewdemo/custom/TMReminderTagsView.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import android.util.AttributeSet;
3737
import android.util.DisplayMetrics;
3838
import android.view.View;
39+
3940
import org.json.JSONArray;
4041
import org.json.JSONException;
4142

@@ -110,16 +111,16 @@ public void setTags(JSONArray tags) {
110111

111112
}
112113

113-
//public void setTags(com.alibaba.fastjson.JSONArray tags) {
114-
// if (tags != null) {
115-
// int length = tags.size();
116-
// String[] newTags = new String[length];
117-
// for (int i = 0; i < length; i++) {
118-
// newTags[i] = tags.getString(i);
119-
// }
120-
// setTags(newTags);
121-
// }
122-
//}
114+
public void setTags(com.alibaba.fastjson.JSONArray tags) {
115+
if (tags != null) {
116+
int length = tags.size();
117+
String[] newTags = new String[length];
118+
for (int i = 0; i < length; i++) {
119+
newTags[i] = tags.getString(i);
120+
}
121+
setTags(newTags);
122+
}
123+
}
123124

124125
public void setTags(String[] tags) {
125126
if (tags != null && this.tags != tags) {
@@ -153,28 +154,30 @@ protected void onDraw(Canvas canvas) {
153154
canvas.drawRect(tagRect, bgPaint);
154155

155156
canvas.drawText(tags[i], left + hPadding,
156-
canvas.getHeight() - getPaddingBottom() - textFontMetrics.descent,
157-
textPaint);
157+
canvas.getHeight() - getPaddingBottom() - textFontMetrics.descent,
158+
textPaint);
158159
left += tagsGap + eachTagWidth[i];
159160
}
160161
}
161162

162163
@Override
163164
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
164165
setMeasuredDimension(tagsWidth + getPaddingRight() + getPaddingLeft(),
165-
tagsHeight + getPaddingTop() + getPaddingBottom());
166+
tagsHeight + getPaddingTop() + getPaddingBottom());
166167
}
167168

168169
@Override
169170
public void onBind(Object jsonObject) {
170171
if (jsonObject instanceof JSONArray) {
171-
setTags((JSONArray)jsonObject);
172+
setTags((JSONArray) jsonObject);
172173
} else if (jsonObject instanceof String) {
173174
try {
174175
setTags(new JSONArray((String) jsonObject));
175176
} catch (JSONException e) {
176177
e.printStackTrace();
177178
}
179+
} else if (jsonObject instanceof com.alibaba.fastjson.JSONArray) {
180+
setTags((com.alibaba.fastjson.JSONArray) jsonObject);
178181
}
179182
}
180183

app/src/main/java/com/tmall/wireless/virtualviewdemo/preview/PreviewActivity.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.widget.LinearLayout;
1414
import android.widget.Toast;
1515

16+
import com.alibaba.fastjson.JSON;
1617
import com.google.gson.Gson;
1718
import com.google.gson.JsonObject;
1819
import com.google.gson.JsonSyntaxException;
@@ -55,7 +56,7 @@ public class PreviewActivity extends AppCompatActivity {
5556
private static final int FROM_REAL_PREVIEW = 2;
5657

5758
protected String mTemplateName;
58-
protected JSONObject mJsonData;
59+
protected com.alibaba.fastjson.JSONObject mJsonData;
5960

6061
public static void startForScanPreview(Context context, String url) {
6162
Intent intent = new Intent(context, PreviewActivity.class);
@@ -126,7 +127,7 @@ protected void onResume() {
126127
setTitle(mTemplateName);
127128
}
128129

129-
protected void preview(String templateName, JSONObject jsonData) {
130+
protected void preview(String templateName, com.alibaba.fastjson.JSONObject jsonData) {
130131
if (TextUtils.isEmpty(templateName)) {
131132
Log.e(TAG, "Template name should not be empty!!!!");
132133
return;
@@ -218,9 +219,9 @@ public void run() {
218219
JsonObject json = previewData.data;
219220
if (json != null) {
220221
try {
221-
mJsonData = new JSONObject(json.toString());
222+
mJsonData = JSON.parseObject(json.toString());
222223
preview(mTemplateName, mJsonData);
223-
} catch (JSONException e) {
224+
} catch (Exception e) {
224225
e.printStackTrace();
225226
}
226227
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ org.gradle.jvmargs=-Xmx2048m
4545
GROUP=com.alibaba.android
4646
ARTIFACT=virtualview
4747
VERSION=1
48-
VERSION_NAME=1.4.5
48+
VERSION_NAME=1.4.6
4949
PACKAGING_TYPE=aar
5050
systemProp.compileSdkVersion=26
5151
systemProp.targetSdkVersion=26

virtualview/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ dependencies {
111111
changing = true
112112
}
113113
compile 'com.android.support:recyclerview-v7:26.0.2@aar'
114+
compile 'com.alibaba:fastjson:1.1.70.android'
114115
}
115116

116117
File deployConfig = rootProject.file('jcenterInstall.gradle')

virtualview/src/main/java/com/tmall/wireless/vaf/expr/engine/DataManager.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import android.support.annotation.Keep;
2828
import android.text.TextUtils;
29+
2930
import org.json.JSONException;
3031
import org.json.JSONObject;
3132

@@ -39,24 +40,31 @@
3940
@Keep
4041
public class DataManager {
4142
private JSONObject mData = new JSONObject();
43+
private com.alibaba.fastjson.JSONObject mFastData = new com.alibaba.fastjson.JSONObject();
4244

4345
public DataManager() {
4446
try {
4547
mData.put("time", 10);
4648
} catch (JSONException e) {
4749
}
50+
mFastData.put("time", 10);
4851
}
4952

50-
public void replaceData(JSONObject obj) {
53+
public void replaceData(Object obj) {
5154
if (null != obj) {
52-
mData = obj;
55+
if (obj instanceof JSONObject) {
56+
mData = (JSONObject) obj;
57+
} else if (obj instanceof com.alibaba.fastjson.JSONObject) {
58+
mFastData = (com.alibaba.fastjson.JSONObject) obj;
59+
}
5360
}
5461
}
5562

5663
public void add(String key, Object value) {
5764
if (!TextUtils.isEmpty(key)) {
5865
try {
5966
mData.put(key, value);
67+
mFastData.put(key, value);
6068
} catch (JSONException e) {
6169
e.printStackTrace();
6270
}
@@ -71,6 +79,7 @@ public void add(Map<String, Object> map) {
7179
Object value = map.get(key);
7280
try {
7381
mData.put(key, value);
82+
mFastData.put(key, value);
7483
} catch (JSONException e) {
7584
e.printStackTrace();
7685
}
@@ -81,10 +90,11 @@ public void add(Map<String, Object> map) {
8190
public Object getData(String key) {
8291
return mData.opt(key);
8392
}
84-
85-
public void setData(String key, Object value) {
93+
94+
public void setData(String key, Object value) {
8695
try {
8796
mData.put(key, value);
97+
mFastData.put(key, value);
8898
} catch (JSONException e) {
8999
e.printStackTrace();
90100
}

virtualview/src/main/java/com/tmall/wireless/vaf/expr/engine/executor/ArrayExecutor.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
import java.util.Set;
2828

2929
import android.util.Log;
30+
3031
import com.tmall.wireless.vaf.expr.engine.DataManager;
3132
import com.tmall.wireless.vaf.expr.engine.data.Data;
3233
import com.tmall.wireless.vaf.expr.engine.data.Value;
34+
3335
import org.json.JSONArray;
3436
import org.json.JSONException;
3537
import org.json.JSONObject;
@@ -81,23 +83,36 @@ protected boolean call(int arrNameId, int resultRegId, Value param, Set<Object>
8183
Object p = param.getValue();
8284
if (p instanceof Integer) {
8385
ret = true;
84-
int index = (Integer)p;
86+
int index = (Integer) p;
8587
for (Object obj : objs) {
86-
JSONArray arr;
87-
if (obj instanceof DataManager) {
88-
arr = (JSONArray) mDataManager.getData(arrName);
89-
} else if (obj instanceof JSONObject) {
90-
arr = ((JSONObject)obj).optJSONArray(arrName);
91-
} else if (obj instanceof JSONArray) {
92-
arr = (JSONArray) obj;
93-
} else {
94-
Log.e(TAG, "error object:" + obj);
95-
ret = false;
96-
break;
97-
}
98-
88+
Object returnValue = null;
9989
try {
100-
Object returnValue = arr.get(index);
90+
if (obj instanceof DataManager) {
91+
if (mDataManager.getData(arrName) instanceof JSONArray) {
92+
JSONArray arr = (JSONArray) mDataManager.getData(arrName);
93+
returnValue = arr.get(index);
94+
} else if (mDataManager.getData(arrName) instanceof com.alibaba.fastjson.JSONArray) {
95+
com.alibaba.fastjson.JSONArray arr = (com.alibaba.fastjson.JSONArray) mDataManager.getData(arrName);
96+
returnValue = arr.get(index);
97+
}
98+
} else if (obj instanceof JSONObject) {
99+
JSONArray arr = ((JSONObject) obj).optJSONArray(arrName);
100+
returnValue = arr.get(index);
101+
} else if (obj instanceof JSONArray) {
102+
JSONArray arr = (JSONArray) obj;
103+
returnValue = arr.get(index);
104+
} else if (obj instanceof com.alibaba.fastjson.JSONArray) {
105+
com.alibaba.fastjson.JSONArray arr = (com.alibaba.fastjson.JSONArray) obj;
106+
returnValue = arr.get(index);
107+
} else if (obj instanceof com.alibaba.fastjson.JSONObject) {
108+
com.alibaba.fastjson.JSONArray arr = ((com.alibaba.fastjson.JSONObject) obj).getJSONArray(arrName);
109+
returnValue = arr.get(index);
110+
} else {
111+
Log.e(TAG, "error object:" + obj);
112+
ret = false;
113+
break;
114+
}
115+
101116
// Log.d(TAG, "returnValue:" + returnValue);
102117
Data result = mRegisterManger.get(resultRegId);
103118
if (null != returnValue) {
@@ -110,7 +125,7 @@ protected boolean call(int arrNameId, int resultRegId, Value param, Set<Object>
110125
result.reset();
111126
}
112127

113-
} catch (JSONException e) {
128+
} catch (Exception e) {
114129
e.printStackTrace();
115130
Log.e(TAG, "set value failed");
116131
ret = false;

virtualview/src/main/java/com/tmall/wireless/vaf/virtualview/core/ArrayAdapter.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class ArrayAdapter extends Adapter {
5050
private ConcurrentHashMap<String, Integer> mTypeMap = new ConcurrentHashMap<>();
5151
private SparseArrayCompat<String> mId2Types = new SparseArrayCompat<>();
5252
private JSONArray mData;
53+
private com.alibaba.fastjson.JSONArray mFastData;
5354

5455
public ArrayAdapter(VafContext context) {
5556
super(context);
@@ -59,8 +60,11 @@ public ArrayAdapter(VafContext context) {
5960
public void setData(Object str) {
6061
if (null == str) {
6162
mData = null;
63+
mFastData = null;
6264
} else if (str instanceof JSONArray) {
6365
mData = (JSONArray) str;
66+
} else if (str instanceof com.alibaba.fastjson.JSONArray) {
67+
mFastData = (com.alibaba.fastjson.JSONArray) str;
6468
} else {
6569
Log.e(TAG, "setData failed:" + str);
6670
}
@@ -70,6 +74,8 @@ public void setData(Object str) {
7074
public int getItemCount() {
7175
if (null != mData) {
7276
return mData.length();
77+
} else if (null != mFastData) {
78+
return mFastData.size();
7379
}
7480
return 0;
7581
}
@@ -90,18 +96,46 @@ public int getType(int pos) {
9096
}
9197
} catch (JSONException e) {
9298
}
99+
} else if (null != mFastData) {
100+
com.alibaba.fastjson.JSONObject obj = mFastData.getJSONObject(pos);
101+
String type = obj.getString(TYPE);
102+
if (mTypeMap.containsKey(type)) {
103+
return mTypeMap.get(type).intValue();
104+
} else {
105+
int newType = mTypeId.getAndIncrement();
106+
mTypeMap.put(type, newType);
107+
mId2Types.put(newType, type);
108+
return newType;
109+
}
93110
}
94111
return 0;
95112
}
96113

97114
@Override
98115
public void onBindViewHolder(Adapter.ViewHolder vh, int pos) {
99116
try {
100-
Object obj = mData.get(pos);
117+
Object obj = null;
118+
if (mData != null) {
119+
obj = mData.get(pos);
120+
} else if (mFastData != null) {
121+
obj = mFastData.get(pos);
122+
}
101123

102124
if (obj instanceof JSONObject) {
103-
JSONObject jObj = (JSONObject)obj;
104-
ViewBase vb = ((IContainer)vh.mItemView).getVirtualView();
125+
JSONObject jObj = (JSONObject) obj;
126+
ViewBase vb = ((IContainer) vh.mItemView).getVirtualView();
127+
if (null != vb) {
128+
vb.setVData(jObj);
129+
}
130+
131+
if (vb.supportExposure()) {
132+
mContext.getEventManager().emitEvent(EventManager.TYPE_Exposure, EventData.obtainData(mContext, vb));
133+
}
134+
135+
vb.ready();
136+
} else if (obj instanceof com.alibaba.fastjson.JSONObject) {
137+
com.alibaba.fastjson.JSONObject jObj = (com.alibaba.fastjson.JSONObject) obj;
138+
ViewBase vb = ((IContainer) vh.mItemView).getVirtualView();
105139
if (null != vb) {
106140
vb.setVData(jObj);
107141
}

0 commit comments

Comments
 (0)