Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Moryx/Serialization/EntryConvert/EntryConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,14 @@ private static EntryValue CreateEntryValue(PropertyInfo property, ICustomSeriali
entryValue.Default = defaultAttribute.Value.ToString();
else if (entryValue.Possible != null && entryValue.Possible.Length >= 1)
entryValue.Default = entryValue.Possible[0];
else if (property.PropertyType.IsValueType)
entryValue.Default = Activator.CreateInstance(property.PropertyType).ToString();

// Value types should have the default value as current value
else if (property.PropertyType.IsValueType)
{
var underlyingType = Nullable.GetUnderlyingType(property.PropertyType);
entryValue.Default = underlyingType != null
? Activator.CreateInstance(underlyingType).ToString()
: Activator.CreateInstance(property.PropertyType).ToString();
}
// Value types should have the default value as current value
if (ValueOrStringType(property.PropertyType))
entryValue.Current = ConvertToString(entryValue.Default, customSerialization.FormatProvider);

Expand Down Expand Up @@ -312,7 +316,7 @@ public static Entry EncodeObject(object instance, ICustomSerialization customSer
convertedProperty.Value.Current = ConvertToString(subEntry.Value.Current, customSerialization.FormatProvider);
convertedProperty.SubEntries = subEntry.SubEntries;
break;
case EntryValueType.Exception:
case EntryValueType.Exception:
convertedProperty.Value.Current = ExceptionPrinter.Print((Exception)value);
break;
case EntryValueType.Stream:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static EntryValueType TransformType(Type propertyType)
else if (propertyType == typeof(Double))
{
valueType = EntryValueType.Double;
}
}
else if (propertyType.IsEnum)
{
valueType = EntryValueType.Enum;
Expand Down
5 changes: 5 additions & 0 deletions src/Tests/Moryx.Tests/Serialization/DummyClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public enum DummyEnum
ValueA,
ValueB
}

public class NullablePropertiesClass
{
public int? Value { get; set; } = 0;
}
}
8 changes: 8 additions & 0 deletions src/Tests/Moryx.Tests/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,14 @@ public void MemoryStreamDecodeReuseCurrentStreamInitSizeIsGreaterThanNewData()
Assert.AreSame(streamInstanceToCheck, targetStreamDummy.MemoryStream);
}

[Test(Description = "Testing nullable properties")]
public void NullableProperty()
{
var nullablePropertiesObject = new NullablePropertiesClass();

Assert.DoesNotThrow(() => EntryConvert.EncodeObject(nullablePropertiesObject));
}

[Test(Description = "Encodes a FileStream")]
public void FileStreamEncode()
{
Expand Down
Loading