WFT is this for?
There are quite a few reasons for this...
- Casing: If you have a JSON from anywhere, it's usually cased differently than C# conventions. For example, the 2sxc GPS field stores
someobject. Enabling case-insensitive access just makes life easier.
- 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.
- 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,