#1 2019-06-27 08:53:29

Patryx
Member
Registered: 2019-03-25

Script - error converting object to JSON

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

#2 2019-07-02 09:16:46

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Script - error converting object to JSON

Hi Patryx,

does this work when you use Oracle Java 8 ?

Regards,
Eduardo

Offline

#3 2019-07-02 10:22:40

Patryx
Member
Registered: 2019-03-25

Re: Script - error converting object to JSON

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 smile) 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

#4 2019-07-02 11:50:54

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Script - error converting object to JSON

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

#5 2019-07-02 12:41:55

Patryx
Member
Registered: 2019-03-25

Re: Script - error converting object to JSON

In stackoverflow (link I pasted in first post) it's written that it works for Java 8.

Offline

#6 2019-07-02 13:17:21

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Script - error converting object to JSON

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

#7 2019-07-03 06:42:43

Patryx
Member
Registered: 2019-03-25

Re: Script - error converting object to JSON

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:
JSONObject.jpg

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 smile
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

Board footer

Powered by FluxBB