1

I am trying to deserialize a JSON using Newtonsoft. However when deserializing a class "Opc.Ua.NodeId" to System.Type. It throws an exception "Error converting value "Opc.Ua.NodeId" to type 'System.Type'. Path 'SessionId.Type'.".

When there is System.Boolean or other System types, it converts without any error.

{
"Status": {
    "Type": "System.Boolean",
    "Value": true
},
"ServerId": {
    "Type": "System.String",
    "Value": {
        "olaBola": "ns=3;i=422970276"
    }
},
"SessionId": {
    "Type": "Opc.Ua.NodeId",
    "Value": {
        "Identifier": "ns=3;i=422970276"
    }
}

}

Class:

public class ExtendedAttribute
{
    public Type? Type { get; set; }

    public object? Value { get; set; }
}

Code:

JsonConvert.DeserializeObject<Dictionary<string, ExtendedAttribute>>(jsonText);

How to resolve this?

Amit Kumar
  • 591
  • 2
  • 8
  • 24

1 Answers1

0

you can try this code to convert a string that contains a class name to Type, and since Type is a reserved c# word, it is better for you to change the name of the property

public class ExtendedAttribute
{  
    [JsonIgnore]
    public Type? type { get; set; }

    public object? Value { get; set; }
    
    [JsonConstructor]
    public ExtendedAttribute(string type)
    {
        this.type=Type.GetType(type);
    }
}
Serge
  • 40,935
  • 4
  • 18
  • 45