Learn how to use serialize and de-serialize without using NewtonSoft DLL 


Problem

Plugin Registration Tool accepts only one assembly (dll) to upload. If you have third party dependency assemblies like Newtonsoft, you have to combine with some assembly merging tools such as ILMerge.exe.


Solution

You can directly use below static class to serialize and de-serialize objects without referencing  Newtonsoft dll.

Don't forget to use .Net Namespace System.Runtime.Serialization.Json in the using section.

 Here TType is your model class for that object.

  public static class JSONSerializer<TType> where TType : class
        {
            /// <summary>
            /// Serializes an object to JSON
            /// </summary>
            public static string Serialize(TType instance)
            {
                var serializer = new DataContractJsonSerializer(typeof(TType));
                using (var stream = new MemoryStream())
                {
                    serializer.WriteObject(stream, instance);
                    return Encoding.Default.GetString(stream.ToArray());
                }
            }

            /// <summary>
            /// DeSerializes an object from JSON
            /// </summary>
            public static TType DeSerialize(string json)
            {
                using (var stream = new MemoryStream(Encoding.Default.GetBytes(json)))
                {
                    var serializer = new DataContractJsonSerializer(typeof(TType));
                    return serializer.ReadObject(stream) as TType;
                }
            }
        }