Reputation: 35
I have invoke on JObject:
jObject.values
So i get Map[String, Any]. I modify this map and getting new instance of Map[String, Any]. And now i wont achieve something reverse to 'values' method: I wont convert my Map[String, Any] to JObject (or json string)
Upvotes: 1
Views: 1474
Reputation: 103837
It's somewhat tricky, because by calling .values
you lose the type information associated with the fields (hence why the value type of the map is Any
).
In order to get a JObject
, you need to pass a List[JField]
into the constructor. So you'll need to convert each map entry ((String, Any)
) into a JField
. Mapping the name is trivial; the challenge is converting an Any
into a JValue
.
You could probably do something with pattern-matching or type-checking (e.g. if the value is an Int
, cast it to int and wrap in a JInt
), but as with all incomplete pattern matching this is going to be fragile - what do you do with non-primitives to wrap them back up the way they came out?
So I suggest that in general, the best way to do this is not to try to do this from the output of .values
but pass the object around itself. Once you've lost the type information, it's hard to get back definitively, and almost any solution will necessarily involve fragile guesswork.
Upvotes: 2