Repository: incubator-weex Updated Branches: refs/heads/master bdd6c2e6b -> 0547dbaf6
[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/0547dbaf Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/0547dbaf Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/0547dbaf Branch: refs/heads/master Commit: 0547dbaf62f8f29a71a87915d8eacfcfd50008ef Parents: d7a12fd Author: jianbai.gbj <jianbai....@alibaba-inc.com> Authored: Fri Mar 9 16:23:44 2018 +0800 Committer: misakuo <misa...@apache.org> Committed: Tue Mar 20 12:40:31 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/0547dbaf/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/0547dbaf/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/0547dbaf/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"; +}