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;
                }





Check out our Microsoft Dynamics 365 training challenge to become a professional in Dynamics 365. With the perfect plan and guidance, we will assist you at every step to reach your goal.




Recent blog posts:

Microsoft Teams integration with Customer Engagement Apps in Dynamics 365

Microsoft Dynamics 365 Team Member License - Limitations

Insert Knowledge Article Templates- Dynamics 365 Wave 2 feature