2

I'm trying to parse a grails parameter map to a Json String, and then back to a parameter map. (For saving html form entries with constraint-violations)

Everything is fine as long as there is no hasMany relationship in the parameter-map.

I'm using

fc.parameter = params as JSON

to save the params as JSON String.

Later I'm trying to rebuild the parameter map and create a new Domain-Object with it:

new Foo(JSON.parse(fc.parameter))

Everything is fine using only 1:1 relationships (states).

[states:2, listSize:50, name:TestFilter]

But when I try to rebuild a params-map with multi-select values (states)

[states:[1,2], listSize:50, name:TestFilter]

I'm getting this IllegalStateException:

Failed to convert property value of type org.codehaus.groovy.grails.web.json.JSONArray to required type java.util.Set for property states; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [de.gotosec.approve.State] for property states[0]: no matching editors or conversion strategy found

I tried to use this, but without success:

JSON.use("deep") {
   new Foo(JSON.parse(fc.parameter))
}

1 Answer 1

0

You can use JsonSlurper instead of the converters.JSON of grails, it maps JSON objects to Groovy Maps. I think this link also might help you.

Edit: Now, if the problem is binding the params map to your domain, you should try using bindData() method, like:

bindData(foo, params)

Note that this straightforward use is only if you're calling bindData inside a controller.

What seems to be happening in your case is that Grails is trying to bind a concrete type of List (ArrayList in the case of JsonSlurper and JSONArray in the case of converters.JSON) into a Set of properties (which is the default data structure for one-to-many associations). I would have to take a look at your code to confirm that. But, as you did substitute states: [1,2] for a method of your app, try another test to confirm this hypothesis. Change:

states:[1,2]

for

states:[1,2] as Set

If this is really the problem and not even bindData() works, take a look at this for a harder way to make it work using object marshalling and converters.JSON. I don't know if it's practical for you to use it in your project, but it sure works nicely ;)

Sign up to request clarification or add additional context in comments.

6 Comments

Hi, using JsonSlurper, leadst to this exception: Failed to convert property value of type java.util.ArrayList to required type java.util.Set for property states; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [de.gotosec.approve.State] for property states[0]: no matching editors or conversion strategy found --- why is the domain class required and not the id?
If you call the parse method from outside the object's constructor, you still get the same error? Or is it a object binding error?
its an object binding error. The parsing makes no problem. it returns a Hashmap where states is an ArrayList and 1 and 2 Strings.
If i replace the 'states:[1,2]' entry in the hashmap with State.getAll(map.get('states') as List<BigInteger>) everything is fine, but why do i have to do it. In the scaffolded save action the Domain Object is builded with the hash-map structure like 'states:[1,2]', why does it fail?
Neither bindData nor the Set casting works, casting to Set leads to the Exception: Failed to convert property value of type java.util.HashSet to required type java.util.Set for property states; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.Integer] to required type [de.gotosec.approve.State] for property states[0]: no matching editors or conversion strategy found
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.