JsonPack.ImmutableCollection 2.0.0

dotnet add package JsonPack.ImmutableCollection --version 2.0.0
NuGet\Install-Package JsonPack.ImmutableCollection -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JsonPack.ImmutableCollection" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JsonPack.ImmutableCollection --version 2.0.0
#r "nuget: JsonPack.ImmutableCollection, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install JsonPack.ImmutableCollection as a Cake Addin
#addin nuget:?package=JsonPack.ImmutableCollection&version=2.0.0

// Install JsonPack.ImmutableCollection as a Cake Tool
#tool nuget:?package=JsonPack.ImmutableCollection&version=2.0.0

Installation

NuGet packages

To install with NuGet, just install the JsonPack package:

Install-Package JsonPack

Quick Start

public class MyClass
{
	public int Age { get; set; }

	public string FirstName { get; set; }

	public string LastName { get; set; }

	[IgnoreDataMember]
	public string FullName { get; set; }
}

Call JsonPackSerializer.ToJsonString<T>/JsonPackSerializer.Deserialize to serialize/deserialize your object instance You can also use JsonPackSerializer.Serialize<T> method to serialize to byte array instead.

class Program
{
    static void Main(string[] args)
    {	
	var mc = new MyClass
        {
            Age = 69,
            FirstName = "Foo",
            LastName = "Bar",
        };

        // Call ToJsonString/Deserialize, that's all.
        string json = JsonPackSerializer.ToJsonString(mc);
        MyClass mc2 = JsonPackSerializer.Deserialize<MyClass>(json);
    }
}

Object Serialization

JsonPack for C# can serialize your own public class or struct types, but is not limited to it and can be extended with custom formatters to support external types as well.

Attribution

JsonPackContract Attribte

Apply MemberSerialization option to specify member serialization.

// `OptOut` All public members are serialized by default.
[JsonPackContract(MemberSerialization = MemberSerialization.OptOut)]
public class Sample1
{
    public int Foo { get; set; }
    private int Bar { get; set; }
}

// Only members marked with `DataMemberAttribute` are serialized.
[JsonPackContract(MemberSerialization = MemberSerialization.OptIn)]
public class Sample2
{
    [DataMember]
    public int Foo { get; set; }
    private int Bar { get; set; }
}

// All public and private fields are serialized.
[JsonPackContract(MemberSerialization = MemberSerialization.Fields)]
public class Sample3
{
    public int Foo { get; set; }
    private int Bar { get; set; }
}

Apply SkipConstructor option to explicitly skip the constructor call on deserialization

[JsonPackContract(SkipConstructor = true)]
public class Sample1
{
    public int Foo { get; set; }
}

JsonPackConstructor Attribute

Allows to specify a constructor to be used for deserialization.

public class Sample1
{
    public int Foo { get; private set; }

    [JsonPackConstructor]
    private Sample1(int foo) 
    {
        Foo = foo;
    }
}

JsonPackFormatter Attribute

Allows to place a custom JsonPackFormatter<T> for you own class or struct or member

[JsonPackFormatter(typeof(MySample1Formatter))]
public class Sample1
{
    public int Foo { get; set; }
}

public class Sample2
{
    [JsonPackFormatter(typeof(MyFooFormatter))]
    public int Foo { get; set; }
}

JsonPackNullValueAttribute

Defines the null value handling option for a specific member. This overrides the option provided by JsonPackSerializerOptions.

public class Sample1
{
    // Ignore null values when serializing and deserializing objects.
    [JsonPackNullValue(NullValueHandling.Ignore)]
    public string Foo { get; set; }
}

public class Sample2
{
    // Include null values when serializing and deserializing objects.
    [JsonPackNullValue(NullValueHandling.Include)]
    public string Foo { get; set; }
}

JsonPackAutoMapper Attribute

Allows to specify additional member name mappings.

public class Sample1
{
    [JsonPackAutoMapper("OldFoo", "NewFoo")]
    public string Foo { get; set; }
}

Serialization Events

Add custom serialization events for OnSerializing and OnDeserialized.

public class Sample1
{
    public Foo { get; set; }

    [DataMember(Name = "Value")]
    private string _serializeOnlyValue;

    [OnSerializing]
    private void OnSerializing(StreamingContext context)
    {
        _serializeOnlyValue = "Sample1 was serialized";
    }

    [OnDeserialized]
    private void OnDeserialized(StreamingContext context)
    {
        // Override Foo with _serializeOnlyValue
        Foo = _serializeOnlyValue; 
    }
}

Json Pack Serializer Options

Create custom serialization and deserialization options for the JsonPackSerializer.

Resolver

var options = JsonPackSerializerOptions.WithResolver(WithImmutableDefaultResolver);

// Use as `DefaultOptions`.
JsonPackSerializer.DefaultOptions = options;

// Use as `parameter` on serialization calls.
var data = JsonPackSerializer.Serializate("FooBar", options);
var str = JsonPackSerializer.Deserializer<string>(data, options);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
.NET Framework net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 124 8/6/2023
1.0.0.2 506 6/3/2020
1.0.0-beta 376 5/31/2020