handling JSON via play framework 1.2.3 - json

I am sending a JSON message via XHR in a post request (content type: application/json; charset=UTF-8 - I am using firefox/chrome).
However, I am unable to process/receive the JSON message in the play 1,.2.3 controller - probably due to pilot error. Any insights/working example would be appreciated - thanks
JSON message (validates successfully in JSONLint):
{"email":"admin1#test.com","password":"admin123"}
Should I be looking at TypeBinder or create the object from the request parameters?

You can use gson that is embedded in play to parse the param. In your controller method
MyParam myParam = new GsonBuilder().create().fromJson(request.params.get("myParam"), MyParam.class);

You could also try using FLEXJSON (http://flexjson.sourceforge.net/) in order to avoid running into any circular loop errors, especially when

I ended up iterating through request.params.allSimple in order to get the value via request.params.get. I used the JsonParser().parse on this value to get the JSONElement. I then used getAsJsonObject() on the JSONElement to get the JSONBody. I retrieved the needed JSONElements through a get call on the JSONBody & then a getAs method call to get to the actual value. I will investigate TypeBinders in more detail - any other suggestions would be most welcome.

Related

NodeJS Joi Vlidation - How to return JSON response instead of a string?

Recently, Iv'e been using the Joi validation library in-order to validate the data which comes in from a request (building a RESTful API).
I was using the .label() method to generate a string response and send it back to the user, but I couldn't find any way to send a JSON response back to the user?
Tried sending a premade JSON inside the string, searching the documentation and the internet of course - couldn't find any mention of it.
Current code example:
textField: Joi.string().required().max(4).label("This example field didn't pass the testing phase, please try again)"),
Any ideas?
If you need to send data in case of error,try .error() method. you can pass error in it.

Angular and JSON, having trouble parsing for ng-repeat

I'm horribly sorry if there is a post for this, I tried to search but didn't find a answer.
Problem:
I'm calling a web service and receiving not so well formed JSON data from a Dynamics Nav service:
JSON:
"[{\"type\":\"2\",\"number\":\"VHT3866\",\"location\":\"Delta\",\"destinationNo\":\"\",\"contactName\":\"Jesus\",\"shipToName\":\"Lord jesus\",\"highPriority\":\"false\",\"hasComment\":\"true\",\"assignedTo\":\"\",\"source\":\"\"},{\"type\":\"2\",\"number\":\"VHT3866\",\"location\":\"Delta\",\"destinationNo\":\"\",\"contactName\":\"Jesus\",\"shipToName\":\"Lord jesus\",\"highPriority\":\"false\",\"hasComment\":\"true\",\"assignedTo\":\"\",\"source\":\"\"}]"
I then take this JSON and use angular.fromJson(json) to get it properly.
It doesn't seem to change into an array of javascript objects, but just plain text.
However if I take the same JSON and just put it manually in like this:
var json = angular.fromJson(stringfromserver);
It turns into a proper javascript object and ng-repeat throws no error.
I found an answer on Quora:
--- Le Batoure,
Angular from json is now strict so assuming that this string is from a trusted source you would have to use "eval()" plus surround the call in parenthesis for it to work
var hatsData = angular.fromJson(eval("(" + hats + ")"))
If you bring your JSON from http request for example you don't need to use the fromJson method.
The JSON is automatically parsed by Angular and you can use it directly.

How can I enable GZIP compression of the JSON response entity on Reslet?

I have a Restlet application already working that accepts JSON and returns JSON entity as response.
I'm trying to understand how I can compress the JSON entity that is returned in the response.
I did not find any clear example on how to achieve it.
I think I have to put somewhere on the router chain the Encoder/EncoderService classes, but I really don't understand where and how to use them.
Could anybody help me?
After some testing, I got the answer.
Creating a new filter like this
Filter encoder = new Encoder(getContext(), false, true, new EncoderService(true));
inside the createInboundRoot() method of my own Application class did the trick, the client requests were already containing the gzip header needed.

Google dictionary api does not return pure json?

I have the following code:
var json = new WebClient().DownloadString(string.Format(#"http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q={0}&sl=en&tl=en", "bar"));
This returns something like this:
dict_api.callbacks.id100({"query":"bar","sourceLanguage":"en","targetLanguage":"en","primaries":[{"type":"headword","terms":[{"type":"text","text":"bar",....
Why is it returning a function rather than just the json? Am I using web client incorrectly?
As I understand it, this is JSONP - JSON which is "padded" with a function call to allow cross-domain data transfer. I strongly suspect that if you pass in a different callback name on the URL, you'll see that other name come back in the response.
(Note that although I work for Google, this answer is not an "official" response from Google in any way, shape or form.)
You may want to use check this out :
json_decode for Google Dictionary API
They actually modify the resultant jsonp to get a json

How, in Sinatra, to pass a Request Body to other classes?

I'm using a Sinatra app to receive server requests and I want to dissect them in a separate class I call "request", but when I pass the request object the body gets dropped. Trying to read the request.body in the main class works but trying to read it in the new class generates a JSONparser octet error.
In the main Sinatra file, this test call generates the correct response:
puts JSON.parse request.body.read
after, I pass the request to the Request Class with the code below.
req=Request.new(request)
But in the Request class initialization def, the same "puts" code above generates the error:
JSON::ParserError - A JSON text must at least contain two octets!:
Both files include the JSON requirement.
A work around is fairly simple but I would prefer the more elegant solution if I could figure out why it is not working as I expect. Any thoughts are appreciated.
from my tests
the Request.new constructor doesn't seem to clone from Request object
request.clone works proper
you need to do the thorough object inspection if you need anything extreme