We are pleased to inform you that we have released an open-source extension for CSHTML5 that adds JSON serialization/deserialization support!

If you are new to the concept of Extensions for CSHTML5, you can read more here.This extension requires CSHTML5 v1.0 Beta 8.2 or newer (link)

UPDATE (July 14, 2017): Improved support for numbers and booleans that are surrounded by quotes. Improved support for generic list deserialization.UPDATE (June 20, 2017): Added support for Guid, null, Nullable<>, and ObservableCollection. Improved support for collections and enumerables.UPDATE (June 8, 2017): Fixed issue with deserialization of empty arrays.UPDATE (April 2017): Added support for Enum and DateTime.

How to install/use it?

To use it, simply add a new class named "JsonConvert.cs" to your project, and copy/paste the following code:

return dateTime; } catch { throw new InvalidOperationException(string.Format("'{0}' is not a valid value for a DateTime.", (string)value)); } } else if (resultType == typeof(string)) { // If the target type is a string, no conversion is needed: return value; } // If the target is another value type, it means that we are converting for example a string into a number or a bool (this is not supposed to happen if the JSON string is perfectly formatted, but it may happen if the JSON string contains numbers inside quotes, or booleans inside quotes): else if (resultType.IsValueType) { // If the result type is Nullable, we need to get the underlying type in order to be able to properly call the "ChangeType" method below (useful for example when converting from "double" to nullable "int?"): var nullableUnderlyingType = Nullable.GetUnderlyingType(resultType); if (nullableUnderlyingType != null) { resultType = nullableUnderlyingType; }

// Verify that the target type is also a value type: if (resultType != null && resultType.IsValueType) { // If the result type is Nullable, we need to get the underlying type in order to be able to properly call the "ChangeType" method below (useful for example when converting from "double" to nullable "int?"): var nullableUnderlyingType = Nullable.GetUnderlyingType(resultType); if (nullableUnderlyingType != null) { resultType = nullableUnderlyingType; }

// Get the type of the IEnumerable items (this works for example with List<T>, ObservableCollection<T>, etc.): Type itemsType = genericArguments[0];

// Create a temporary List<T> in order to add items to it. Later we will convert it to the final type if needed. var list = typeof(JsonConvert) .GetMethod("CreateNewInstanceOfGenericList", BindingFlags.NonPublic | BindingFlags.Static) .MakeGenericMethod(itemsType) .Invoke(null, new object[] { });

// Note: in the code above, we call the method "CreateNewInstanceOfGenericList" instead of // calling "var list = Activator.CreateInstance(typeof(List<>).MakeGenericType(itemsType))" // because the latter does not appear to properly initialize the underlying JavaScript "_items" // collection that is inside the List<T> implementation in JSIL as of July 14, 2017, thus // resulting in an exception when calling the Add method (reference: CSHTML5 tickets #623 and // #648).

// Get a reference to the "Add" method of the generic list that we just created: var listAddMethod = list.GetType().GetMethod("Add");

// Create a new instance of the resulting type: try { result = Activator.CreateInstance(resultType); } catch (Exception ex) { if (!ignoreErrors) throw new InvalidOperationException(string.Format("Unable to create an instance of type '{0}'. A common cause is that the type does not have a default public constructor.", (resultType != null ? resultType.ToString() : "null")), ex); else return null; }

static Task<IJsonType> ConvertJavaScriptObjectToCSharpNestedDictionariesAndLists(object javaScriptObject, bool ignoreErrors) { // Note: This method needs to be "async" because it is // recursive and, in the Simulator, it is not possible // to recursively call "ExecuteJavaScript" (ie. to make // nested synchronous calls to JS).

// Note: the following JS call must be asynchronous because // this algorithm is recursive and, in the Simulator, it is // not possible to recursively call "ExecuteJavaScript" // (ie. to make nested synchronous calls to JS).

// Note: the following JS call must be asynchronous because // this algorithm is recursive and, in the Simulator, it is // not possible to recursively call "ExecuteJavaScript" // (ie. to make nested synchronous calls to JS).

// Note: the code above was commented and replaced by the code below because in some // cases the call to "IsAssignableFrom" let to an exception due to an issue in the // JS implementation in the JSIL libraries (reference: CSHTML5 tickets #623 and #648).

static Type GetInterface(Type type, string name) { // Note: this method is here because "Type.GetInterface(name)" is not yet // available in the JSIL libraries as of July 14, 2017. // When available, this method can be replaced with "Type.GetInterface(name)".

static object ChangeType(object value, Type conversionType) { // Note: this method is here to work around an issue in the "Convert.ChangeType" // implementation in the JSIL libraries as of July 14, 2017, which does not // appear to give the appropriate result when converting from a string to // an integer or boolean. It can be replaced with "Convert.ChangeType" when // fixed.

MessageBox.Show("Name of the second feature: " + deserializedProduct.Features[1].Name);MessageBox.Show("Name of the third available size: " + deserializedProduct.Sizes[2]);MessageBox.Show("Release date: " + deserializedProduct.ReleaseDate.ToString());

MessageBox.Show("Product name: " + deserializedObject["Name"].Value.ToString());MessageBox.Show("Name of the second feature: " + deserializedObject["Features"][1]["Name"].Value.ToString());MessageBox.Show("Name of the third available size: " + deserializedObject["Sizes"][2].Value.ToString());

Sorry if this is a ridiculous question - I've got limited programming knowledge that I tend to gather just as I need it. I have been using C# to make web service call and deserialize the JSON through JSON.Net, but that's obviously not possible with C#/XAML. The web service I'm working with uses spaces in field names, so with JSON.Net I was able to set the "JsonProperty" in the classes:

UPDATE: The JSON extension has been updated with the following features:- Support for Guid- Support for null and Nullable<>- Support for ObservableCollection- Improved support for collections and enumerables

UPDATE: The JSON extension has been updated today with the following features:- Improved support for numbers and booleans that are surrounded by quotes.- Improved support for generic list deserialization.