The One About The Serial Killer

So we’re hooking in to an external RESTful service. Because, REST man, it’s lovely and SOAP/BiscuitTransfer/Whatever is the enemy. Looove a bit of REST…

OK, now you’re back from Googling BiscuitTransfer, lets break this.

We need to Serialize some classes. How do we do that? Duh. Sitecore’s already using Newtonsoft.Json. It’d be stupid not to use it as well. Don’t be stupid.

So we do it like this:

var body = JsonConvert.SerializeObject(request);

Lovely stuff.

Compile/Run/Whatever (Unit Test?)

Aww, it doesn’t work quite right. You look at the output and call the guy who wrote the service (because it’s documented right?) and it turns out you need to use camel case, ignore those pesky nulls and a few other things which make little sense but you’ll do as you’re told.

So you’re all “I’m not doing that every time I call SerializeObject, I’m a ROCK STAR”. You’re doing that shit in an Initialize method (or in my case anĀ Initialise method because I’m British and screwyouMicrosoft).

     JsonConvert.DefaultSettings = () => new JsonSerializerSettings
     {
         ContractResolver = new CamelCasePropertyNamesContractResolver(),
         NullValueHandling = NullValueHandling.Ignore,
         DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
         CheckAdditionalContent = true,
         Converters = converters
     };

You run all your Unit Tests (some of them are technically integration tests. Shhh!)

UnitTestsYeah baby! They all pass.

You rock.

Up to GIT it goes.

You push that bad boy.

LUNCHTIME!

You nip over to the cafe and have one of their extraordinary toasted paninis. Cheese and salami. Tasty.

You get back from lunch and the other devs aren’t happy. Some muppet broke Sitecore – not totally. But someone important noticed. No-one’s really sure what’s caused it but it’s not happy. DAMN YOU CONTINUOUS INTEGRATION!

Errors2

So it turns out, DefaultSettings means “Default Settings”. So Sitecore uses those as well. Who knew?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s