Json Helper
The JsonHelper class enables you to serialize objects into JSON or parse JSON strings and convert them into objects.
| Method Name | Arguments | Description |
|---|
| FromJson<T>(string jsonStr) | jsonString of type string, Type of the object is defined via Generics "T". | Converts a JSON string to an object of type "T". |
| ToJson(object model) | Any object type | Returns a JSON string. |
| ToJson(object model,
JsonSerializationSettings settings) | Any object type for model argument,
JsonSerializationSettings options for settings | Returns a JSON string. |
The example below demonstrates how to convert an object to a JSON string and how to parse the JSON string back to the object:
var contact = Database.Query<Contact>()
.Where(a => a.Email == "jeanm@mycompany.com")
.First();
// Convert to JSON string:
string jsonStr = JsonHelper.ToJson(contact);
// Parse JSON string into object:var contactObj = JsonHelper.FromJson<Contact>(jsonStr);
JsonSerializationSettings
| Property | Type | Notes |
|---|
| ValueWhenNull | string | This value is returned if the object is null. |
IndentedFormatting | bool | JSON string will be prettified. |
| IgnoreNulls | bool | Null properties will not be added to the final JSON string |