'JSON.NET: Serialize json string property into json object

Is it possible to tell JSON.NET I have a string with JSON data? E.g. I have a class like this:

public class Foo
{
    public int Id;
    public string RawData;
}

which I use like this:

var foo = new Foo();
foo.Id = 5;
foo.RawData = @"{""bar"":42}";

which I want to be serialized like this:

{"Id":5,"RawData":{"bar":42}}

Basically I have a piece of unstructured variable-length data stored as JSON already, I need fully serialized object to contain this data as a part.

Thanks.

EDIT: Just to make sure it is understood properly, this is one-way serialization, i.e. I don't need it to deserialize back into same object; the other system shall process this output. I need content of RawData to be a part of JSON, not a mere string.



Solution 1:[1]

It is possible, that using JRaw could be more suitable and compact solution

See this post

Solution 2:[2]

You can use another property to deserialize the object property json.

public class Foo
{
    public int Id;
    public string RawData;
    private object thisObject;
    public object ThisObject 
    {
        get
        {
            return thisObject ?? (thisObject = JsonConvert.DeserializeObject<object>(RawData));
        }
    }        
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Gardiner
Solution 2 Tarek Ayna