You can create a CustomCreationConverter to do what you need to do. Here's a sample (rather ugly, but demonstrates how you may want to go about this):
namespace JsonConverterTest1
{
public class Mapped
{
private Dictionary<string, object> _theRest = new Dictionary<string, object>();
public int One { get; set; }
public int Two { get; set; }
public Dictionary<string, object> TheRest { get { return _theRest; } }
}
public class MappedConverter : CustomCreationConverter<Mapped>
{
public override Mapped Create(Type objectType)
{
return new Mapped();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var mappedObj = new Mapped();
var objProps = objectType.GetProperties().Select(p => p.Name.ToLower()).ToArray();
//return base.ReadJson(reader, objectType, existingValue, serializer);
while (reader.Read())
{
if (reader.TokenType == JsonToken.PropertyName)
{
string readerValue = reader.Value.ToString().ToLower();
if (reader.Read())
{
if (objProps.Contains(readerValue))
{
PropertyInfo pi = mappedObj.GetType().GetProperty(readerValue, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
var convertedValue = Convert.ChangeType(reader.Value, pi.PropertyType);
pi.SetValue(mappedObj, convertedValue, null);
}
else
{
mappedObj.TheRest.Add(readerValue, reader.Value);
}
}
}
}
return mappedObj;
}
}
public class Program
{
static void Main(string[] args)
{
string json = "{'one':1, 'two':2, 'three':3, 'four':4}";
Mapped mappedObj = JsonConvert.DeserializeObject<Mapped>(json, new MappedConverter());
Console.WriteLine(mappedObj.TheRest["three"].ToString());
Console.WriteLine(mappedObj.TheRest["four"].ToString());
}
}
}
So the output of mappedObj after you deserialize the JSON string will be an object with its One
and Two
properties populated, and everything else put into the Dictionary
. Granted, I hard-coded the One and Two values as int
s, but I think this demonstrates how you'd go about this.
I hope this helps.
EDIT: I updated the code to make it more generic. I didn't fully test it out, so there may some cases where it fails, but I think it gets you most of the way there.