If you don't want to alter your class structure, you can use a custom JsonConverter
to achieve the result you want:
class WrappedStringConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(string));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
string s = (string)value;
JObject jo = new JObject(new JProperty("value", s));
jo.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
return (string)jo["value"];
}
}
To use it, add a [JsonConverter]
attribute to your string property like this:
class Foo
{
[JsonConverter(typeof(WrappedStringConverter))]
public string MyRootAttr { get; set; }
}
Round-trip demo here: https://dotnetfiddle.net/y1y5vb