https://kotlinlang.org logo
Title
h

Hexa

07/17/2018, 6:14 PM
I got a code that looks like this:
import com.fasterxml.jackson.databind.ObjectMapper

fun main ( args : Array < String > ) {

    val mapper = ObjectMapper()
    val jsonResult = mapper.writerWithDefaultPrettyPrinter()
            .writeValueAsString(someString)

    System.out.println("blank: ${jsonResult.isBlank()}")
    System.out.println("isEmpty: ${jsonResult.isEmpty()}")
    System.out.println("{}: ${jsonResult.contentEquals("{}")}")



}
jsonResult
returns
{}
in my logs which means its a an empty json. How do I check if jsonResult is equals to
{}
(or empty). All of my println returns
false
, so
isEmpty
and
isBlank
dont work.
n

Nikky

07/17/2018, 6:52 PM
kt
val someString = "{}"
    val mapper = ObjectMapper()
    val jsonResult = mapper.readTree(someString)
    println(jsonResult.nodeType)
    if(jsonResult is ObjectNode) {
        println(jsonResult.size())
    }
jsonResult.size() == 0
i assume thats what you want ? i am pretty sure you could also tell it to deserialize it as map somehow
h

Hexa

07/17/2018, 8:50 PM
That works, thanks @Nikky