Verxt3: io.vertx.core.json.impl.Json and JsonSlurper

476 views
Skip to first unread message

Jorge Riquelme

unread,
Nov 18, 2014, 3:19:39 PM11/18/14
to ve...@googlegroups.com
Hi,

I'm having problems related to parsing of Json. I'm working with
groovy, using JsonSlurper to parse Json, but I'm getting the following
exception:

java.lang.IllegalStateException: Illegal type in JsonObject: class
java.math.BigDecimal
at io.vertx.core.json.impl.Json.checkAndCopy(Json.java:94)
at io.vertx.core.json.JsonObject.copy(JsonObject.java:341)
at io.vertx.core.json.impl.Json.checkAndCopy(Json.java:81)
...

Looking at the source code, it seems that BigDecimal isn't being
processed, in Json.java, line 63, mehod checkAndCopy:

...
} else if (val instanceof Number && !(val instanceof BigDecimal)) {
...

The funny thing is that the number in cuestion isn't a "big" decimal,
but JsonSlurper always uses BigDecimal, check
http://groovy.329449.n5.nabble.com/What-kind-of-numbers-for-JSON-parsing-td5004287.html

Related to this, I tried to work with JsonObject instead of
JsonSlurper, using the method getMap (in the same way of
wrapObject/unwrapObject in lang.groovy.InternalHelper of
verxt-groovy), but the "inner" objects of the resultant map aren't
maps, but instances of JsonObject, so an expressión like:

jsonObj.someList[0].aProperty

has to be transformed to:

jsonObj.someList[0].getString('aProperty') or
jsonObj.someList[0].getMap().aProperty

This doesn't work for me, because it ties JsonObject to other parts of
my system and breaks my unit tests :p

Ah!, btw, amazing project, We have a couple of systems up and running
using vertx2 and vertx3 looks promising. Any help/advice would be
appreciated.


best regards, Jorge Riquelme

Abhishek Pande

unread,
Mar 7, 2016, 11:56:19 PM3/7/16
to vert.x, jo...@larix.cl
Hi Jorge,

We are facing the same issue. Can you tell us what is the solution that you found?

Thanks & Regards,
Abhishek

foxgem

unread,
Mar 8, 2016, 2:27:57 AM3/8/16
to ve...@googlegroups.com, jo...@larix.cl
Hi

I’m using groovy with Vertx, at the same time I’m using JsonSlurper in my vertx project.

If precision is not too important, you can convert BigDecimal into Float or something else before you fed it to JsonSlurper. Here are two methods from my Utils you can refer to

    static Map castDecimalValueToFloat(Map data) {
        if (!data) {
            return data
        }

        data.entrySet().each { entry ->
            if (entry.value instanceof BigDecimal) {
                entry.value = entry.value as Float
            } else if (entry.value instanceof List) {
                entry.value = castDecimalValueToFloat(entry.value)
            } else if (entry.value instanceof Map) {
                entry.value = castDecimalValueToFloat(entry.value)
            }
        }

        data
    }

    static List castDecimalValueToFloat(List rows) {
        if (!rows) {
            return rows
        }

        rows.collect { data ->
            if (data instanceof Map) {
                castDecimalValueToFloat(data)
            } else if (data instanceof BigDecimal) {
                data as float
            } else {
                data
            }
        }
    }

And here is my JsonUtils:

@CompileStatic
class JsonUtils {

    private static Logger log = LoggerFactory.getLogger(JsonUtils.class)

    static String toJsonString(def obj) {
        String jsonString = ''
        try {
            jsonString = new JsonBuilder(obj).toString()
        } catch (e) {
            log.error("Data Could not be jsonifized: ${obj}", e)
        }

        jsonString
    }

    static def toJsonObject(String text) {
        if (text) {
            new JsonSlurper().parseText(text)
        } else {
            log.error("${text} could not be parsed to JSON.")
            null
        }
    }
}


-- 
foxgem
已使用 Sparrow

已使用 Sparrow
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/bce9853e-d925-4901-b128-56b99c85c51f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages