Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore Advance when reading float/double with XmlBufferReader. #80274

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
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;

using System.Xml;
using Xunit;

namespace System.Runtime.Serialization.Tests
{
public static class DataContractSerializerHelper
{
internal static T SerializeAndDeserialize<T>(T value, string baseline, DataContractSerializerSettings settings = null, Func<DataContractSerializer> serializerFactory = null, bool skipStringCompare = false)
internal static T SerializeAndDeserialize<T>(T value, string baseline, DataContractSerializerSettings settings = null, Func<DataContractSerializer> serializerFactory = null, bool skipStringCompare = false, bool verifyBinaryRoundTrip = true)
{
DataContractSerializer dcs;
if (serializerFactory != null)
Expand All @@ -23,6 +23,7 @@ internal static T SerializeAndDeserialize<T>(T value, string baseline, DataContr
dcs = (settings != null) ? new DataContractSerializer(typeof(T), settings) : new DataContractSerializer(typeof(T));
}

T deserialized;
using (MemoryStream ms = new MemoryStream())
{
dcs.WriteObject(ms, value);
Expand All @@ -38,9 +39,37 @@ internal static T SerializeAndDeserialize<T>(T value, string baseline, DataContr
}

ms.Position = 0;
T deserialized = (T)dcs.ReadObject(ms);
deserialized = (T)dcs.ReadObject(ms);
}

if (verifyBinaryRoundTrip)
{
RoundTripBinarySerialization<T>(value, settings, serializerFactory);
}

return deserialized;
return deserialized;
}

internal static T RoundTripBinarySerialization<T>(T value, DataContractSerializerSettings settings = null, Func<DataContractSerializer> serializerFactory = null)
{
DataContractSerializer dcs;
if (serializerFactory != null)
{
dcs = serializerFactory();
}
else
{
dcs = (settings != null) ? new DataContractSerializer(typeof(T), settings) : new DataContractSerializer(typeof(T));
}

using (MemoryStream ms = new MemoryStream())
using (XmlDictionaryWriter binWriter = XmlDictionaryWriter.CreateBinaryWriter(ms))
{
dcs.WriteObject(binWriter, value);
binWriter.Flush();
ms.Position = 0;
XmlDictionaryReader binReader = XmlDictionaryReader.CreateBinaryReader(ms, XmlDictionaryReaderQuotas.Max);
return (T)dcs.ReadObject(binReader);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,18 @@ public long ReadInt64()
=> BitConverter.IsLittleEndian ? ReadRawBytes<long>() : BinaryPrimitives.ReverseEndianness(ReadRawBytes<long>());

public float ReadSingle()
=> BinaryPrimitives.ReadSingleLittleEndian(GetBuffer(sizeof(float), out int offset).AsSpan(offset, sizeof(float)));
{
float f = BinaryPrimitives.ReadSingleLittleEndian(GetBuffer(sizeof(float), out int offset).AsSpan(offset, sizeof(float)));
Advance(sizeof(float));
return f;
}

public double ReadDouble()
=> BinaryPrimitives.ReadDoubleLittleEndian(GetBuffer(sizeof(double), out int offset).AsSpan(offset, sizeof(double)));
{
double d = BinaryPrimitives.ReadDoubleLittleEndian(GetBuffer(sizeof(double), out int offset).AsSpan(offset, sizeof(double)));
Advance(sizeof(double));
return d;
}

public decimal ReadDecimal()
{
Expand Down