CakePHP JSON extension? - json

I've created a simple REST service that serves data as XML. I've managed to enable XML, JS and RSS format but I can not find the way to enable JSON format. Is JS == JSON? Guess not :).
How can I enable this in version 1.2/1.3?
Thx!!

Router::parseExtensions('json');

If you have PHP 5.2 or higher, it ships with JSON encode/decode support. Check the docs here.
You'll probably need to do the encoding/output by hand, but it should be trivial to code.
Bonus points would be to build it as a behavior :)
Edit:
Check out the $javascript->object() method here, it may do what you want.

Quick google search indicates that there are is a json Component for CakePHP. Link to article discussing its use in Cake 1.2: http://www.pagebakers.nl/2007/06/05/using-json-in-cakephp-12/

just add this line of code in your controller or AppController
var $components = array('RequestHandler');
function beforeFilter() {
$this->RequestHandler->setContent('json', 'text/x-json');
}
and run it into internet explorer.

Related

Blazor WebAssembly JsonException: A possible object cycle was detected which is not supported

I get this error because I have circular references defined in my object model. My question is, is there any way to resolve this using one of the following two options?
Using Newtonsoft.Json and options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
Using System.Text.Json and options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve;
I'm not seeing a way to switch to Newtonsoft.Json in a Blazor WebAssembly application and I tried implementing option 2 in the ConfigureServices function of Startup.cs in my Server project but I still kept getting the error.
I'm just trying to find a solution that doesn't require me redefining my object model. The JsonIgnore attribute does not appear to be an option either because I assume, and it appears, that then any fields I define it on do not exist in the Json on the client which breaks my application.
Update: I found this site which looks to me like discusses exactly what I'm referring to here and how to implement the solution but I have not got it to work yet. If anyone is successfully using Blazor WebAssembly with circular references in your object model please let me know what you're doing.
https://github.com/dotnet/aspnetcore/issues/28286
Thank you for pointing out this error in Blazor. I found the answer in the issue you mentioned (this comment). You need to change json options also on the Client side. This works for me:
On server
services.AddControllersWithViews().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve;
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
On client
var response = await Http.GetFromJsonAsync<T>("{Address}", new JsonSerializerOptions
{
ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve,
PropertyNamingPolicy = null
});
To the two options you mentioned there is a third option available if you use .NET 6 or above.
Using System.Text.Json and options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
Beware that ignoring cycles have issues on its own (such as data corruption) but if you were depending on it when you were using Newtonsoft.Json then you will be fine as it is basically the same behavior.
If you prefer to go with ReferenceHandler.Preserve, please share more info on what error you are getting and I can try to help you out.
One way to go about this is specify how much depth an object is allowed to have. Please see the documentation here regarding how to do this with System.Text.Json. I think this may help.

SPRING-DATA-REST Backend with AngularJS frontend

Is there any frontend application sample that consumes RESTful services of Spring-data-rest backend which is written with angularJS.
My prefer for rest api for angularjs is RESTANGULAR module...
In the site you can see many examples that how they deal with Rest calls and really good documentation and good community as well...
In this example which is from spring example SPRING.IO they uses $http, but I should say that Restangular uses $http as well, so basically you can say that Restangular is extended version of $http...
and for the last you can look for $resource...
I will update my answer If I will find something new...
Here are some links to AngularJS apps consuming Spring RESTful services
https://github.com/spinner0815/spring-data-rest-angularjs
At the moment, I think that angular-hal is the best library to consume Spring Data Rest output and stay with its philosophy of discovering url through relations.
The home page: https://github.com/LuvDaSun/angular-hal
and some examples :
https://github.com/LuvDaSun/angular-hal/tree/master/demo (by the dev)
https://www.jiwhiz.com/blogs/Consume_RESTful_API_With_Angular_HAL
https://github.com/paulcwarren/gs-rolebased-ui-with-hypermedia
here has a nice lib spring-data-rest-js can help you.
It's a easy to use and lightweight javascript lib can run in both node.js and browser. After use this lib you can manage entity like this way:
let springRest = require('spring-data-rest-js');
let Student = springRest.entity.extend('students');
let student = new Student();
student.set('name', 'Tom');
//create entity
student.save().then(()=> {
//update entity
student.set('name', 'Physics');
retuen student.save();
}).then(()=> {
//delete entity
retuen student.delete();
}).catch(err=> {
done(err);
})
base on fetch API and Promise,inspired by Parse.
It can be work with lib like AngularJS React Vue...

When to encode as HTML in Grails

I often see Grails sample code where the programmer has called a method called encodeAsHTML(). I figure I should probably use this in my Grails applications (for security reasons, I assume?), but I was wondering when I should use this method. What objects/properties/etc. are candidates for the encodeAsHTML() method?
Thank you!
Use encodeAsHTML() (or encodeAsJavaScript, etc) for everything that you've got from user. For every string that could be modified by user (got from input form, from request parameter, from external API call, etc)
See also:
https://en.wikipedia.org/wiki/Cross-site_scripting
https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet
I am not sure when this was introduced to Grails, but if in Config.groovy you set grails.views.default.codec="html" then encodeAsHTML() is called whenever you use ${} in GSPs.
Source: http://alwaysthecritic.typepad.com/atc/2010/06/grails-gsp-html-escaping-confusion.html

getWindowHandle function doesn't exist for driver in Selenium

I need to implement switch from one window to another in IE. However, element driver doesn't support getWindowHandle function.
I assume it might be just configuration problem or settings, though I don't know how to fix it.
Please, any suggestions.
I'm working with c# - Visual Studio
You haven't said which language bindings you're using, but based on a comment you posted, it looks like you're using C#. The method names are slightly different for each language binding. From this answer:
The object, method, and property names in the .NET language bindings
do not exactly correspond to those in the Java bindings. One of the
principles of the project is that each language binding should "feel
natural" to those comfortable coding in that language.
So you have to do a little translation if you're trying to copy-paste Java code. In this case, you want the combination of the WindowHandles property (to look for the new window handle) and the CurrentWindowHandle property of the driver. You can find full API documentation for the .NET bindings at the project's Google code site.
I am going to make wild guess:
Try to initialize your driver like this:
WebDriver driver = new FirefoxDriver(); //assume you use firefox
The interface WebDriver supports that method. Do not forget to store the handle somewhere ;)
String myWindow = driver.getWindowHandle();
BTW that method should return you actual window If you need all windows you probably should use getWindowHandles() method
If this does not work, please provide more info:
what error exactly are you getting?
How do you initialize WebDriver?
What version of selenium are you using?|
What type of driver are you using?

Best Class for base64 encoding/decoding Action Script?

What would be a best Class for base64 encryption/decryption in Action Script?
Adobe has two utils for this - Base64Encoder & Base64Decoder. Both are located in the mx.utils package. Although, I had to track them down here - encoder & decoder.
The usage would be something like:
var bmd:BitmapData = myBitmap.bitmapData;
var ba:ByteArray = bmd.getPixels(new Rectangle(0,0,bmd.width,bmd.height));
var b64:Base64Encoder = new Base64Encoder();
b64.encodeBytes(ba);
trace(b64.toString());
Similarly, 'b64.encode' would encode a String rather than a ByteArray.
Both the encoder and decoder add their respective results to an internal buffer. So, you just have to use 'toString' to return the current buffer.
This one seems to have some legs/supporters: http://garry-lachman.com/2010/04/21/base64-encoding-class-in-actionscript-3/
At this link you will find a good Base64 class:
http://www.sociodox.com/base64.html
blooddy_crypto claims (according to it's benchmark) to have a faster base64 encoder/decoder than the mx.utils one.
Most of the packages that I have seen that include one as a support function use the one that is credited to Steve Webster. I do not know which package this started out in, but it appears in several libraries, including the as3crypto lib on Google Code.