0

I am trying to deserealize json:

Classes is generated by json2sharp

But it throws exception:

Element ": Data" contains data from a data type that maps to the name "http://schemas.datacontract.org/2004/07/TMSoft.CryptoDoc.Gu45:Gu45". The deserializer is no information of any type, are matched with the same name. Use DataContractResolver or add type that matches "Gu45", the list of known types, such as using KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.

How to fix it?

namespace json
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = System.IO.File.ReadAllText(@"D:\Json.txt");
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RootObject));
            var account = serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(json)));
        }
    }

    public class Branch
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }

    public class Direction
    {
        public object Code { get; set; }
        public string Name { get; set; }
    }

    public class Org
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }

    public class Wagon
    {
        public string Comment { get; set; }
        public object NoteTimeString { get; set; }
        public string Number { get; set; }
        public string OwnerCode { get; set; }
        public string StateString { get; set; }
    }

    public class Data
    {
        public string __type { get; set; }
        public Branch Branch { get; set; }
        public string DeliveryTimeString { get; set; }
        public Direction Direction { get; set; }
        public string EsrCode { get; set; }
        public int GU45Type { get; set; }
        public object ManeuverTime { get; set; }
        public string Number { get; set; }
        public Org Org { get; set; }
        public object ParkName { get; set; }
        public object ParkNum { get; set; }
        public string RailwayName { get; set; }
        public string RegistrationDateTimeString { get; set; }
        public object Span { get; set; }
        public string StationName { get; set; }
        public string TakingTimeString { get; set; }
        public object TrackNum { get; set; }
        public object TrackNumAddition { get; set; }
        public object WagonNote { get; set; }
        public List<Wagon> Wagons { get; set; }
    }

    public class Body
    {
        public Data Data { get; set; }
        public List<int> Signature { get; set; }
    }

    public class RootObject
    {
        public Body Body { get; set; }
        public int Revision { get; set; }
        public int State { get; set; }
        public string StateDate { get; set; }
        public string WosId { get; set; }
    }
}

Json.txt:

{
   "Body": {
      "Data": {
         "__type": "Gu45:#TMSoft.CryptoDoc.Gu45",
         "Branch": {
            "Code": "9898",
            "Name": "Place"
         },
         "DeliveryTimeString": "07.03.2014 10:00:00",
         "Direction": {
            "Code": null,
            "Name": "Test"
         },
         "EsrCode": "320007",
         "GU45Type": 2,
         "ManeuverTime": null,
         "Number": "1",
         "Org": {
            "Code": "1860",
            "Name": "Test"
         },
         "ParkName": null,
         "ParkNum": null,
         "RailwayName": "Test",
         "RegistrationDateTimeString": "07.03.2014",
         "Span": null,
         "StationName": "Test",
         "TakingTimeString": "07.03.2014 12:00:00",
         "TrackNum": null,
         "TrackNumAddition": null,
         "WagonNote": null,
         "Wagons": [
            {
               "Comment": "РЕМТ",
               "NoteTimeString": null,
               "Number": "44916989",
               "OwnerCode": "22",
               "StateString": "0"
            }
         ]
      },
      "Signature": [
         48,
         106
      ]
   },
   "Revision": 1966,
   "State": 2,
   "StateDate": "2014-03-07T12:48:03Z",
   "WosId": "15805729"
}
A191919
  • 3,422
  • 7
  • 49
  • 93
  • 1
    I tried your code with `Json.net` it works. `JsonConvert.DeserializeObject(json);`, so why don't u use it instead? – esiprogrammer Nov 24 '16 at 10:27
  • @esiprogrammer, team lead prohibits introduce new dependencies in the project. – A191919 Nov 24 '16 at 10:52
  • Json.NET is used everywhere. It's even the built-in serializer in ASP.NET Web API projects. The extra dependency is ` DataContractJsonSerializer`, a legacy class that isn't being actively updated precisely because even Microsoft uses Json.NET – Panagiotis Kanavos Nov 24 '16 at 12:07
  • The question here is, why do you want to *add* a dependency on a legacy class that even Microsoft doesn't use? – Panagiotis Kanavos Nov 24 '16 at 12:08
  • BTW your Json uses multiple non-standard formats for date and time strings, eg `07.03.2014 10:00:00`. That's not a good idea – Panagiotis Kanavos Nov 24 '16 at 12:10
  • @PanagiotisKanavos, The problem consists in the following as the "DataContractJsonSerializer" is already in the project team lead does not want to include Json.net relationship without any serious argument. – A191919 Nov 24 '16 at 12:34

1 Answers1

1

I had similar experience in my previous projects with DataContractJsonSerializer. it seems that "__type" field has a special meaning for DataContractJsonSerializer. This is one of the main reasons that I use Json.net

it matters only if it's the first property in the object but if it has any other positions it works and exceptions is not thrown. you can try it but changing __type position below the branch property in Json.txt. I don't know if it's the case and you can find a workaround. I haven't tried this solution but it worth to give it a try:

hope it helps

Community
  • 1
  • 1
esiprogrammer
  • 1,438
  • 1
  • 17
  • 22