Posts tagged ‘javascript’

Fireflies

I had an idea about writing a small code to animate fireflies. On Christmas break I was bored and I figured I had a few hours to play with it. I also wanted to use some tools I hadn’t used yet and figured that was a good time to do Javascript since I haven’t done much Javascript. I fired up the editor ( Sublime ) and used the browser to run / debug, Firefox and FireBug of course. There is a bit of a difference using this setup compared to writing Java code in Eclipse which I’m used to. First off you don’t get the compilation in Sublime and secondly you have to switch to another program to load and debug. Switching between the two was what annoyed me. Later I though, what if I can find a tool that is an editor and can Ftp to a server as well and runs within the browser. If on each save you can Ftp it to a sever that is serving the Javascript then I could do it all from the browser. I went looking around and fount Net2Ftp which is supposed to at least come close to the tool I’m wishing for. I will have to try it out the next time I’m coding in Javascript. The other thing that got me is that there is no good way of declaring your own classes, you have to work with prototypes and such, why that is beats me.

As you can see from the code, it’s not pretty.

If you want to see the Javascript in action click here.

fireflies

Json in C#

I did some Json work in C#, I hadn’t done any Json with C# before.  Naturally I go googling without much luck at first.  When you look for Json libraries and code snippets they are plentiful, but most of them java script browser stuff.  Then a friend pointed out Jayrock which was exactly what I needed.  It can be used in C# code to address Json feed similar to Xml feeds.

As mentioned before json is extremely java script friendly.  Let’s take a look at one of the Google examples.

<h3>Upcoming Google Developer Events</h3>
<div id=“agenda”></div>
<script>
  function listEvents(root) {
    var feed = root.feed;
    var entries = feed.entry || [];
    var html = [‘<ul>’];

    for (var i = 0; i < entries.length; ++i) {
      var entry = entries[i];
      var title = entry.title.$t;
      var start = (entry[‘gd$when’]) ? entry[‘gd$when’][0].startTime : “”;     
      html.push(‘<li>’, start, ‘ – ‘, title, ‘</li>’);
    }
    html.push(‘</ul>’);
    document.getElementById(“agenda”).innerHTML = html.join(“”);
  }
</script>

<script src=“http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json-in-script&callback=listEvents”>
</script>

Run the sample.

And then in C# using Jayrock

using Jayrock.Json;

WebClient client = new WebClient();
Uri url = new Uri(“http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json”);
string gevents= client.DownloadString(url);

using (JsonTextReader reader = new JsonTextReader(new StringReader(gevents)))
{

    while (reader.Read())
    {
        if (reader.Depth == 4 &&
            reader.TokenClass == JsonTokenClass.Member &&
            reader.Text != null )
        {
            if (reader.Text.Equals(“gd$when”))
            {
                // navigate to the right node
                for( int i = 0; i < 6; i++)
                    reader.Read();
               
                // startTime
                Console.WriteLine(reader.ReadString());
            }
            else if (reader.Text.Equals(“title”))
            {
                // navigate to the right node
                for (int i = 0; i < 5; i++)
                    reader.Read();

                // $t
                Console.WriteLine(reader.ReadString());
            }
        }
    }
}

Those for loops are a bit ugly to get down the tree to the correct node. There has to be a better way of doing that. With simple data pairs it’s no problem but with heavier feeds like the one I took in the sample it’s not as obvious. If you have a simpler or more elegant way of doing it in C# drop a comment.

This is what the Json feed looks like.

Here are some tools, that I came up on JsonViewer which is hosted on CodePlex. The viewer also comes with a plugin for Fiddler, which is really handy and also has one for VS. Fiddler is a http debugging proxy. CodePlex is Microsoft’s answer to sourceforge.

You can find extensive list of json libraries here, for bunch of different languages.