protected override JsonObjectContract CreateObjectContract(Type objectType) { var contract = base.CreateObjectContract(objectType); if (typeof(JsonModel).IsAssignableFrom(objectType)) { contract.ExtensionDataGetter = instance => { return(instance is JsonModel model && model.ExtensionData != null ? model.ExtensionData.Select(x => KeyValuePair.Create(x.Key as object, (x.Value as DefaultJsonToken).Token as object)) : null); }; var skippedProperties = objectType.GetCustomAttribute <JsonSkippedPropertiesAttribute>()?.Properties; contract.ExtensionDataSetter = (instance, key, value) => { if (instance is not JsonModel model) { return; } if (skippedProperties != null && Array.IndexOf(skippedProperties, key) != -1) { return; } IJsonToken token; if (value == null) { token = null; } else if (value is JToken jToken) { token = DefaultJsonToken.Create(jToken, _serializer.UnderlyingSerializer); } else { token = DefaultJsonToken.Create(JToken.FromObject(value, _serializer.UnderlyingSerializer), _serializer.UnderlyingSerializer); } model.ExtensionData.Add(key, token); }; } return(contract); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var token = JToken.ReadFrom(reader); return(DefaultJsonToken.Create(token, serializer)); }