DeserializeObject only deserialize partly

85 views Asked by At

Given a class

public class ClassTest
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
    public D_type D { get; set; }
}

I am calling

JsonConvert.DeserializeObject<ClassTest>(json);

Which works fine with the D object of class D_type it correctly parses as I need it. However, I don't want JSON.net to deserialize D, I want to do this instead

public class ClassTest
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
    public string D_json { get; set; }
}

I want to keep D as the json string and not deserialize it.

I am going to use a different deserializer right afterwards to convert it to something else. It seems pointless to deserialize to an object D_type and then serialize it back to json, only to deserialize it again.

1

There are 1 answers

1
ParoX On BEST ANSWER

Seems I am able to do

public class ClassTest
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
    public dynamic D { get; set; }
}

and then just use

var obj = JsonConvert.DeserializeObject<ClassTest>(json);
var json = obj.D.ToString()

but I am still curious if there is a better way, specifically one that returns a real JSONObject or something I can add properties to