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.
UPDATE (January 21, 2021): Fixed NotSupportedException with generic enumerables during deserialization in the Simulator (with CSHTML5 v2), and Enum deserialization
UPDATE (February 26, 2020): Fixed serialization on Integer types when running in the Simulator (with CSHTML5 v2)
UPDATE (November 9, 2018): Added Bridge.NET support, added a bunch of stubs, and changed namespace from "CSHTML5.Extensions.Json" to "Newtonsoft.Json"
UPDATE (September 12, 2018): Improved support for Long types (Int64)
UPDATE (October 13, 2017): Added support for case-insensitive deserialization: if the case-sensitive property is not found, the deserializer looks for the case-insensitive property. Also added JsonArray object with ability to count the number of items.
UPDATE (September 13, 2017): Fixed an issue with static properties that interfered with the serialization/deserialization. Furthermore, private properties are no longer serialized by default. To serialize them, add "BindingFlags.NonPublic" to the "GetProperties" call.
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 install and use it, simply add a new class named "JsonConvert.cs" to your project, and copy/paste the code from the following URL:
https://gist.github.com/cshtml5/2446871a8bc0fa4ceddcfb448b6aca54
Documentation:
Here is an example code for Serialization:
Code: Select all
string json = JsonConvert.SerializeObject(product);
MessageBox.Show(json);
Here is an example code for strongly-typed Deserialization:
Code: Select all
Product deserializedProduct = await JsonConvert.DeserializeObject<Product>(json);
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());
And here is an example code for dynamic Deserialization:
Code: Select all
IJsonType deserializedObject = await JsonConvert.DeserializeObject(json);
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());
Here is some JSON string for testing:
Code: Select all
{
"Name":"TestProduct",
"ProductType":"B2C",
"Price":12.5,
"Count":341,
"IsAvailable":true,
"Sizes":[
"Small",
"Medium",
"Large"
],
"Features":[
{
"Name":"TestFeature1"
},
{
"Name":"TestFeature2"
},
{
"Name":"TestFeature3"
}
],
"ReleaseDate":"2017-04-10T16:26:41.754Z"
}
Here are some test classes:
Code: Select all
public class Product
{
public string Name { get; set; }
public ProductType ProductType { get; set; }
public double Price { get; set; }
public int Count { get; set; }
public bool IsAvailable { get; set; }
public string[] Sizes { get; set; }
public List<Feature> Features { get; set; }
public DateTime ReleaseDate { get; set; }
}
public class Feature
{
public string Name { get; set; }
}
public enum ProductType
{
B2B, B2C
}
We hope you will enjoy this library!
If you find any issues, please feel free to post them on the forums or to send an email to support@cshtml5.com
Thank you.
Regards,
The CSHTML5 Team