question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return `null` from `Converter`

See original GitHub issue

Currently, 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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cbeustcommented, Oct 17, 2018

I see. I guess it would make sense for toJson() to be nullable then, I’ll make the change.

0reactions
FWDekkercommented, Oct 17, 2018

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:

@Target(AnnotationTarget.FIELD)
annotation class PhoneAnnotation

class PhoneConverter : Converter {
    override fun canConvert(cls: Class<*>) = cls == Phone::class.java

    override fun fromJson(jv: JsonValue) = Phone(jv.string!!)

    override fun toJson(value: Any) = "\"${(value as Phone).type}\""
}


fun main(args: Array<String>) {
    val persons = Klaxon()
        .fieldConverter(PhoneAnnotation::class, PhoneConverter())
        .parseArray<Person>(File("persons.json").inputStream())
}

If I change the canConvert function to always return false, nothing changes when I execute the main function.

My goal is to set the phone field of the Person object to null if the JSON’s phone property is an empty string, but because the fromJson function cannot return null it sets the phone field to Phone("") instead.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found