public void should_serialize_fields(string suffix, Type type, object value, string name) { var memberName = name + suffix; var @object = new SimpleTypeField(); @object.SetPropertyOrFieldValue(memberName, value); var result = Serialize.FormUrlEncoded(@object, x => x.IncludePublicFields().IncludeMembersWhen((m, o) => m.Name == memberName)); var json = "{0}={1}".ToFormat(memberName, type.IsBoolean() ? value.ToString().ToLower() : HttpUtility.UrlEncode(value.ToString())); result.ShouldEqual(json); }
// All other types should attempt to do a resolution via self binding. protected override bool TypeIsSelfBindable(Type service) { return !service.IsInterface && !service.IsAbstract && !service.ContainsGenericParameters && !service.IsEnum && !service.IsArray && !service.IsNumericType() && !service.IsString() && !service.IsBoolean() && !service.IsDateTime() && service != typeof(char); }
public void should_deserialize_typed_fields(string suffix, Type type, object value, string name) { var form = "{0}={1}".ToFormat(name + suffix, type.IsBoolean() ? value.ToString().ToLower() : HttpUtility.UrlEncode(value.ToString())); var result = Deserialize.FormUrlEncoded<SimpleTypeField>(form, x => x.IncludePublicFields()); result.ShouldNotBeNull(); result.ShouldBeType<SimpleTypeField>(); result.GetPropertyOrFieldValue(name + suffix).ShouldEqual(value); }
/// <summary> /// Assign the default value of a given Type /// </summary> /// <param name="assignment"><see cref="Assignment"/> to assign to</param> /// <param name="type">The type of which to create a default for</param> /// <returns>The <see cref="Assignment"/> to build on</returns> public static Assignment WithDefaultValue(this Assignment assignment, Type type) { if( type == typeof(Guid) ) return assignment.WithGuidEmpty(); else if (type.IsValueType) { if (type.IsNumericType()) return assignment.WithDefaultNumericValue(type); if (type.IsDate()) return assignment.WithDate(); if (type.IsBoolean()) return assignment.WithBoolean(); } else { if( type == typeof(string) )return assignment.WithLiteral("\"\""); return assignment.WithNullValue(); } return assignment; }
public void should_deserialize_typed_fields(string suffix, Type type, object value, string name) { var json = "{{ \"{0}\": {1} }}".ToFormat(name + suffix, type.IsNumeric() || type.IsBoolean() ? value.ToString().ToLower() : "\"" + value + "\""); var result = Deserialize.Json<SimpleTypeField>(json, x => x.IncludePublicFields()); result.ShouldNotBeNull(); result.ShouldBeType<SimpleTypeField>(); result.GetPropertyOrFieldValue(name + suffix).ShouldEqual(value); }
public void should_deserialize_string_fields(string suffix, Type type, object value, string name) { var jsonValue = type.IsNumeric() || type.IsBoolean() ? value.ToString().ToLower() : value; var json = $"{{ \"{name + suffix}\": \"{jsonValue}\" }}"; var result = Deserialize.Json<SimpleTypeField>(json, x => x.IncludePublicFields()); result.ShouldNotBeNull(); result.ShouldBeType<SimpleTypeField>(); result.GetPropertyOrFieldValue(name + suffix).ShouldEqual(value); }
public void should_serialize_fields(string suffix, Type type, object value, string name) { var memberName = name + suffix; var @object = new SimpleTypeField(); @object.SetPropertyOrFieldValue(memberName, value); var result = Serialize.Json(@object, x => x.IncludePublicFields().IncludeMembersWhen((m, o) => m.Name == memberName)); var json = "{{\"{0}\":{1}}}".ToFormat(memberName, type.IsNumeric() || type.IsBoolean() ? value.ToString().ToLower() : "\"" + value.ToString().Replace("/", "\\/") + "\""); result.ShouldEqual(json); }