You can use the UntypedToTypedValueConverter
from JSON.net (de)serialize untyped property, with one difference - you need to apply it to the array items rather than the array itself using [JsonProperty(ItemConverterType = typeof(UntypedToTypedValueConverter))]
, e.g.:
public class RootObject
{
[JsonProperty(ItemConverterType = typeof(UntypedToTypedValueConverter))]
public object [] Items { get; set; }
}
This applies the converter to the entries in the array rather than to the array itself. Then serialize and deserialize with JsonSerializerSettings.TypeNameHandling = TypeNameHandling.Auto
, e.g.:
var root = new RootObject { Items = new object[] { 1, 1L, int.MaxValue, long.MaxValue } };
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };
var json = JsonConvert.SerializeObject(root, settings);
var root2 = JsonConvert.DeserializeObject<RootObject>(json, settings);
Sample fiddle.