You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by mi...@apache.org on 2018/03/29 06:16:37 UTC
[4/6] incubator-weex git commit: [WEEX-244][android]Weex Android
Support W3c Force Api & wson improve for JSONField
[WEEX-244][android]Weex Android Support W3c Force Api & wson improve for JSONField
Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/ee195d57
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/ee195d57
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/ee195d57
Branch: refs/heads/master
Commit: ee195d57565c9d518de44fb0f1137d9d502b08fa
Parents: db3895a
Author: jianbai.gbj <ji...@alibaba-inc.com>
Authored: Fri Mar 9 16:23:44 2018 +0800
Committer: jianbai.gbj <ji...@alibaba-inc.com>
Committed: Fri Mar 9 16:48:10 2018 +0800
----------------------------------------------------------------------
.../main/java/com/taobao/weex/wson/Wson.java | 29 +++++++++++++-
.../java/com/taobao/weex/wson/AnnoTest.java | 41 ++++++++++++++++++++
.../test/java/com/taobao/weex/wson/Person.java | 30 ++++++++++++++
3 files changed, 98 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/ee195d57/android/sdk/src/main/java/com/taobao/weex/wson/Wson.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/main/java/com/taobao/weex/wson/Wson.java b/android/sdk/src/main/java/com/taobao/weex/wson/Wson.java
index 5741f8a..85d740f 100644
--- a/android/sdk/src/main/java/com/taobao/weex/wson/Wson.java
+++ b/android/sdk/src/main/java/com/taobao/weex/wson/Wson.java
@@ -24,6 +24,7 @@ import android.support.v4.util.LruCache;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.annotation.JSONField;
import com.taobao.weex.utils.WXLogUtils;
import java.lang.reflect.Array;
@@ -499,7 +500,7 @@ public class Wson {
if(object.getClass().isEnum()){
writeObject(JSON.toJSONString(object));
}else{
- writeMap(toMap(object));
+ writeAdapterObject(object);
}
refs.remove(refs.size()-1);
}
@@ -600,6 +601,19 @@ public class Wson {
position++;
}
+ private final void writeAdapterObject(Object object){
+ if(specialClass.get(object.getClass().getName()) != null){
+ writeObject(JSON.toJSON(object));
+ return;
+ }
+ try{
+ writeMap(toMap(object));
+ }catch (Exception e){
+ specialClass.put(object.getClass().getName(), true);
+ writeObject(JSON.toJSON(object));
+ }
+ }
+
private final Map toMap(Object object){
Map map = new JSONObject();
try {
@@ -637,7 +651,11 @@ public class Wson {
map.put(fieldName, value);
}
}catch (Exception e){
- throw new RuntimeException(e);
+ if(e instanceof RuntimeException){
+ throw (RuntimeException)e;
+ }else{
+ throw new RuntimeException(e);
+ }
}
return map;
}
@@ -749,6 +767,7 @@ public class Wson {
private static final String METHOD_PREFIX_IS = "is";
private static LruCache<String, List<Method>> methodsCache = new LruCache<>(128);
private static LruCache<String, List<Field>> fieldsCache = new LruCache<>(128);
+ private static LruCache<String, Boolean> specialClass = new LruCache<>(16);
private static final List<Method> getBeanMethod(String key, Class targetClass){
@@ -766,6 +785,9 @@ public class Wson {
String methodName = method.getName();
if(methodName.startsWith(METHOD_PREFIX_GET)
|| methodName.startsWith(METHOD_PREFIX_IS)) {
+ if(method.getAnnotation(JSONField.class) != null){
+ throw new UnsupportedOperationException("getBeanMethod JSONField Annotation Not Handled, Use toJSON");
+ }
methods.add(method);
}
}
@@ -785,6 +807,9 @@ public class Wson {
if((field.getModifiers() & Modifier.STATIC) != 0){
continue;
}
+ if(field.getAnnotation(JSONField.class) != null){
+ throw new UnsupportedOperationException("getBeanMethod JSONField Annotation Not Handled, Use toJSON");
+ }
fieldList.add(field);
}
fieldsCache.put(key, fieldList);
http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/ee195d57/android/sdk/src/test/java/com/taobao/weex/wson/AnnoTest.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/test/java/com/taobao/weex/wson/AnnoTest.java b/android/sdk/src/test/java/com/taobao/weex/wson/AnnoTest.java
new file mode 100644
index 0000000..1dce185
--- /dev/null
+++ b/android/sdk/src/test/java/com/taobao/weex/wson/AnnoTest.java
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.taobao.weex.wson;
+
+import com.alibaba.fastjson.JSON;
+
+import junit.framework.TestCase;
+
+import org.junit.Assert;
+
+/**
+ * Created by furture on 2018/3/9.
+ */
+
+public class AnnoTest extends TestCase {
+
+ public void testAnno(){
+ Person person = new Person();
+ person.id = "3333";
+ for(int i=0; i<10; i++) {
+ Assert.assertEquals(JSON.toJSONString(person),
+ JSON.toJSONString(Wson.parse(Wson.toWson(person))));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/ee195d57/android/sdk/src/test/java/com/taobao/weex/wson/Person.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/test/java/com/taobao/weex/wson/Person.java b/android/sdk/src/test/java/com/taobao/weex/wson/Person.java
new file mode 100644
index 0000000..8c9fd7b
--- /dev/null
+++ b/android/sdk/src/test/java/com/taobao/weex/wson/Person.java
@@ -0,0 +1,30 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.taobao.weex.wson;
+
+import com.alibaba.fastjson.annotation.JSONField;
+
+/**
+ * Created by furture on 2018/3/9.
+ */
+
+public class Person {
+ @JSONField(name = "uid")
+ public String id = "3333";
+}