CakePHP 1.2 and JSON

Posted in: JavaScript,PHP,jQuery |

While working on BartenderDB.com I decided to try to make the ‘data entry’ as ‘fluid’ as possible, meaning … lots and lots and LOTS of AJAX.  Using jQuery of course.

While looking for some nice Cake 1.2 JSON information, I ran across an interesting post on Pagebakers called Using JSON in CakePHP 1.2.  I gave it a quick read (actually, I skimmed it for code snippets and just rolled my own from the ‘general idea’) and it helped out tremendously.  I was up and running with JSON in under 5 minutes.  Great resource, and kudo’s to the poster.

After setting up my JSON Views, I realized that Cake was still in ‘debug’ mode — so I modified my views/layouts/json/default.ctp and simply added a Configure::write(‘debug’,0); to it … and viola, problem solved, all my JSON requests were free of Cake’s added dev/staging information.

JsonViewEngine for ASP.NET MVC Framework

Posted in: C# |

I am currently tasked with a project at work that, after building out the design documents, was going to rely on ASP.NET MVC and at the same time take advantage of the URL Rewriting that comes with it 'out of the box' for 'data source urls' for AJAX requests. We decided to use the jQuery library, and the $.getJSON() function within it to request for JSON data.

Well, if you've played with ASP.NET MVC, you've probably noticed that it doesn't have any 'quick and simple' "json serialization in a view" ... or, does it?

I recently saw Scott Guthrie at a local .NET User Group event, and he mentioned that the ASP.NET MVC Framework was completely "pluggable", and you could easily replace the built-in View Engine. So, I immediately searched for examples, found one ... tossed it out and whipped together a really quick and simple 'JsonViewEngine' class.

Here's the code:


C#:
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.IO;
  6. using System.Security.Permissions;
  7. using System.Web;
  8. using System.Web.Script.Serialization;
  9. using System.Web.UI;
  10. using System.Web.Mvc;
  11.  
  12.  
  13. namespace Sim.Microsite.Framework.Web.Mvc.JsonViewEngine
  14. {
  15.     [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  16.     [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  17.     public class JsonViewEngine: IViewEngine
  18.     {
  19.         #region IViewEngine Members
  20.         public void RenderView(ViewContext viewContext)
  21.         {
  22.             if (viewContext == null)
  23.             {
  24.                 throw new ArgumentNullException("viewContext");
  25.             }
  26.            
  27.             viewContext.HttpContext.Response.Clear();
  28.             viewContext.HttpContext.Response.ContentType = "application/json";
  29.  
  30.             System.Text.StringBuilder jsonText = new System.Text.StringBuilder();
  31.             JsonFx.Json.JsonWriter jsonWriter = new JsonFx.Json.JsonWriter(jsonText);
  32.             jsonWriter.Write(viewContext.ViewData);
  33.  
  34.             viewContext.HttpContext.Response.Write( jsonText.ToString() );
  35.         }
  36.         #endregion
  37.     }
  38. }

As you may or may not be able to tell, I am using the JsonFx serializer for this project, but you could easily replace that with the built-in System.Web.Script.Serialization.JavaScriptSerializer or any other library of your choice.

After building this, I also decided to take it and create an XmlViewEngine that simply returns XML data, as we have some use for that as well with this project ... I'll leave you to implement that on your own, it's basically the same code ... just uses XmlSerializer instead -- however, I wrote some custom code for serializing the IDictionary that the default ViewPage uses for ViewData (even though ViewData is defined as an object in the viewContext ... ViewPage defines it as generic dictionary).

So ... have fun with that.

LinkShare  Referral  Prg - default banner