Skip to content

🚀 A Bridge.NET implementation of the Newtonsoft.Json API

License

Notifications You must be signed in to change notification settings

colderlord/Bridge.Newtonsoft.Json

Repository files navigation

The Bridge.Newtonsoft.Json package provides Bridge.NET support for a limited subset of the Newtonsoft.Json API. See it working on Deck.NET.

Build status Build Status NuGet Status

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
supported SerializeObject(Object) Serializes the specified object to a JSON string.
supported SerializeObject(Object, Formatting) Serializes the specified object to a JSON string using formatting.
supported SerializeObject(Object, JsonSerializerSettings Serializes the specified object to a JSON string using JsonSerializerSettings.
supported SerializeObject(Object, Formatting, JsonSerializerSettings) Serializes the specified object to a JSON string using formatting and JsonSerializerSettings.
not supported SerializeObject(Object, JsonConverter[]) Serializes the specified object to a JSON string using a collection of JsonConverter.
not supported SerializeObject(Object, Formatting, JsonConverter[]) Serializes the specified object to a JSON string using formatting and a collection of JsonConverter.
not supported SerializeObject(Object, Type, JsonSerializerSettings) Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings.
not supported 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
supported DeserializeObject(String) Deserializes the JSON to a .NET object.
supported DeserializeObject(String) Deserializes the JSON to the specified .NET type.
supported DeserializeObject(String, JsonSerializerSettings) Deserializes the JSON to a .NET object using JsonSerializerSettings.
supported DeserializeObject(String, JsonSerializerSettings) Deserializes the JSON to the specified .NET type using JsonSerializerSettings.
supported DeserializeObject(String, Type) Deserializes the JSON to the specified .NET type.
supported DeserializeObject(String, Type, JsonSerializerSettings) Deserializes the JSON to the specified .NET type using JsonSerializerSettings.
not supported DeserializeObject(String, JsonConverter[]) Deserializes the JSON to the specified .NET type using a collection of JsonConverter.
not supported 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
supported PopulateObject(String, Object) Populates the specified object with values from a JSON string.
supported PopulateObject(String, Object, JsonSerializerSettings) Populates the specified object with values from a JSON string using JsonSerializerSettings.

Example

Account account = new Account
{
    Email = "[email protected]",
    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": "[email protected]",
//   "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
supported None 0 No special formatting is applied. This is the default.
supported 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.

Supported Name Value Description
supported None 0 Do not include the .NET type name when serializing types.
supported Objects 1 Include the .NET type name when serializing into a JSON object structure.
not supported Arrays 2 Include the .NET type name when serializing into a JSON array structure.
not supported All 3 Always include the .NET type name when serializing.
not supported Auto 4 Include the .NET type name when the type of the object being serialized is not the same as its declared type. Note that this doesn't include the root serialized object by default. To include the root object's type name in JSON you must specify a root type object with SerializeObject(Object, Type, JsonSerializerSettings) or Serialize(JsonWriter, Object, Type).

NullValueHandling

Specifies null value handling options for the JsonSerializer.

Original NullValueHandling documentation from Newtonsoft.Json.

Supported Name Value Description
supported Include 0 Include null values when serializing and deserializing objects.
supported 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"
// }

About

🚀 A Bridge.NET implementation of the Newtonsoft.Json API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •