Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support no parameter constructor #291

Closed
Nstd opened this issue Apr 20, 2020 · 10 comments
Closed

Support no parameter constructor #291

Nstd opened this issue Apr 20, 2020 · 10 comments
Assignees
Labels
feature request L4 Difficulty level 4 (Maximum is 9)
Milestone

Comments

@Nstd
Copy link
Collaborator

Nstd commented Apr 20, 2020

现在导入的时候设置Disable Kotlin Data Class可以生成普通的class,但是混淆以后仍然报错,尝试了一下,发现将构造方法改成无参的,变量都改成成员变量就好了。

报错的例子:

class Test (
    @JSONField(name = "a")
     var a = 0,
    @JSONField(name = "b")
    var b = listOf<Int>()
)

改成这样就好了:

class Test {
    @JSONField(name = "a")
     var a = 0
    @JSONField(name = "b")
    var b = listOf<Int>()
}

所以,希望能增加一个配置:是否以无参构造方式生成成员变量。

@Nstd Nstd changed the title Support member parameter Support no parameter constructor Apr 20, 2020
@xurui1995
Copy link
Collaborator

请问”混淆以后报错“是指?

@Nstd
Copy link
Collaborator Author

Nstd commented Apr 22, 2020

请问”混淆以后报错“是指?

说的是 b 不可为null,改成第二种写法就没报这个错了

@xurui1995
Copy link
Collaborator

@Nstd 可否提供具体的报错信息以及会报错的原json字符串

@Nstd
Copy link
Collaborator Author

Nstd commented May 14, 2020

@Nstd 可否提供具体的报错信息以及会报错的原json字符串

使用的JSON解析库是fastjson
环境:Release & 混淆
测试方法:使用测试对象1,所有用例都报错,但是改成测试对象2后都正常
fastjson的issue里看了下,应该是他们的问题,但是都没解决,所以想你们能不能帮忙加个这样的选项,解析成普通成员而非构造方式。

测试对象1

@Keep
class TestBean (
    @JSONField(name = "a")
    var a: Int = 0,
    @JSONField(name = "b")
    var b: List<Int> = listOf()
)

测试对象2

@Keep
class TestBean {
    @JSONField(name = "a")
    var a: Int = 0
    @JSONField(name = "b")
    var b: List<Int> = listOf()
}

测试数据

var testStr = "{a: 10}"
var j1 = JSON.parseObject(testStr, TestBean::class.java)

var testStr2 = "{a: 11, b: [1, 2, 3]}"
var j2 = JSON.parseObject(testStr2, TestBean::class.java)

var testStr3 = "{a: 12, b: []}"
var j3 = JSON.parseObject(testStr3, TestBean::class.java)

报错日志

     Caused by: java.lang.NullPointerException: Attempt to get length of null array
        at com.alibaba.fastjson.util.JavaBeanInfo.<init>(SourceFile:135)
        at com.alibaba.fastjson.util.JavaBeanInfo.build(SourceFile:897)
        at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.<init>(SourceFile:49)
        at com.alibaba.fastjson.parser.ParserConfig.createJavaBeanDeserializer(SourceFile:963)
        at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(SourceFile:805)
        at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(SourceFile:538)
        at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(SourceFile:679)
        at com.alibaba.fastjson.JSON.parseObject(SourceFile:383)
        at com.alibaba.fastjson.JSON.parseObject(SourceFile:287)
        at com.alibaba.fastjson.JSON.parseObject(SourceFile:560)

@Nstd
Copy link
Collaborator Author

Nstd commented Jun 28, 2020

@xurui1995 请问这个有考虑加入新版本吗?

@wuseal
Copy link
Owner

wuseal commented Jun 28, 2020

@Nstd 小伙子要不要考虑提交一个PR上来?加我微信 sealkingking 指导你开发

wuseal added a commit that referenced this issue Jul 2, 2020
add new feature: replace constructor parameters by member variables  fix #291
@wuseal wuseal added feature request L4 Difficulty level 4 (Maximum is 9) labels Feb 10, 2021
@wuseal wuseal added this to the 3.7.0 milestone Feb 10, 2021
@wuseal
Copy link
Owner

wuseal commented May 18, 2021

Released in 3.7.0

@wuseal wuseal closed this as completed May 18, 2021
@Nstd
Copy link
Collaborator Author

Nstd commented May 18, 2021

Released in 3.7.0

如果能更新下 ChangeLog 就更好了,ChangeLog 好像很久没更新了

@wuseal
Copy link
Owner

wuseal commented May 19, 2021

人懒了🤣,后面我想想搞个自动化change log吧😁

@Nstd
Copy link
Collaborator Author

Nstd commented May 21, 2021

哈哈哈,可以有

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request L4 Difficulty level 4 (Maximum is 9)
Projects
None yet
Development

No branches or pull requests

3 participants