public void GivenMethodAllowedWhenDeserializeCalledThenShouldDeserialize() { var nullableParameter = GetType().GetMethod( nameof(NullableParameter), BindingFlags.Public | BindingFlags.Static); var method = Expression.Call(nullableParameter, Expression.Constant(null)); var serialized = methodSerializer.Serialize(method, TestSerializer.GetDefaultState()); rulesConfig.RuleForMethod(selector => selector.ByMemberInfo(nullableParameter)); var deserialized = methodSerializer.Deserialize(serialized, TestSerializer.State); Assert.NotNull(deserialized); }
public void GivenOptionsIgnoreNullWhenMethodCallSerializedThenShouldDeserialize() { var nullableParameter = GetType().GetMethod( nameof(NullableParameter), BindingFlags.Public | BindingFlags.Static); var method = Expression.Call(nullableParameter, Expression.Constant(null)); var options = new JsonSerializerOptions { IgnoreNullValues = true, IgnoreReadOnlyProperties = true }; var serialized = TestSerializer .GetSerializedFragment <MethodExpr, MethodCallExpression>(method, options); rulesConfig.RuleForMethod(selector => selector.ByMemberInfo(nullableParameter)); var deserialized = methodSerializer.Deserialize(serialized, options.ToSerializationState()); Assert.NotNull(deserialized); }
/// <summary> /// Registers the default rules. /// </summary> /// <param name="rules">The <see cref="IRulesConfiguration"/>.</param> public static void RegisterDefaultRules(IRulesConfiguration rules) { if (primitiveTypes == null) { primitiveTypes = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes()) .Where(t => t.Namespace != null && t.Namespace.StartsWith(nameof(System)) && t.IsPrimitive && t != typeof(IntPtr) && t != typeof(UIntPtr)) .ToArray(); } if (collectionTypes == null) { var collectionNamespace = typeof(IEnumerable).Namespace; collectionTypes = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes()) .Where(t => t.Namespace != null && t.Namespace.StartsWith(collectionNamespace) && t.IsPublic && t.IsSerializable && !t.IsEnum && !typeof(Exception).IsAssignableFrom(t)) .ToArray(); } foreach (var primitive in primitiveTypes.Union(collectionTypes)) { rules.RuleForType(primitive); } foreach (var common in new[] { typeof(Math), typeof(Enumerable), typeof(Queryable), typeof(string), typeof(DateTime), typeof(TimeSpan), typeof(Guid), }) { rules.RuleForType(common); } rules.RuleForMethod( selector => selector.ByNameForType <MethodInfo, object>( nameof(object.ToString))); }