| By Adam Hoffman | Article Rating: |
|
| January 10, 2013 09:15 AM EST | Reads: |
2,684 |
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,684
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
- New Relic Q1 2013 Blazes Past Growth Targets and Reaches 40,000 Active Customer Accounts
- Cloud Expo New York: Using APIs for Better Business Partnerships
- Research and Markets: Crystal Oscillator Market: Global Forecast & Analysis - A Market Research Report Segmented by General Circuitry, By Applications, By Technology, By Mounting Scheme & Geography (2013-2018)
- Don’t forget to register for FOSE 2013
- Component Models in Java | Part 2
- The Cloud Delivers a New American Workforce
- Synchronoss Technologies to Present at Upcoming Conferences
- API Management Start-Up Gets Funding
- Interop Las Vegas Previews News Announcements from over 60 Exhibitors & Sponsors
- Verizon to Go into the Cloud Storage Biz
- How to Stop Worrying and Love Your Cloud
- Cloud People: A Who's Who of Cloud Computing
- New Relic Q1 2013 Blazes Past Growth Targets and Reaches 40,000 Active Customer Accounts
- GDS International Confirms Unprecedented Delegation for Upcoming Next Generation Telecoms Europe Summit in May
- AWS Going into a New Line of Work
- Cloud Expo New York: Using APIs for Better Business Partnerships
- Research and Markets: Crystal Oscillator Market: Global Forecast & Analysis - A Market Research Report Segmented by General Circuitry, By Applications, By Technology, By Mounting Scheme & Geography (2013-2018)
- Google Compute enters the IaaS market
- Agile Solutions for Cloud, Big Data, Mobility Services
- SolarWinds to Announce First Quarter 2013 Financial Results on Tuesday, April 30 and Present at Upcoming Investor Conferences
- Don’t forget to register for FOSE 2013
- Apple’s Key Rubber-Band Patent Found Invalid Again
- How to Re-imagine Your Business for a Mobile World
- Where Are RIA Technologies Headed in 2008?
- Should RIM BlackBerries Be Rented?
- Has the Technology Bounceback Begun?
- Trump's Apprentice Runner-Up Rebecca Jarvis Has $150,000 Job Offer From SYS-CON Media
- "Mobile Web 2.0" – How Web 2.0 Impacts Mobility & Digital Convergence
- Ringback Tones
- Microsoft and Sprint Collaborate on Mobile Search
- Mobile Music Gets Boost From New W600 "Walkman Phone"
- i-Technology Blog: Zero-Cost Telephony, the 6-Ton Elephant in the Telco Room
- Dolphin Announces Open API With Over 50 Add-ons Including Dropbox and Wikipedia
- Java Edition of Windows Live Messenger for Mobile Launched
- Alcatel + Microsoft = Internet TV Over IP, a.k.a. "IPTV," Coming Soon To a PC or TV Near You























