Home > Back-end >  SerializationException: The data contract type <TypeName> could not be deserialized because mo
SerializationException: The data contract type <TypeName> could not be deserialized because mo

Time:01-22

Im getting an 'SerializationException' exception when deserializing data's from json file:

public static T Deserialize(string FilePath)
{
    using (FileStream FS = new FileStream(FilePath, FileMode.OpenOrCreate))
    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));

        return (T)serializer.ReadObject(FS) ?? null;
    }
}

and this is how im trying to deserialize:

ServiceEntity resultEntity = JsonHelper<ServiceEntity>.Deserialize(DestinationFilePath);

while (resultEntity != null)
{
    ListViewItem listViewItem = new ListViewItem(new string[] { resultEntity.ServiceName, resultEntity.ServiceStatus.ToString(), resultEntity.DisplayName });

    lvServices.Items.Add(listViewItem);
}

T is this:

[DataContract]
sealed class ServiceEntity
{
    [DataMember]
    public string DisplayName { get; set; }

    [DataMember]
    public string ServiceName { get; set; }

    [DataMember]
    public ServiceControllerStatus ServiceStatus { get; set; }
}

JSON data is : https://pastebin.com/M18GD8yh

CodePudding user response:

Your data is a single line of hundreds of JSON objects. The code reads the entire file and tries to deserialize it into a single C# object. You need to come up with a way to process each object separately. If you have control over the input file, it might be a good start to put the objects into an array, and deserialize that.

[{
    "DisplayName": "AarSvc_2dae3",
    "ServiceName": "AarSvc_2dae3",
    "ServiceStatus": 1
},{
    "DisplayName": "AMD External Events Utility",
    "ServiceName": "AMD External Events Utility",
    "ServiceStatus": 4
}, {
…
}]

CodePudding user response:

what you call a json, is not a valid json string, it is just a collection of json-like peaces in one string. But you can easily fix it

string json = File.ReadAllText(filePath;

json="[" json.Replace("}{","},{") "]";

after this you can parse it as Json Array

JArray services = JArray.Parse(json);

or you can deserialize it in your c# class

using Newtonsoft.Json;

List<ServiceEntity> services = JsonConvert.DeserializeObject<List<ServiceEntity>>(json);

or try your fancy serializer.

Output in a json format

[
  {
    "DisplayName": "AarSvc_2dae3",
    "ServiceName": "AarSvc_2dae3",
    "ServiceStatus": 1
  },
  {
    "DisplayName": "AMD External Events Utility",
    "ServiceName": "AMD External Events Utility",
    "ServiceStatus": 4
  },
  ....
]

UPDATE

If you have an acces to code to create a json from the c# object it is better to fix it there. IMHO you can use Newtonsonft.Json as a serializer . You can install it using a Nuget package. After this use this code

using Newtonsoft.Json;

 List<ServiceEntity> services = ... your code to create list of services;
     
var json = JsonConvert.SerializeObject(services);
     

if you need to save this json in a file

File.WriteAllText(FilePath, json);
  • Related