protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { var jsonProperty = base.CreateProperty(member, memberSerialization); if (!jsonProperty.PropertyType.IsPrimitive && jsonProperty.PropertyType != typeof(string) && jsonProperty.Readable) { var innerContract = innerResolver.ResolveContract(jsonProperty.PropertyType); if (innerContract is JsonObjectContract) { var objectContract = (JsonObjectContract)innerContract; var idProperty = objectContract.Properties.GetClosestMatchProperty(ResolvePropertyName("Id")); if (idProperty != null && idProperty.Readable && (innerResolver.ResolveContract(idProperty.PropertyType) is JsonPrimitiveContract)) { jsonProperty = new JsonProperty { PropertyName = ResolvePropertyName(member.Name + "Id"), Readable = true, PropertyType = idProperty.PropertyType, ValueProvider = new NestedValueProvider(jsonProperty.ValueProvider, idProperty.ValueProvider), }; } } // Possibly handle innerContract is JsonArrayContract? // Possibly handle innerContract is JsonDictionaryConract? } return(jsonProperty); }
public static string GetFields(Type type) { var resolver = new CamelCasePropertyNamesContractResolver(); var contract = (JsonObjectContract)resolver.ResolveContract(type); return(string.Join(",", contract.Properties.Select(p => p.PropertyName))); }
public override JsonContract ResolveContract(Type type) { var defaultContract = base.ResolveContract(type); if (defaultContract is JsonDictionaryContract) { return(defaultContract); } return(_camelCasePropertyNamesContractResolver.ResolveContract(type)); }
public void EnsureContractsShared() { CamelCasePropertyNamesContractResolver resolver1 = new CamelCasePropertyNamesContractResolver(); var contract1 = (JsonObjectContract)resolver1.ResolveContract(typeof(CamelCasePropertyNamesContractResolverTests)); CamelCasePropertyNamesContractResolver resolver2 = new CamelCasePropertyNamesContractResolver(); var contract2 = (JsonObjectContract)resolver2.ResolveContract(typeof(CamelCasePropertyNamesContractResolverTests)); Assert.IsTrue(ReferenceEquals(contract1, contract2)); var nt1 = resolver1.GetNameTable(); var nt2 = resolver2.GetNameTable(); Assert.IsTrue(ReferenceEquals(nt1, nt2)); }
public JsonContract ResolveContract(Type type) { var signalRAssembly = typeof(Microsoft.AspNetCore.SignalR.Infrastructure.Connection).GetTypeInfo().Assembly; if (type.GetTypeInfo().Assembly.Equals(signalRAssembly)) { var defaultContractSerializer = new DefaultContractResolver(); return(defaultContractSerializer.ResolveContract(type)); } else { var camelCaseContractResolver = new CamelCasePropertyNamesContractResolver(); return(camelCaseContractResolver.ResolveContract(type)); } }
private static JsonContract CreateContract(Type type) { var defaultContractResolver = new CamelCasePropertyNamesContractResolver(); return(defaultContractResolver.ResolveContract(type)); }