Skip to main content
Home  ›  Blog

AsDynamic(json).IsAweSOme == true

A new AsDynamic(...) in 2sxc provides case-insensitive, null-safe JSON access. This bridges the gap between javascript camelCase and C# PascalCase and helps with null checks.

WFT is this for?

There are quite a few reasons for this...

  1. Casing: If you have a JSON from anywhere, it's usually cased differently than C# conventions. For example, the 2sxc GPS field stores {"latitude": ..., "longitude": ...} but in C# you would prefer Latitude and Longitude. JavaScript objects could also arrive as someObject or someobject. Enabling case-insensitive access just makes life easier. 
  2. Null-Checks: since a JSON could contain anything, there is a big risk that fields are missing when you work with the data. Being able to write var gpsLat = AsDynamic(json).Latitude ?? 0; is just way cooler than using a try-catch or a bunch of safe conversions. 
  3. Duck-Typing for different schemas: The null-checks also help when you're not sure which JSON you'll get. In some cases you expect a JSON that could be a person or a product. In these cases it's also much easier to verify this with duck-typing like in JavaScript instead of complicated C# object casts. 
  4. Single Item or Array: Most JSON de-serializers require your code to decide if it's going to unpack an array or an object. But our new GenericJacket doesn't care - just give it the JSON, and you get what you need. 

You'll also notice that many developers run into this challenge, if you google the questions. 2sxc has the answer 😁.

Using in it 2sxc for Custom Input Fields

The main reason we implemented this in 2sxc is custom input fields like the GPS picker. Previously these were implemented in a way where a custom field provided the UI. You then created a bunch of additional fields (like a Longitude and Latitude field) to write the values, so that the Razor code could access them. 

Now using the new AsDynamic, you can simply write @AsDynamic(Content.GPS).Latitude. Awesome, right?

We believe this will make it much more appealing to create custom input fields in 2sxc. These fields could also just be a UI which retrieve data from an API-endpoint, saving it into 2sxc as JSON. Anything you want is now easy to do. 

Using it in 2sxc for External Data

Another common use case is code which talks to a WebApi. In such scenarios you get JSON back, and now it's really easy to use it in C# code. 

API & Examples

We documented the APIs and created some JSON data examples in our tutorial app, and the new Content templates for 2sxc 10.20+ will also show how to use this with GPS fields. Enjoy!

Note that you need 2sxc 10.21 to use this. 

Love from Switzerland,

Daniel Mettler grew up in the jungles of Indonesia and is founder and CEO of 2sic internet solutions in Switzerland and Liechtenstein, an 20-head web specialist with over 800 DNN projects since 1999. He is also chief architect of 2sxc (see github), an open source module for creating attractive content and DNN Apps.

Read more posts by Daniel Mettler