Friday, December 14, 2007

What the Heck is JSON!?!

That was my question when I first came across JSON. What the heck is JSON, and why didn't I know it?

The quick definition is that JSON stands for JavaScript Object Notation. Of course, that doesn't really tell you much. Now you know what JSON stands for, but what does it really mean?

Come on, man, tell us. What does it mean?

The bottom line is that JSON is a lightweight data-interchange format. In English, that means that JSON is an simple and elegant method of moving data from one point to another.

Sound familiar? Are all you XML enthusiasts standing up, pumping your fists and screaming "Mother of God! My power....my power...it's slipping away. What's wrong with this world? Dear God, what's wrong with this world..."?

Well, sit down and take a deep breath. JSON isn't going to replace XML. JSON isn't going to go away, either.

JSON's strength is in it's simplicity. It is essentially either an unordered set of name/value pairs, or it is an ordered collection of values. That's it. Nothing more. The JSON clan claims that this is set in stone. There will never be anything more. No schema. No comments. No headache. Just simple data that you can move from point a to point b.

Ah, simplicity, I love you so....

I hear you. You want me to stop the yapping and show JSON in action. Well, here you go. We're going to use some code from a VE implementation. The code below is a server-side code snippet for getting some search results from MapPoint. This data will be consumed on the client so our job is to convert the data to JSON and send it upstream as a string. Ready? Okay!

/*...remember, this is inside a method on the server...most likely in a web service or a library called by the web service*/
FindResults fss = this.findService.FindAddress(fas);
JavaScriptSerializer s = new JavaScriptSerializer();
string json = s.Serialize(fss);
return json.ToString();


Whew! That JSON sure is hard work! The key is using the JavaScriptSerializer. That bad boy takes my collection of results objects and serializes it into a JSON collection of objects. Bing, bang, boom. Done.

Now let's see how we consume it on the client.

function WebMethodCallback(result, eventArgs)
{
var data;

try
{
data = eval('(' + result + ')');
var numberFound = data.NumberFound;

}
catch(e)
{
//handle this exception
}
}


Again, that's it! We eval the string into an object we called data. From that point forward, data is used just like any other object. Notice on the next line we set an local variable to a property off of the data object. Shazam! JSON in action, baby.

Tuesday, December 11, 2007

Virtual PC Virtually a Requirement for Any Developer

Virtual PC is a fantastic tool for the developer. I've used this Virtual PC in the past for everything from a Hong Kong version of Windows XP to Sharepoint development.

One of the things I like to do is have a "vanilla" installation of XP and Windows Server 2003 at the ready. When a new project comes down that requires installation, such as Sharepoint or Team Foundation Server, I'll copy the vanilla and begin my installation.

Virtual PC is easy to use. Simply install the client and then create a new VPC. The wizard will walk you though the steps.

One tricky item is getting network connectivity to the VPC. The easiest method is to select Settings for your newly created VPC, click on Networking, and then select Shared networking (NAT) as your adapter. This will use your host computers external network access for your VPC. Very easy. Very slick.

I can't say enough about the advantages of Virtual PC. If you need to test installations or web-based applications on different platforms, Virtual PC makes this a snap.

Wednesday, December 5, 2007

Microsoft Sponsored Architect Council in Bellevue

I went to the Architect Council Tuesday. I was able to get a preview of IIS 7.0, SQL 2008, and Visual Studio 2008. I'll be discussing VS2008 in much more detail later. The big news with IIS 7.0 (at least for me) was shared configuration. This is a single file, called the applicationHost.config, which has all of the configuration information for IIS. If you went to IIS Manager in IIS 6.0 to configure something, you'd find that in the applicationHost.config file for IIS 7.0.

Having a single file to house this information means that you can host it on your network and have your IIS boxes in your web farm all point to the single file for configuration. No longer will you run the risk of having a box or two in your farm not match the configuration of your other boxes. This is a huge win.

The boxes using this applicationHost.config file are watching it for changes. Once a change is saved, the boxes pick up the change and recycle at the appropriate level. For example, if you were to make an IIS-wide change, IIS would recycle, but if you were to make a change to an application pool, only that app pool would recycle.

Another big win for IIS is the integrated pipeline. This is important for static content that needed to be served up through asp.net before to take advantage of things like membership or caching. Now you can just select the components you want in your pipeline. I'll go into this in more detail in a future post, too.

One last win is the failed request tracing. This allows you to set up an XML dump based on whatever criteria you specify. For example, you could set up your site to watch for 500 errors and dump to XML whenever the error occurs. This gives us better investigation opportunities when things go wrong.

All-in-all a good day. Well worth the time. I made a few important contacts and I learned a couple of items.

Wednesday, November 28, 2007

Find a Location with Virtual Earth 6.0

A working map in Virtual Earth 6.0 is great, but what is really handy is having a map load with a specific area. This can be a state, a country, a city, whatever. It can be a location you have set as a default starting point, or a location supplied by a user.

For today, we'll just use a static starting location.

Use the code we created yesterday. We'll just modify it slightly so our map doesn't default to the whole of the United States.

Step 1
First, find the line of Javascript code that looks like this:
map.LoadMap();

We're going to change this line to load a specific point on the map.

Step 2
Next, we're going to create a new Virtual Earth object called the VELatLong object. This is one that you will use a lot in your Virtual Earth development. This object represents a position on the map - a latitude and longitude.

Since we're just creating a static starting location for our map, we're going to hard code our lat/long location. To do so, on the line just before map.LoadMap(), we create a new VELatLong object and pass in the latitude and longitude in the constructor, like this:
var centerPosition = new VELatLong(47.49319671735114,-122.21654891967773);

Step 3
Next, we're going to use this VELatLong object and pass it into our LoadMap method to set our starting location.
map.LoadMap(centerPosition);

Go ahead and compile and browse to your page. You should see the map load with a new location. But there is still a problem. We're too far away to see anything of value.

Step 4
Finally, we're going to set a default zoom level. The zoom levels are integers ranging from 1 to 19, with 19 being fully zoomed in. The default is 4. Let's get up close and personal, and set a zoom level of 13.
map.LoadMap(centerPosition,13);

That should do it. Compile and browse and you should be looking at Renton Airport.

Tuesday, November 27, 2007

How To Show a Map in Virtual Earth 6.0

Displaying a map using Virtual Earth 6.0 is a snap. In four short steps, you can have a fully functional, dynamic map on your web site!

Step 1.
First, we need to add a link to the Javascript that drives Virtual Earth. VE is a client-side application, which means it needs Javascript to run. If you have Javascript-disabled browsers in your user pool, you'll be out of luck.

Here's the link you'll need for the latest version of Virtual Earth:
<script type="text/javascript" src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6"></script>

Notice the ?v=6 appended to the end of the url. That indicates that you want to use version 6 of Virtual Earth.

You'll need this Javascript referenced and available on each page that you want the mapping functionality.

Step 2.
Next, you'll need to add a div to represent your map, like this:
<div id="myMap" style="z-index:100;position:relative; width:413px; height:327px; top:0px; left:0px; border-style:solid; border-width:1px; border-color:#C2C2C2;"></div>

Be sure this is inside of your form tags. You can style it anyway that you'd like. You can also call it anything that you'd like. I've called mine myMap. We'll use that ID in upcoming Javascript calls.

Step 3.
Next, we need to add a script block with a couple of short functions to initialize and set up the map. I've cryptically called these InitializeMap() and SetupMap(), as follows:

<script language="javascript" type="text/javascript">
//be sure this is outside the function block so you can reference it later.
var map = null;

function InitializeMap()
{
//pass in the ID of your div here
map = new VEMap('myMap');

//we need to give the map time to load before we use it...
setTimeout('SetupMap(); ', 1000);
}

function SetupMap()
{
try
{
map.LoadMap();
}
catch(e)
{
alert(e.message);
}
}
</script>

Step 4.
This is your last step. Here you need to get things kicked off. To do so, add a body.onLoad() method to your body element to fire off the InitializeMap() function, like this:
<body onload="InitializeMap()">

That's it! Compile your application and browse to your page. You should now have a map that allows panning and zooming. We'll discuss other things you can do with this map in later posts.

Monday, November 26, 2007

Visual Studio 2008 Available!

Visual Studio 2008 is available for download at MSDN subscriptions: http://msdn2.microsoft.com/en-us/subscriptions/bb608344.aspx

Thursday, November 15, 2007

devconnections 2007 - las vegas

I just returned from DevConnections 2007. It was well worth the time and money. Overall the conference was a great success. It offered a lot of great training. The rooms were large with perfect A/V setups. The sessions were 75 minutes each, offering just enough detail to get you started in the right direction with a handful of knowledge nuggets.

I would definitely recommend DevConn. It had all the benefits of TechEd without the trip to the East Coast, nor the added expense of two additional days of training. The three days DevConn offered was perfect. My brain wasn’t fried by the end, yet I was able to get a lot good training in the three days.

Trip Highlight
The best session was that with Juval Lowy. I first saw Juval about five years ago at a local conference when he taught a great session on test driven development. This time I listened to him teach about service-oriented development. It was awesome. Definitely the best of the bunch.

I'll detail more about this session and the trip in general in future posts. If you ever get a chance to go to DevConnections, TechEd, or the PDC, definitely go. They are a great time and full of information.