Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

fastjson 在解析 member 有特定前缀的时候有问题 #443

Closed
edhub opened this issue Nov 4, 2015 · 5 comments
Closed

fastjson 在解析 member 有特定前缀的时候有问题 #443

edhub opened this issue Nov 4, 2015 · 5 comments

Comments

@edhub
Copy link

edhub commented Nov 4, 2015

例如,非public成员使用m开头(这是Google推荐的安卓命名规则):
@JSONField(name="new_message")
private String mNewMessage;
public String getNewMessage { return mNewMessage; }
public String setNewMessage(String newMessage) { mNewMessage = newMessage; }

这时 fastjson 会无法正确的解析出 new_message 这个字段。

目前我们使用的解决方案是修改在 com.alibaba.fastjson.util.TypeUtils 中的方法

public static Field getField(Class<?> clazz, String fieldName) {
    String mFieldName = "m"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
    for (Field field : clazz.getDeclaredFields()) {
        if (fieldName.equals(field.getName()) || mFieldName.equals(field.getName())) {
            return field;
        }
    }

    Class<?> superClass = clazz.getSuperclass();
    if(superClass != null && superClass != Object.class) {
        return getField(superClass, fieldName);
    }

    return null;
}
@zhaiyuyong
Copy link

public String setNewMessage(String newMessage) { mNewMessage = newMessage; }

为什么这个函数类型是String?

@zhaiyuyong
Copy link

public static class A {
@JSONField(name = "new_message")
private String mNewMessage;
@JSONField(name = "new_message")
public String getNewMessage() {
return mNewMessage;
}
@JSONField(name = "new_message")
public void setNewMessage(String newMessage) {
mNewMessage = newMessage;
}

    @Override
    public String toString() {
        return "A [mNewMessage=" + mNewMessage + "]";
    }

}

你这个属性名称和 set get不一样时候需要在get和set方法上面加@JSONField(name = "new_message") 即可

@edhub
Copy link
Author

edhub commented Nov 12, 2015

这里的返回值String估计是用生成器然后改的时候弄错了

On Thu, Nov 12, 2015 at 12:37 PM, 翟玉勇 notifications@github.com wrote:

public String setNewMessage(String newMessage) { mNewMessage = newMessage;
}

为什么这个函数类型是String?


Reply to this email directly or view it on GitHub
#443 (comment).

@edhub
Copy link
Author

edhub commented Nov 12, 2015

恩,我知道可以在get和set方法上面加注解。但是:

  1. 使用代码生成工具的时候,注解是直接生成在field上面的
  2. mFieldName这种方式是 Google Android 官方推荐的命名约束,所以我认为 fastjson 有足够的理由来支持这种规范
  3. 手动在get/set方法上面加注解的话,会比较花时间,而且不值得

另外,我在原问题里面里面描述的临时解决方案只处理了 json -> object 的问题。object -> json
还需要处理另外一个地方。完整的patch请看附件。

对我们来说,patch一个小的改动已经完全满足了我们自己的需要。提出来的话,说不定也可以帮到其他人。

On Thu, Nov 12, 2015 at 12:38 PM, 翟玉勇 notifications@github.com wrote:

public static class A {
@JSONField(name = "new_message")
private String mNewMessage;
@JSONField(name = "new_message")
public String getNewMessage() {
return mNewMessage;
}
@JSONField(name = "new_message")
public void setNewMessage(String newMessage) {
mNewMessage = newMessage;
}

@Override
public String toString() {
    return "A [mNewMessage=" + mNewMessage + "]";
}

}

你这个属性名称和 set get不一样时候需要在get和set方法上面加@JSONField(name = "new_message") 即可


Reply to this email directly or view it on GitHub
#443 (comment).

commit 670f6bfc9556d503a7d1908b154e7941aa5ebd5a
Author: Gu Caipeng gucp@sudiyi.net
Date: Wed Nov 4 11:34:11 2015 +0800

Fixed a field resolving issue

When a non-public field start with m, which follows Android naming
convention, fastjson doesn't find the correct field.

diff --git a/.DS_Store b/.DS_Store
new file mode 100644
index 0000000..aed2c26
Binary files /dev/null and b/.DS_Store differ
diff --git a/out/.DS_Store b/out/.DS_Store
new file mode 100644
index 0000000..a010bb2
Binary files /dev/null and b/out/.DS_Store differ
diff --git a/out/artifacts/.DS_Store b/out/artifacts/.DS_Store
new file mode 100644
index 0000000..51f2f54
Binary files /dev/null and b/out/artifacts/.DS_Store differ
diff --git a/out/artifacts/fastjson_jar/.DS_Store b/out/artifacts/fastjson_jar/.DS_Store
new file mode 100644
index 0000000..6c0813a
Binary files /dev/null and b/out/artifacts/fastjson_jar/.DS_Store differ
diff --git a/src/main/java/com/alibaba/fastjson/parser/ParserConfig.java b/src/main/java/com/alibaba/fastjson/parser/ParserConfig.java
index 57fb28a..051d01a 100755
--- a/src/main/java/com/alibaba/fastjson/parser/ParserConfig.java
+++ b/src/main/java/com/alibaba/fastjson/parser/ParserConfig.java
@@ -359,6 +359,10 @@ public class ParserConfig {
if (field == null) {
field = getField0(clazz, "m_" + fieldName);
}

  •    if (field == null) {
    
  •        String mFieldName = "m" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    
  •        field = getField0(clazz, mFieldName);
    
  •    }
     return field;
    
    }

diff --git a/src/main/java/com/alibaba/fastjson/util/TypeUtils.java b/src/main/java/com/alibaba/fastjson/util/TypeUtils.java
index 8e30bd2..a96d4f0 100755
--- a/src/main/java/com/alibaba/fastjson/util/TypeUtils.java
+++ b/src/main/java/com/alibaba/fastjson/util/TypeUtils.java
@@ -1313,8 +1313,9 @@ public class TypeUtils {
}

 public static Field getField(Class<?> clazz, String fieldName) {
  •    String mFieldName = "m" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
     for (Field field : clazz.getDeclaredFields()) {
    
  •        if (fieldName.equals(field.getName())) {
    
  •        if (fieldName.equals(field.getName()) || mFieldName.equals(field.getName())) {
             return field;
         }
     }
    

@wenshao wenshao added this to the 1.2.9 milestone Apr 15, 2016
@wenshao wenshao closed this as completed Apr 15, 2016
@wenshao wenshao modified the milestones: 1.1.49.android, 1.2.9 Apr 15, 2016
@wenshao
Copy link
Member

wenshao commented Apr 15, 2016

在1.1.49.android版本中将会支持

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants