Fastjson: Pojo conversion plugin

Created on 11 Apr 2017  ·  14Comments  ·  Source: alibaba/fastjson

Hello, i added to this plugin fastjson library support. Can you add this information to description, if you want. I think, that it can helpfull to some one ;)

And can you give me some information about fastjson kotlin support if it exists - i cant find it.

Thanks.

Most helpful comment

https://github.com/alibaba/fastjson/releases/tag/1.1.62.android
https://github.com/alibaba/fastjson/releases/tag/1.2.37

@robohorse
1.2.37 & 1.1.62.android released. in the new version, nullable data class is supported.

All 14 comments

Thank you for your support, it's a good job. fastjson can provide built-in kotlin support in the future, but I have not learned koltlin.

Ok, thanks. I will wait for Kotlin support.

kotlin now officially supported by google so it would be nice if you support it

Great! I will do it soon

https://github.com/alibaba/fastjson/releases/tag/1.2.36
https://github.com/alibaba/fastjson/releases/tag/1.1.61.android
fastjson 1.1.61.android & 1.2.36 built-in support kotlin.

@amorenew @robohorse

I use following version

compile group: 'com.alibaba', name: 'fastjson', version: '1.2.36'

I have simple Json string:

{"value":1, "text":"text"}

and 2 POJO objects
1) java

public class ResponseJava{

    @JSONField(name="text")
    private String text;

    @JSONField(name="value")
    private int value;

    public void setText(String text){
        this.text = text;
    }

    public String getText(){
        return text;
    }

    public void setValue(int value){
        this.value = value;
    }

    public int getValue(){
        return value;
    }

    @Override
    public String toString(){
        return 
            "ResponseJava{" + 
            "text = '" + text + '\'' + 
            ",value = '" + value + '\'' + 
            "}";
        }
}
  1. kotlin
data class ResponseKotlin(

    @field:JSONField(name="text")
    val text: String? = null,

    @field:JSONField(name="value")
    val value: Int? = null
)

and finally i try to parse it.
That works fine:

 ResponseJava response = JSON.parseObject(TEXT, ResponseJava.class);

that has empty fields:

 ResponseKotlin response = JSON.parseObject(TEXT, ResponseKotlin.class);

Where is my mistake?

@amorenew @wenshao

@robohorse it's a bug for nullable data class, the problem has been fixed on the branch andrioid ( https://github.com/alibaba/fastjson/tree/android ) and the branch master (https://github.com/alibaba/fastjson/tree/master ).

i have very little experience with kotlin, i need your help for test.

@robohorse
val is same as the final could you try var and tell us if it is working?
https://stackoverflow.com/questions/44200075/val-and-var-in-kotlin

yes i will see just a little later

test code:

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.annotation.JSONField

fun main(args: Array<String>) {

    println("test for DataClass-------")
    val dt = DataClass(1,2)

    val json = JSON.toJSONString(dt)
    println(json)

    val clz = DataClass::class
    println(clz.javaObjectType)

    val dt1 = JSON.parseObject(json,clz.javaObjectType)
    println(dt1)

    println("test for DataClassSimple-------")
    val dts = DataClassSimple(1,2)
    val jsons = JSON.toJSONString(dts)
    println(jsons)

    val clzs = DataClassSimple::class
    println(clzs.javaObjectType)

    val dt2 = JSON.parseObject(jsons,clzs.javaObjectType)
    println(dt2)


    println("test for DataClassSimple-------")
    val dtf = DataClassField(1,2)
    val jsonf = JSON.toJSONString(dtf)
    println(jsonf)

    val clzf = DataClassField::class
    println(clzf.javaObjectType)

    val dt3 = JSON.parseObject(jsonf,clzf.javaObjectType)
    println(dt3)

}

data class DataClass(@JSONField(name="aa")val a : Int, @JSONField(name="bb")val b : Int)

data class DataClassField(@field:JSONField(name="aaa")val a : Int, @field:JSONField(name="bbb")val b : Int)

data class DataClassSimple(val a : Int, val b : Int)

output:

test for DataClass-------
{"aa":1,"bb":2}
class DataClass
DataClass(a=1, b=2)
test for DataClassSimple-------
{"a":1,"b":2}
class DataClassSimple
DataClassSimple(a=1, b=2)
test for DataClassSimple-------
{"aaa":1,"bbb":2}
class DataClassField
DataClassField(a=1, b=2)

all above look like well.

thanks, i will try to do it at this weekend

https://github.com/alibaba/fastjson/releases/tag/1.1.62.android
https://github.com/alibaba/fastjson/releases/tag/1.2.37

@robohorse
1.2.37 & 1.1.62.android released. in the new version, nullable data class is supported.

Ok, thanks! After all test will release new version)

Sorry for delay. FastJson Kotlin support available in 1.8.5 version - released right now

Was this page helpful?
0 / 5 - 0 ratings