How to cast a local map value to JsonObject

1,324 views
Skip to first unread message

mathias

unread,
Oct 14, 2015, 11:08:41 AM10/14/15
to vert.x

I have a valid Json string as a local map value:

String value = "{\"key\":2,\"value\":3}"

How to get the JsonObject back from this string/map?

First try:
LocalMap<String, String> map = ...
Object value = map.get(key);
JsonObject data = (JsonObject) value;
//--> java.lang.String cannot be cast to io.vertx.core.json.JsonObject

Second try:
LocalMap<String, String> map = ...
Object value = map.get(key);
JsonObject data = new JsonObject((String) value);
//--> io.vertx.core.json.JsonObject cannot be cast to java.lang.String

It seems to be harder than everything else I did today :-)
Any idea how to solve this?

Tim Fox

unread,
Oct 14, 2015, 11:13:26 AM10/14/15
to ve...@googlegroups.com
On 14/10/15 16:08, mathias wrote:

I have a valid Json string as a local map value:

String value = "{\"key\":2,\"value\":3}"

How to get the JsonObject back from this string/map?

You can convert any valid JSON string into a JsonObject with:

JsonObject jsonObject = new JsonObject(str);



First try:
LocalMap<String, String> map = ...
Object value = map.get(key);
JsonObject data = (JsonObject) value;
//--> java.lang.String cannot be cast to io.vertx.core.json.JsonObject

^ The value here is a string, you can't cast that to a JsonObject



Second try:
LocalMap<String, String> map = ...
Object value = map.get(key);
JsonObject data = new JsonObject((String) value);
//--> io.vertx.core.json.JsonObject cannot be cast to java.lang.String

^ The value here is a JsonObject, you can't cast that to a String

If you have some _actual code_, not just snippets (I hate snippets because they invariable hide the important parts where the problems are), then it will be much easier to diagnose.


It seems to be harder than everything else I did today :-)
Any idea how to solve this?
--
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 http://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/25ad0e54-e707-45db-be58-447b2c29dba4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mathias

unread,
Oct 14, 2015, 12:04:35 PM10/14/15
to vert.x
You are right. The issue was a bit outside of the code snippet :-(

The problem was that my map value was filled at different places of the app whereby not all places used a string:
Object value = someOtherJsonObject.getValue("value");
map
.put(key, value); // automatically puts a JsonObject as value^^

Everything works when every value put into the map is assured to be a string:
Object value = someOtherJsonObject.getValue("value");
map
.put(key, value.toString()); //uses encode() inside which is nice

Thanks!
Reply all
Reply to author
Forward
0 new messages