The Bridge.Newtonsoft.Json package provides Bridge.NET support for a limited subset of the Newtonsoft.Json API. See it working on Deck.NET.
IMPORTANT: Only the features listed below are supported.
Installation
Install using NuGet:
Install-Package Bridge.Newtonsoft.Json
JsonConvert.SerializeObject
Serializes the specified object to a JSON string.
Original SerializeObject documentation from Newtonsoft.Json.
Supported | Name | Description |
---|---|---|
SerializeObject(Object) | Serializes the specified object to a JSON string. | |
SerializeObject(Object, Formatting) | Serializes the specified object to a JSON string using formatting. | |
SerializeObject(Object, JsonSerializerSettings | Serializes the specified object to a JSON string using JsonSerializerSettings. | |
SerializeObject(Object, Formatting, JsonSerializerSettings) | Serializes the specified object to a JSON string using formatting and JsonSerializerSettings. | |
SerializeObject(Object, JsonConverter[]) | Serializes the specified object to a JSON string using a collection of JsonConverter. | |
SerializeObject(Object, Formatting, JsonConverter[]) | Serializes the specified object to a JSON string using formatting and a collection of JsonConverter. | |
SerializeObject(Object, Type, JsonSerializerSettings) | Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings. | |
SerializeObject(Object, Type, Formatting, JsonSerializerSettings) | Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings. |
Example
Product product = new Product();
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "ExpiryDate": "2008-12-28T00:00:00",
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
JsonConvert.DeserializeObject
Deserializes the JSON to a .NET object.
Original DeserializeObject documentation from Newtonsoft.Json.
Supported | Name | Description |
---|---|---|
DeserializeObject(String) | Deserializes the JSON to a .NET object. | |
DeserializeObject(String) | Deserializes the JSON to the specified .NET type. | |
DeserializeObject(String, JsonSerializerSettings) | Deserializes the JSON to a .NET object using JsonSerializerSettings. | |
DeserializeObject(String, JsonSerializerSettings) | Deserializes the JSON to the specified .NET type using JsonSerializerSettings. | |
DeserializeObject(String, Type) | Deserializes the JSON to the specified .NET type. | |
DeserializeObject(String, Type, JsonSerializerSettings) | Deserializes the JSON to the specified .NET type using JsonSerializerSettings. | |
DeserializeObject(String, JsonConverter[]) | Deserializes the JSON to the specified .NET type using a collection of JsonConverter. | |
DeserializeObject(String, Type, JsonConverter[]) | Deserializes the JSON to the specified .NET type using a collection of JsonConverter. |
Example
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
Deck.NET
https://deck.net/newtonsoft.json
public class Program
{
public static void Main()
{
Product product = new Product();
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = JsonConvert.SerializeObject(product, Formatting.Indented);
// Write the json string
Console.WriteLine(output);
// Deserialize the json back into a real Product
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
// Write the properties
Console.WriteLine(deserializedProduct.Name);
Console.WriteLine(deserializedProduct.ExpiryDate);
Console.WriteLine(deserializedProduct.Price);
Console.WriteLine(deserializedProduct.Sizes);
}
}
public class Product
{
public string Name { get; set; }
public DateTime ExpiryDate { get; set; }
public decimal Price { get; set; }
public string[] Sizes { get; set; }
}
JsonConvert.PopulateObject
Populates the specified object following the description in a JSON string.
Original PopulateObject documentation from Newtonsoft.Json.
Supported | Name | Description |
---|---|---|
PopulateObject(String, Object) | Populates the specified object with values from a JSON string. | |
PopulateObject(String, Object, JsonSerializerSettings) | Populates the specified object with values from a JSON string using JsonSerializerSettings. |
Example
Account account = new Account
{
Email = "james@example.com",
Active = true,
CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
Roles = new List<string>
{
"User",
"Admin"
}
};
string json = @"{
""Active"": false,
""Roles"": [
""Expired""
]
}";
JsonConvert.PopulateObject(json, account);
// {
// "Email": "james@example.com",
// "Active": false,
// "CreateDate" = "2013-01-20",
// "Roles": [
// "User",
// "Admin",
// "Expired"
// ]
// }
Formatting
Specifies formatting options for the JsonTextWriter.
Original Formatting documentation from Newtonsoft.Json.
Supported | Name | Value | Description |
---|---|---|---|
None | 0 | No special formatting is applied. This is the default. | |
Indented | 1 | Causes child objects to be indented according to the Indentation and IndentChar settings. |
JsonSerializerSettings
Original JsonSerializerSettings documentation from Newtonsoft.Json.
ContractResolver
Original ContractResolver documentation from Newtonsoft.Json.
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
TypeNameHandling
Specifies type name handling options for the JsonSerializer.
Original TypeNameHandling documentation from Newtonsoft.Json.
NullValueHandling
Specifies null value handling options for the JsonSerializer.
Original NullValueHandling documentation from Newtonsoft.Json.
Supported | Name | Value | Description |
---|---|---|---|
Include | 0 | Include null values when serializing and deserializing objects. | |
Ignore | 1 | Ignore null values when serializing and deserializing objects. |
JsonConstructor Attribute
Instructs the JsonSerializer to use the specified constructor when deserializing that object.
Original JsonConstructor documentation from Newtonsoft.Json.
https://deck.net/5adc821b73491a122a51fd229ee1a8d3
using System;
using Newtonsoft.Json;
public class Program
{
public static void Main(string[] args)
{
var x = Message.Empty.Add("abc");
var json = JsonConvert.SerializeObject(x);
Console.WriteLine(json);
var cloneX = JsonConvert.DeserializeObject<Message>(json);
Console.WriteLine(cloneX.Value);
}
}
public class Message
{
static Message _empty = new Message("");
public static Message Empty
{
get
{
return _empty;
}
private set
{
_empty = value;
}
}
private Message(bool value) { }
[JsonConstructor]
private Message(string value)
{
Value = value;
}
public string Value { get; private set; }
public Message Add(string value)
{
return new Message(Value + value);
}
}
JsonIgnore Attribute
Instructs the JsonSerializer not to serialize the public field or public read/write property value.
Original JsonIgnore documentation from Newtonsoft.Json.
public class Product
{
public string Name { get; set; }
[JsonIgnore]
public DateTime ExpiryDate { get; set; }
public double Price { get; set; }
[JsonIgnore]
public string[] Sizes { get; set; }
}
public static void Main()
{
var x = new Product
{
Name = "Apple",
ExpiryDate = DateTime.Now,
Price = 3.99,
Sizes = new string[] { "S", "M", "L" }
};
var json = JsonConvert.SerializeObject(x);
}
// {
// "Name": "Apple",
// "Price": "3.99"
// }