[Fork] questions/52721999/modelstate-error-newtonsoft-json-jsonserializationexception-cannot-populate-l?noredirect=1#comment92399021_52721999

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Reflection;
using System.Collections.Specialized;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;	

using NUnit.Framework; // Throws NUnit.Framework.AssertionException

//https://stackoverflow.com/questions/52721999/modelstate-error-newtonsoft-json-jsonserializationexception-cannot-populate-l?noredirect=1#comment92399021_52721999

	public class NotificationDTO
    {
        public long idCompanyService { get; set; }

        //public DocTypeEnum DocumentType { get; set; }

        public List<Tuple<string, byte[], System.Net.Mime.ContentType>> Attachments { get; set; }
    }

    class TestClass
    {
        public static void Test()
        {
			TestFull();
		}

        public static void TestFull()
        {
            var json = @"{""idCompanyService"":1234,""DocumentType"":1,""Attachments"":[{""Item1"":""wqere.pdf"",""Item2"":""TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="",""Item3"":{""Boundary"":null,""CharSet"":null,""MediaType"":""application/pdf"",""Name"":null,""Parameters"":[]}},{""Item1"":""ewqeqwe.xml"",""Item2"":""TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="",""Item3"":{""Boundary"":null,""CharSet"":null,""MediaType"":""text/xml"",""Name"":null,""Parameters"":[]}}]}";
			
			Console.WriteLine("Trying to deserialize a {0}: ", typeof(NotificationDTO));
			
			var dto = JsonConvert.DeserializeObject<NotificationDTO>(json);
			
			var json2 = JsonConvert.SerializeObject(dto, Formatting.Indented);
			
			Console.WriteLine(json2);			
		}

	}

	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: " + Environment.Version);
			Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}
;