JSONWebservice with JSONOBJECT / JSON ARRAY as argument?

Hal Seldon, modified 12 Years ago. Junior Member Posts: 34 Join Date: 9/20/13 Recent Posts
Is there any way to implement a JSON WEBSERVICE that I can pass a JSONOBJECT or JSONARRAY as argument?

I read in Developer's guide :
In addition to the common types, arguments can be of type List or Map. To pass a List argument, send a JSON array. To pass a Map argument, send a JSON object. The conversion of these is done in two steps, ingeniously referred to below as Step 1 and Step 2:
Step 1--JSON deserialization: JSON arrays are converted into List<String> and JSON objects are converted to Map<String, String>. For security reasons, it is forbidden to instantiate any type within JSON deserialization.
Step 2--Generification: Each String element of the List and Map is converted to its target type (the argument's generic Java type specified in the method signature). This step is only executed if the Java argument type uses generics.


But if I implemented a simple remote method (inside EntityServiceImpl) with JSONOBJECT parameter :
public JSONObject testWebservice1(JSONObject object) {
	return JSONFactoryUtil.createJSONObject();
}

When I tested with this values:
{ "test" : "value"} => "[ ]: Could not find a no-arg constructor for com.liferay.portal.kernel.json.JSONObject"


And for JSONArray parameter (inside EntityServiceImpl):
public JSONObject testJSONArray(JSONArray list) {
		return JSONFactoryUtil.createJSONObject();
}


When I tested with this values:
["test1","test2","test3"] => "Unable to convert to type: com.liferay.portal.kernel.json.JSONArray"
[test1,test2,test3] => "Unable to convert to type: com.liferay.portal.kernel.json.JSONArray"
{[test1,test2,test3]} => Expected : after a key at charecter 21
{ "array" : [test1,test2,test3]}=> "[ ]: Could not find a no-arg constructor for com.liferay.portal.kernel.json.JSONArray"
thumbnail
Meera Prince, modified 12 Years ago. Liferay Legend Posts: 1111 Join Date: 2/8/11 Recent Posts
Hi

Have a look into following link it may help you and you can find example portlets too.


http://www.liferaysavvy.com/2014/02/liferay-plugin-portlet-json-web_2559.html


Regards,
Meera Prince
Hal Seldon, modified 12 Years ago. Junior Member Posts: 34 Join Date: 9/20/13 Recent Posts
I read your articles and made hundred test....no info for my question.
thumbnail
Meera Prince, modified 12 Years ago. Liferay Legend Posts: 1111 Join Date: 2/8/11 Recent Posts
Hi
need to have little bit patience when we explore the things this is my advice..

go through following link

https://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/json-web-services

In the above link please concentrate on JSON Web Services Invoker and Object parameters Parts.

One more in the above code JSONFactoryUtil.createJSONObject() it will create empty object of JSON object once you get this you need fill the valued in object other wise it will give empty object like "[ ]"

General creation of JSON Object as follows

public JSONObject testWebservice1() {
JOSNObject jsonObject=JSONFactoryUtil.createJSONObject();
jsonObject.put("user",",meera")
return jsonObject;
}

and one more thing could please tell me what exactly your web service call and why you want to send Object parameter specially json object or array

Regards,
Meera Prince
Hal Seldon, modified 12 Years ago. Junior Member Posts: 34 Join Date: 9/20/13 Recent Posts
Hi Meera

Sorry, maybe my post is confusing... My problem is not the returned empty JSONObect....

My question is about pass JSONObject or JSONArray as input parameter.....

A clearer example:

public String  testWebservice1(JSONObject jsonObject) {
  return "this is a test";
}

Public String testWebservice2(JSONArray jsonArray) {
  return "this is another test";
}


I don't want to return JSONObject or JSONArray... I want to pass them like input arguments...is this possible?

Thanks in advance
thumbnail
Meera Prince, modified 12 Years ago. Liferay Legend Posts: 1111 Join Date: 2/8/11 Recent Posts
Hi
Yes we have option to pass objects as request parameter that is why in the above links i specified the Object parameters part in the above link.

There you can see that is they are passing foo object

/jsonws/foo/get-bar/zap-id/10172/start/0/end/1/+foo

Read same thing and try it yourself meaning while i will try and post you ok..


Regards,
Meera Prince
Hal Seldon, modified 12 Years ago. Junior Member Posts: 34 Join Date: 9/20/13 Recent Posts
Thanks Meera

I read this doc a hundred times before post.... There is something that not works for me, or I'm not understanding howto.

With the sample that you suggest:

/jsonws/foo/get-bar/zap-id/10172/start/0/end/1/+foo

How can I pass foo object with data? If Foo is JSONObject or JSONArray, how can I fill it with data?

Data sample that I want to pass as object argument:

{"field1":"value1"} => for JSONObject

[{"field1":"value1"},{"field2":"value2"},{"field3":"value3"}] => for JSONArray

I read the doc, but I didn't found a clear sample to learn how can I pass complex objects (filled with data), and in my test case, for JSONObject or JSONArray type....
thumbnail
Meera Prince, modified 12 Years ago. Liferay Legend Posts: 1111 Join Date: 2/8/11 Recent Posts
HI
Ok i got it but here you need to understand one thing..

Passing object means we need to pass object type not the data ok ..

If you want pass json object it doesn't mean we are passing data for object.

try like this it may work

http://localhost:8080/[plugin-context]/api/jsonws//foo/testWebservice2/param1/[{"field1":"value1"},{"field2":"value2"},{"field3":"value3"}]

OR in Encoded format

http://localhost:8080/api/jsonws/foo/testWebservice2/param1/[%7B%22field1%22:%22value1%22%7D,%7B%22field2%22:%22value2%22%7D,%7B%22field3%22:%22value3%22%7D]

Public String testWebservice2(String param1) {
JSONArray array=JSONFactoryUtil.createJSONArray(param1)
return array;
}


Regards,
Meera Prince
Hal Seldon, modified 12 Years ago. Junior Member Posts: 34 Join Date: 9/20/13 Recent Posts
Ok Meera

I understand.... great info... but just one question more..

...what is the purpose to pass an object as input parameter like your proposed example, with no data initialized:
/jsonws/foo/get-bar/zap-id/10172/start/0/end/1/+foo

I think the webservice method will be something like....
public void  getBar(long zapId, long start, long end, Foo foo) {
   //some webservice code
}


foo will be a object with attributes...Why we pass empty? or how can I fill it?