You are not logged in.
Hi,
I need a script which returns json. I have a problem if I want to work wirh json object and then convert it to json string.
For example:
class Person { String name }
def json = groovy.json.JsonOutput.toJson([ new Person(name: 'John'), new Person(name: 'Max') ]);
return json;
After executing I get an error:
Script execution failed.
error message: javax.script.ScriptException: java.lang.ClassCastException: class [B cannot be cast to class [C ([B and [C are in module java.base of loader 'bootstrap') (java.lang.ClassCastException)
script arguments:
file: getReportInfo.groovy (id: 263821, line 45)
line number: 45
line: def jsonp = JsonOutput.toJson([ new Person(name: 'John'), new Person(name: 'Max') ])
The same error when I use:
def groovy.json.JsonBuilder jsonBuilder = new groovy.json.JsonBuilder();
jsonBuilder.error {
code '404'
message 'ID not found'
}
//def String json = groovy.json.JsonOutput.prettyPrint(jsonBuilder.toString());
def String json = jsonBuilder.toString();
return json;
According to:
https://stackoverflow.com/questions/526 … in-java-11
https://bugs.openjdk.java.net/browse/JDK-8218540
if I understand well, it is a problem with incompatible GROOVY version to JAVA 11 (problem wth converting char[]/byte[]) and I need a newer groovy package.
How can I resolve the problem?
Last edited by Patryx (2019-06-27 08:54:19)
Offline
Hi Patryx,
does this work when you use Oracle Java 8 ?
Regards,
Eduardo
Offline
Hi Eduadro,
I don't know, but I suppose it will work, but we are not going to use Java 8, so it is not a solution for us.
I noticed that you used JSONObject package https://developer.android.com/reference … JSONObject (maybe you had such a problem too ) so I found out how to use this library as workaround to create json and I skipped recommended JsonOutput by groovy.
Last edited by Patryx (2019-07-02 10:28:38)
Offline
Hi Patryx,
ok, but this would help us to understand the problem. If exactly the same code works with Oracle Java 8 but not with Java 11 it would help us to limit / understand the problem.
Regards,
Eduardo
Offline
In stackoverflow (link I pasted in first post) it's written that it works for Java 8.
Offline
Hi Patryx,
ok, I think the code in stackoverflow is a minimal example so we will test with this.
You mentioned you found a workaround: could you post the complete code of the workaround? It would be the best for the code in stackoverflow, so we have both versions for the same code.
Regards,
Eduardo
Offline
Hi Eduardo,
My workaround isn't connected with groovy.json.* like JsonOutput or JsonBuilder, so I don't think you can use it in stackoverflow as working solution.
I just use org.json.JSONObject which jar I found in your libs after installation: "apache-tomcat\webapps\reportserver\WEB-INF\lib\json-20090211.jar". I see you used it here:
Unfortunately it's not the same as JsonOutput (more options, easier to work with json) but it helps me a bit to create string for json in easier way than manually
Example:
import org.json.*;
/* Class MyError */
public class MyError {
public int code
public String message
public JSONObject createJsonObject() {
JSONObject jo = new JSONObject();
jo.put("code", this.code ?: JSONObject.NULL);
jo.put("message", this.message ?: JSONObject.NULL);
JSONObject mainObj = new JSONObject();
mainObj.put("error", jo);
return mainObj;
}
}
String jsonString = new MyError(code: 403, message: "Cannot used JSONObject! :(").createJsonObject().toString();
Last edited by Patryx (2019-07-03 06:45:23)
Offline