Skip to content

Commit

Permalink
Merge branch 'jhickson-multipledefinition'
Browse files Browse the repository at this point in the history
  • Loading branch information
aaubry committed Feb 19, 2018
2 parents 05b6e27 + 5796acf commit 1800935
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 89 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Please read [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

# Changelog

## Version 4.3.1

* Avoid definition of SerializationException (and some other types) on NET Standard

## Version 4.3.0

* Add support for (de)serialization of System.Type
Expand Down
5 changes: 2 additions & 3 deletions YamlDotNet/Core/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,8 @@ private bool ValueIsRepresentableInOutputEncoding(string value)
private bool IsUnicode(Encoding encoding)
{
return encoding is UTF8Encoding ||
encoding is UnicodeEncoding ||
encoding is UTF7Encoding ||
encoding is UTF8Encoding;
encoding is UnicodeEncoding ||
encoding is UTF7Encoding;
}

private void AnalyzeTag(string tag)
Expand Down
91 changes: 10 additions & 81 deletions YamlDotNet/Helpers/Portability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,48 +42,6 @@ internal static class StandardRegexOptions
#endif

#if NETSTANDARD1_3
/// <summary>
/// Mock UTF7Encoding to avoid having to add #if all over the place
/// </summary>
internal sealed class UTF7Encoding : System.Text.Encoding
{
public override int GetByteCount(char[] chars, int index, int count)
{
throw new NotImplementedException();
}

public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
throw new NotImplementedException();
}

public override int GetCharCount(byte[] bytes, int index, int count)
{
throw new NotImplementedException();
}

public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
{
throw new NotImplementedException();
}

public override int GetMaxByteCount(int charCount)
{
throw new NotImplementedException();
}

public override int GetMaxCharCount(int byteCount)
{
throw new NotImplementedException();
}
}

/// <summary>
/// Mock SerializableAttribute to avoid having to add #if all over the place
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
internal sealed class SerializableAttribute : Attribute { }

internal static class ReflectionExtensions
{
public static Type BaseType(this Type type)
Expand Down Expand Up @@ -209,6 +167,11 @@ public static TypeCode GetTypeCode(this Type type)
}
}

public static bool IsDbNull(this object value)
{
return value?.GetType()?.FullName == "System.DBNull";
}

public static Type[] GetGenericArguments(this Type type)
{
return type.GetTypeInfo().GenericTypeArguments;
Expand Down Expand Up @@ -283,33 +246,6 @@ public static bool IsInstanceOf(this Type type, object o)
}
}

internal enum TypeCode
{
Empty = 0,
Object = 1,
DBNull = 2,
Boolean = 3,
Char = 4,
SByte = 5,
Byte = 6,
Int16 = 7,
UInt16 = 8,
Int32 = 9,
UInt32 = 10,
Int64 = 11,
UInt64 = 12,
Single = 13,
Double = 14,
Decimal = 15,
DateTime = 16,
String = 18,
}

internal abstract class DBNull
{
private DBNull() {}
}

internal sealed class CultureInfoAdapter : CultureInfo
{
private readonly IFormatProvider _provider;
Expand Down Expand Up @@ -354,6 +290,11 @@ public static bool IsEnum(this Type type)
return type.IsEnum;
}

public static bool IsDbNull(this object value)
{
return value is DBNull;
}

/// <summary>
/// Determines whether the specified type has a default constructor.
/// </summary>
Expand Down Expand Up @@ -457,18 +398,6 @@ public static object ReadValue(this PropertyInfo property, object target)
#endif
}

#if NETSTANDARD1_3
namespace System.Runtime.Serialization
{
public class SerializationException : Exception
{
public SerializationException(string message) : base(message)
{
}
}
}
#endif

#if NET20
namespace System.Runtime.CompilerServices
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,15 @@ protected virtual void Traverse<TContext>(IObjectDescriptor value, IObjectGraphV
visitor.VisitScalar(value, context);
break;

case TypeCode.DBNull:
visitor.VisitScalar(new ObjectDescriptor(null, typeof(object), typeof(object)), context);
break;

case TypeCode.Empty:
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", typeCode));

default:
if (value.IsDbNull())
{
visitor.VisitScalar(new ObjectDescriptor(null, typeof(object), typeof(object)), context);
}

if (value.Value == null || value.Type == typeof(TimeSpan))
{
visitor.VisitScalar(value, context);
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Serialization/Utilities/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static object ChangeType(object value, Type destinationType, IFormatProvi
public static object ChangeType(object value, Type destinationType, CultureInfo culture)
{
// Handle null and DBNull
if (value == null || value is DBNull)
if (value == null || value.IsDbNull())
{
return destinationType.IsValueType() ? Activator.CreateInstance(destinationType) : null;
}
Expand Down
5 changes: 5 additions & 0 deletions YamlDotNet/YamlDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,10 @@
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard1.3' And '$(TargetFramework)' != 'net20'">
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
<PackageReference Include="System.Runtime.Serialization.Formatters">
<Version>4.3.0</Version>
</PackageReference>
</ItemGroup>

</Project>

0 comments on commit 1800935

Please sign in to comment.