Example #1
0
            protected override JsonObjectContract CreateObjectContract(Type objectType)
            {
                var contract       = base.CreateObjectContract(objectType);
                var objectTypeInfo = objectType.GetTypeInfo();

                if (TypeData <ExtensibleObject> .TypeInfo.IsAssignableFrom(objectTypeInfo))
                {
                    contract.ExtensionDataGetter = o => ((DirtyDictionary <string, object>)((ExtensibleObject)o).ExtensionData).GetDirtyItems().Select(p => new KeyValuePair <object, object>(p.Key, p.Value));
                    contract.ExtensionDataSetter = (o, k, v) => ((ExtensibleObject)o).ExtensionData[k] = v;
                    if (TypeData <DirtyExtensibleObject> .TypeInfo.IsAssignableFrom(objectTypeInfo))
                    {
                        var idPropertyName       = DirtyExtensibleObject.GetIdPropertyName(objectTypeInfo);
                        var backingFieldTypeInfo = GetBackingFieldInfo(objectType, idPropertyName)?.FieldInfo.FieldType.GetTypeInfo();
                        idPropertyName = CamelCaseNamingStrategy.GetPropertyName(idPropertyName, false);
                        var idProperty = contract.Properties.GetClosestMatchProperty(idPropertyName);
                        if (idProperty != null && (backingFieldTypeInfo == null || !backingFieldTypeInfo.IsGenericType || backingFieldTypeInfo.IsGenericTypeDefinition || backingFieldTypeInfo.GetGenericTypeDefinition() != TypeData.OpenNeverSerializeValueType))
                        {
                            idProperty.ShouldSerialize = o => ((IIdentifiable)o).Id != null;
                        }

                        ReadEntityAttributes(objectTypeInfo, contract);
                    }
                }
                return(contract);
            }
Example #2
0
            protected override JsonObjectContract CreateObjectContract(Type objectType)
            {
                var contract       = base.CreateObjectContract(objectType);
                var objectTypeInfo = objectType.GetTypeInfo();

                if (TypeData <ExtensibleObject> .TypeInfo.IsAssignableFrom(objectTypeInfo))
                {
                    contract.ExtensionDataGetter = o => ((DirtyDictionary <string, object>)((ExtensibleObject)o).ExtensionData).GetDirtyItems().Select(p => new KeyValuePair <object, object>(p.Key, p.Value));
                    contract.ExtensionDataSetter = (o, k, v) => ((ExtensibleObject)o).ExtensionData[k] = v;
                    if (TypeData <DirtyExtensibleObject> .TypeInfo.IsAssignableFrom(objectTypeInfo))
                    {
                        var idPropertyName       = DirtyExtensibleObject.GetIdPropertyName(objectTypeInfo);
                        var backingFieldTypeInfo = GetBackingFieldInfo(objectType, idPropertyName)?.FieldInfo.FieldType.GetTypeInfo();
                        idPropertyName = CamelCaseNamingStrategy.GetPropertyName(idPropertyName, false);
                        var property = contract.Properties.GetClosestMatchProperty(idPropertyName);
                        if (property != null && (backingFieldTypeInfo == null || !backingFieldTypeInfo.IsGenericType || backingFieldTypeInfo.IsGenericTypeDefinition || backingFieldTypeInfo.GetGenericTypeDefinition() != TypeData.OpenNeverSerializeValueType))
                        {
                            property.ShouldSerialize = o => ((IIdentifiable)o).Id != null;
                        }

                        var entityAttribute = objectTypeInfo.GetCustomAttribute <EntityAttribute>(false);
                        if (entityAttribute != null && !string.IsNullOrEmpty(entityAttribute.PropertiesToAlwaysSerialize))
                        {
                            var propertiesToAlwaysSerialize = entityAttribute.PropertiesToAlwaysSerialize.Split(',');
                            foreach (var propertyToAlwaysSerialize in propertiesToAlwaysSerialize)
                            {
                                property = contract.Properties.GetClosestMatchProperty(propertyToAlwaysSerialize);
                                var valueProvider = property.ValueProvider;
                                property.ShouldSerialize = o => valueProvider.GetValue(o) != null;
                            }
                        }
                    }
                }
                return(contract);
            }