Return `null` from `Converter`
See original GitHub issueCurrently, the Converter
’s fromJson
method must return Any
, which precludes returning null
values. However, I think there are legitimate situations in which fromJson
should return null
, in particular when the converter is used as a field converter.
For example, consider the following JSON:
[
{
"name": "Felix",
"phone": "Nokia"
},
{
"name": "Jake",
"phone": ""
}
]
and the following classes:
data class Person(
val name: String,
@PhoneAnnotation
val phone: Phone?
)
data class Phone(val type: String)
In this case, it is not possible to use a field converter for the phone
field. A workaround would be to use a converter for the entire Person
class, but if Person
is a class with, say, 20 fields, this would result in a lot of unnecessary code.
Am I perhaps overseeing another solution to this problem?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How to convert a method which return null to "null-safety."
In my old project, I used a method to perform get operation in dart, where I used to return null if the output...
Read more >WPF Getbinding returns null when converter accessed from style
WPF Getbinding returns null when converter accessed from style RRS feed ... First statement uses the converter in style whereas second xaml ...
Read more >StringValueConverter: fromString returns null - SAP Community
In java, Strings are Objects. So fromString(String str) for this StringValueConverter won't make much sense. However, you can implement your own converter class ......
Read more >Java Utililty Methods Null Value Convert - Java2s.com
convert Null try { String strvalue = String.valueOf(o); if (strvalue.equals(null) || strvalue.equals("null") || strvalue.length() == 0) { return ""; } else ...
Read more >Property Value Converter always returning null on v8.1?
Property Value Converter always returning null on v8.1? Questions, Bugs, feedback, etc. Umbraco 8. Hi,. Sorry if I'm asking ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I see. I guess it would make sense for
toJson()
to be nullable then, I’ll make the change.I need an
@JetBrains.com
email address to enter the Slack, which I don’t have. I tried signing up here, but I didn’t receive an invite yet.About my usage of the converter, I used the example from the README’s “Field conversion overriding” section. In addition to the code I gave in the issue description, I have the following:
If I change the
canConvert
function to always return false, nothing changes when I execute themain
function.My goal is to set the
phone
field of thePerson
object tonull
if the JSON’sphone
property is an empty string, but because thefromJson
function cannot returnnull
it sets thephone
field toPhone("")
instead.