| By Adam Hoffman | Article Rating: |
|
| January 10, 2013 09:15 AM EST | Reads: |
2,292 |
Now, in order to do this, it makes some assumptions. It looks at the JSON object, and if it sees a certain type of data, it creates a certain type of column to support the data. The easiest thing that it could have done would be to just create text columns in the underlying SQL database, since any type of data can be stored as a string. Instead, it does something quite a bit smarter, and creates data types that are quite a bit more “appropriate”. Here’s the strategy that it uses:
| JSON / CLR Data Type | T-SQL Type in underlying SQL Azure database table |
| Numeric values (integer, decimal, floating point) | float(53) – this is the maximum precision float. |
| Boolean | bit |
| DateTime | DateTimeOffset(3) |
| String | nvarchar(max) |
OK, so this is great. Dynamic schema is pretty cool. However…
When I work on a database schema, I often run into a case where I want to create a “code/decode” style table. In my world, a “code/decode” table is typically a two column table, where the primary key is the “code” and the other column is a textual description. The canonical example is a “States” table (in the US). Like this:
| Code | Decode |
| AL | Alabama |
| AK | Alaska |
| AZ | Arizona |
| … | … |
| IL | Illinois |
| … | … |
| WY | Wyoming |
Then, with this table defined, I’ll just use it anywhere I need a State… like this:
| Column | DataType | Notes |
| FirstName | nvarchar(50) | |
| LastName | nvarchar(50) | |
| Address1 | nvarchar(100) | |
| Address2 | nvarchar(100) | |
| City | nvarchar(100) | |
| State | char(2) | Foreign Key to State Code |
| ZIPCode | char(5) |
Now, in my current project, it wasn’t a state code that I needed, it was a “CurrencyType”. And moreover, I wanted to be able to have an “enum” value of CurrencyTypes to be able to work with in my application. This led me to wonder how WAMS could handle storing and retrieving an “enum” in the database. It’s not immediately clear that it would know what to do with it as a JSON type. The JavaScriptSerializer seems to turn enum values into an integer value, which would probably work, but I really wanted my underlying SQL table to store “IL” for Illinois, and not the number 14 as a 53 bits of precision float. So, how can I turn my enum values into the underlying state codes as a 2 character string on the way in, and reconvert them back to my enum values on the way out?
To start with, here’s the (truncated) version of my enum for my CurrencyType.
public enum CurrencyType
{
UnitedArabEmiratesDirham,
AfghanistanAfghani,
AlbaniaLek,
//...
UnitedStatesDollar,
//...
Unknown
}
Next, the secret sauce turns out to be an IDataMemberJsonConverter . This interface exists within the Microsoft.WindowsAzure.MobileServices assembly, and has 2 methods that you need to implement – ConvertFromJson and ConvertToJson. Here’s the type converter that I created to massage my enum into a string going in, and from a string back to the enum on the way back out…
First, the ConvertToJson method:
public IJsonValue ConvertToJson(object instance)
{
if (instance is Types.CurrencyType)
{
switch ((Types.CurrencyType)instance)
{
case Types.CurrencyType.UnitedArabEmiratesDirham:
return JsonValue.CreateStringValue("AED");
case Types.CurrencyType.AfghanistanAfghani:
return JsonValue.CreateStringValue("AFN");
case Types.CurrencyType.AlbaniaLek:
return JsonValue.CreateStringValue("ALL");
// SNIP
case Types.CurrencyType.UnitedStatesDollar:
return JsonValue.CreateStringValue("USD");
// SNIP
default:
return JsonValue.CreateStringValue("XXX");
}
}
else
return JsonValue.Parse("null");
}
Next, the ConvertFromJson method:
public object ConvertFromJson(IJsonValue value)
{
CurrencyType result = Types.CurrencyType.Unknown;
if (value != null && value.ValueType == JsonValueType.String)
{
switch (value.GetString())
{
case "AED":
return Types.CurrencyType.UnitedArabEmiratesDirham;
case "AFN":
return Types.CurrencyType.AfghanistanAfghani;
case "ALL":
return Types.CurrencyType.AlbaniaLek;
// SNIP
case "USD":
return Types.CurrencyType.UnitedStatesDollar;
// SNIP
case "ZWD":
return Types.CurrencyType.ZimbabweDollar;
case "XXX":
return Types.CurrencyType.Unknown;
default:
return Types.CurrencyType.Unknown;
}
}
return result;
}
Now, the only thing remaining is to decorate the CLR class that we’re using to hold our object with enough information to get the serializer to use our type converter as necessary. In my case, the column for CurrencyType is contained in a class called FamilyGroup. Here’s that implementation:
public class FamilyGroup
{
public long Id { get; set; }
public string Name { get; set; }
[DataMemberJsonConverter(ConverterType = typeof(CurrencyTypeConverter))]
public CurrencyType CurrencyType { get; set; }
}
And that’s it. We have our enum to work with in our code, we’re able to store and retrieve our values to WAMS and the data in the tables is readable to boot.
Happy coding.
Published January 10, 2013 Reads 2,292
Copyright © 2013 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Adam Hoffman
Adam is an technical evangelist working for Microsoft. By day, you can likely find him somewhere in the Midwest, driving to somewhere, ready to figure out how the cloud can save your family from certain doom, and make you rich and successful in the process. Before he started evangelizing, Adam was a Senior Developer Lead for Microsoft in Redmond, working on Office 365, BPOS, and Office Live. He misses Redmond, and the excitement of the mother ship, but the call of bitter cold and lots of snow in Chicago was too much for him, and he had to return.
Prior that that he developed software and ran teams for Thomson Reuters, Method Engine (which he co-founded), VSA Partners, Navigant Consulting, Andersen Consulting, and a couple of CASE tool companies way back in the day. His favorite possession is a clone of the original Altair computer, running BillG's 8K BASIC, which he's still intending to hook up to the cloud, someday, somehow.
You can read his Windows Azure Blog at http://stratospher.es, and tweeter-er him at http://twitter.com/stratospher_es.
Additionally, if you'd like to keep track of his cloud connected car, you can follow it on Twitter at http://twitter.com/cloudcar1.
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo New York Speaker Profile: Dave Linthicum – Cloud Technology Partners
- Cloud Expo New York: Cloud Is Changing the Economics of Business
- Best CIO Practices Shared from SHI’s Customers
- Cloud Expo New York: Deploying Hybrid Cloud for Performance and Uptime
- Cloud Expo New York: Delivering Digital Marketing on the Cloud
- Big Data Isn’t About the Database, It’s About the Application
- Cloud Expo New York: Rethink IT and Reinvent Business with IBM SmartCloud
- Cloudant to Exhibit at Cloud Expo & Big Data Expo New York
- BEA Updates WebLogic SOA Portal for Web 2.0 Era
- How to Move Your Oracle Databases to Amazon EC2 Cloud
- The Accessibility of the Cloud
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo New York: Best CIO Practices Shared from SHI’s Customers
- Cloud Expo New York Speaker Profile: Dave Linthicum – Cloud Technology Partners
- Cloud Expo New York: Cloud Is Changing the Economics of Business
- Cloud Expo New York: How to Use Google Apps Script
- Cloud Computing Bootcamp at Cloud Expo New York
- Rackspace Hosting Named “Platinum Plus Sponsor” of Cloud Expo New York
- Best CIO Practices Shared from SHI’s Customers
- Cloud Expo New York: Why Big Data Is Really About Small Data
- Cloud Expo New York: Deploying Hybrid Cloud for Performance and Uptime
- Cloud Expo New York: Delivering Digital Marketing on the Cloud
- Small Cancers, Big Data, and a Life Examined
- The i-Technology Right Stuff
- The Top 150 Players in Cloud Computing
- Who Are The All-Time Heroes of i-Technology?
- Where Are RIA Technologies Headed in 2008?
- Get the Message
- i-Technology Viewpoint: Is Web 2.0 the Global SOA?
- ESB Myth Busters: 10 Enterprise Service Bus Myths Debunked
- i-Technology Viewpoint: Thinking Outside the VC Box
- i-Technology Viewpoint: When to Leave Your First IT Job
- SOA Web Services Edge Conference Coverage on SYS-CON.TV
- SYS-CON.TV's "SOA Web Services" and "Enterprise Open Source" Programs To Air in December
- Five Reasons Why Web 2.0 Matters


















