Jun 03

Another artcile by Michael Schwarz, check it out here:

Silverlight 1.1 comes with an built-in JSON serializer which can serialize common data types like string, numbers and arrays. It includes a object serializer, too, I think it is nearly the same as in ASP.NET AJAX or Ajax.NET Professional.

I modified my IsolatedStorage demo and added a new method called GetFiles(string pattern) which will return an array of string containing the file names in the storage. Because I already did the managed JavaScript (DLR) demo in on of my earlier posts I’m now using C# (I love Silverlight, I can choose my favorite language all the time).

namespace IsolatedStorage{   [Scriptable]   public class Storage   {       [Scriptable]       public string GetFiles(string pattern)       {           using (IsolatedStorageFile isf =                  IsolatedStorageFile.GetUserStoreForApplication())           {               JavaScriptSerializer jss = new JavaScriptSerializer();               return jss.Serialize(isf.GetFileNames(“*.*”));           }       }

       [Scriptable]       public bool WriteText(string filename, string s)       {           try           {               using (IsolatedStorageFile isf =                      IsolatedStorageFile.GetUserStoreForApplication())               {                   using (IsolatedStorageFileStream fsm =                          new IsolatedStorageFileStream(filename,                                           FileMode.OpenOrCreate, isf))                   {                       using (StreamWriter sw = new StreamWriter(fsm))                       {                           sw.Write(s);                       }                   }               }           }           catch (Exception)           {               return false;           }

           return true;       }

       [Scriptable]       public string ReadText(string filename)       {           try           {               using (IsolatedStorageFile isf =                      IsolatedStorageFile.GetUserStoreForApplication())               {                   using (IsolatedStorageFileStream fsm = new                          IsolatedStorageFileStream(filename,                                       FileMode.Open, isf))                   {                       using (StreamReader sr = new StreamReader(fsm))                       {                           return sr.ReadToEnd();                       }                   }               }           }           catch (Exception)           {               return null;           }       }   }}

Because Silverlight (or the Scriptable methods) don’t support more complex data types until now (I hope this will change in future releases) we have to return a JSON string that will be parsed on the client-side JavaScript later.

The JavaScriptSerializer is included in the namespace System.Windows.Browser.Serialization. You have to create a new instance of the JavaScriptSerializer class and call the Serialize method which you pass the .NET object you want to serialize. In our example it is a string array we get as result of the IsolatedStorageFile.GetFiles method.

On the client-side JavaScript code I use the eval statement, hm, is not the best way to parse JSON, but the easiest. Of course you can use the ASP.NET AJAX parser or the JavaScript parser at json.org:

var control = document.getElementById(“SilverlightControl”);var storage = control.Content.Storage;

// save any data to the storagealert(storage.WriteText(“test2.txt”, “Hello World!”));

// read data from the storagealert(storage.ReadText(“test2.txt”));

// get all filenames in storagevar files;eval(“files = “ + storage.GetFiles(“*.*”)); // eval parse JSONalert(“Files: “ + files.join(“,”));

You can download this example here. For those of you don’t have Visual Studio Orcas installed I have included the binary files, too. So, simple double click on the TestPage.html. If you run the example you will see three message boxes: the first will display true if writing was sucessful, the second one will display the content of the file specified in the argument. The last message box will display all filenames in the storage.

read original

Apr 16

Michael Schwarz has provided small example how to implement AJAXPro with jQuery and JSON support. Here are some valueable links and the actual code:

“I forgot to put the beta version online that will support jQuery and json.js from http://www.json.org. You can download the latest beta of the AjaxPro library at http://www.ajaxpro.info/download/jQueryAjaxPro.zip. The download currently includes only the .NET 2.0 library including a Visual Studio .NET 2005 Web Site project.

The C# AjaxMethod

I added a very simple AjaxMethod HelloWorld which will only return a string and get one argument. The reason is that I don’t have included the new JSON converters which will be ready in the next days.
[AjaxPro.AjaxMethod]
public static string HelloWorld(string s)
{
return “Hello ” + s + “!”;
}

view original

Mar 06

Joe Walker is talking about the safety of JSON. He has talked about CSRF in the past, and this time he delves into the Array/JSON hack.

I saw some discussion recently about using JSON for secured data, and I’m not sure that everyone understands the risks.

I believe that JSON is unsafe for anything but public data unless you are using unpredictable URLs.“

How safe is your JSON?
view original

Feb 28

In this article Bilal Haidar introduces JavaScript Object Notation (JSON) and provides an AJAX/JSON example to show the power of JSON when used to interchange data between a client browser and a server.

You have always used the Asynchronous JavaScript and XML (AJAX) to allow a web application to send asynchronous requests from the client browser to the server to retrieve some data and then let the client browser running your application receive a server response again in an asynchronous fashion. Data interchange usually was done either in the form of simple human readable strings or in an XML format.
view original