-
Notifications
You must be signed in to change notification settings - Fork 176
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
fields with null value are given the non nullable type Any #23
Comments
yeah,Good job! Thanks for your advise, It should be that as you discribed ,It will be fixed next version soon! |
Happy to be of help to such a useful tool |
@saied89 Hi,After thinking about this issue again,I found that if JSON always not sure whether it be null or not ,We'd better to make all the field to to nullable, and the now plugin has support this now by selecting the For your condition ,We may have a better solution:
fun filterNullJsonField(jsonString:String):String {
val moshi=Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
if (jsonString.startsWith("{")) {
val adapter = moshi.adapter(Map::class.java)
val tempMap = adapter.fromJson(json)
return adapter.toJson(tempMap)
} else {
val adapter = moshi.adapter(List::class.java)
val tempList = adapter.fromJson(json)
return adapter.toJson(tempList)
}
}
from {
"_id": null,
"name": "test",
"type": "test"
} to {"name":"test","type":"test"}
User(id=java.lang.Object@3b0090a4, name=test, type=test) Hope useful for you ,If any problem welcome to reply me directly. |
I don't like default values when parsing JSON. I'd rather it fail and throw an exception than keep working with wrong value. I also don't know what "lang.Object@3b0090a4" means |
@saied89 Glad to receive your response, Yeah, you could choose no default value. {
"_id": null,
"name": "test",
"type": "test"
} And also some times may like this: {
"_id": null,
"name": null,
"type": "test"
} Or this: {
"_id": null,
"name": null,
"type": null
} when in the third condition ,Did you want to make the generate model to be like as this ?: data class User(
@Json(name = "_id") val id: Any?,
@Json(name = "name") val name: Any?,
@Json(name = "type") val type: Any?
)
|
No. but imagine you're given a postman for a project and part of the json that is returned there is the third case. But you don't need User class right now. You just need it to parse so that you can use other parts. You will fill out the right types further in development when non null values are returned(case 1 and 2 in your example) and you actually need the User class. It is a common scenario for me at least. |
@saied89 Yes ,we can fill out the right types further in development when non null values are returned. but tell me ,what the maybe model declaration looks like when the return data is the third case, If probably could you make some cases with code,Then I can catch your mind more accurate |
I think we're over complicating the matter a bit :) |
@saied89 Hi,Friends,you are right ,what we want is that the model could be used to parse the json. {
"_id": null,
"name": null,
"type": null
} we only could declare a nullable type that could used to parse the json ,And we could declared it like this : data class User(
@Json(name = "_id") val id: Any?,
@Json(name = "name") val name: Any?,
@Json(name = "type") val type: Any?
) then we can parse the json . |
Does it ever make sense to declare such a json non-nullable? Would a user ever want that? |
@saied89 Ok ,I think I got it ,the json you parsed with contains null value meaning the field would be nullable some times, and others with real value meaning that they are non-nullable every time , Is that right? If so , An option |
yeah that would be great! thanks |
@saied89 Hi ,I just released version 2.0-beta, Which has complete the advice you raised ,If you have time ,you could have a test if it works for you. 😄 Just download the jar in the release url and install local ,I think you know how to download and install local the plugin .thanks for your awesome advice again ! |
I humbly suggest that you move the option to a check box(its not really a third type) which is only enabled if type non-nullable is selected. It might be called "Assign nullable type to fields with value null" |
@saied89 First, If now the version reach your needs? |
Yeah it's working great 😄 thanks! |
Close this issue if any other problem, welcome to raise a new one. |
Jsons that contain fields with null value could not be parsed with the generated model due to the fields being assigned the non nullable type Any.
I think Any? is more appropriate.
example:
json:
{
"_id": null,
"name": "test",
"type": "test"
}
is parsed to
data class test(
@JSON(name = "_id") val id: Any, //null
@JSON(name = "name") val name: String, //test
@JSON(name = "type") val type: String //test
)
The text was updated successfully, but these errors were encountered: