Commit f3f4b790 authored by Ray Koopa's avatar Ray Koopa

Merge branch 'master' of https://github.com/Syroot/BinaryData

parents d1ff29ea 46134da8
......@@ -38,6 +38,8 @@ namespace Syroot.BinaryData.Serialization.UnitTest
Assert.AreEqual(origInstance.Text, readInstance.Text);
Assert.AreEqual(origInstance.Struct.Green, readInstance.Struct.Green);
Assert.AreEqual(origInstance.Struct.Red, readInstance.Struct.Red);
Assert.AreEqual(origInstance.TextWithDefaultSettings, readInstance.TextWithDefaultSettings);
Assert.AreEqual(origInstance.TextWithCustomEncoding, readInstance.TextWithCustomEncoding);
}
[TestMethod]
......@@ -83,6 +85,8 @@ namespace Syroot.BinaryData.Serialization.UnitTest
[BinaryMember(Order = 1)] public byte Y = 0x44;
[BinaryMember(Order = 2, StringCoding = StringCoding.Int32CharCount)] public string Text = "Hello, Test!";
[BinaryMember(Order = 3)] public TestStruct Struct = new TestStruct { Green = 0x0000FF00, Red = 0xFF000000 };
[BinaryMember(Order = 4)] public string TextWithDefaultSettings = "I'm using some accents: òàùèìéééé";
[BinaryMember(Order = 5, StringEncoding = "iso-8859-1")] public string TextWithCustomEncoding = "I'm using some accents: òàùèìéééé";
}
private struct TestStruct
......
using System;
using System.Collections;
using System.Text;
namespace Syroot.BinaryData
{
/// <summary>
......@@ -47,7 +48,13 @@ namespace Syroot.BinaryData
/// <see cref="StringCoding.VariableByteCount"/>.
/// </summary>
public StringCoding StringCoding { get; set; }
/// <summary>
/// Gets or sets the encoding for <see cref="String"/> members. Defaults to
/// <see cref="Encoding.UTF8"/> (utf-8).
/// </summary>
public string StringEncoding { get; set; }
/// <summary>
/// Gets or sets the number of elements to read or write. Required for <see cref="IEnumerable"/> members or
/// strings when <see cref="StringCoding"/> is <see cref="StringCoding.Raw"/>.
......
......@@ -3,7 +3,8 @@ using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace Syroot.BinaryData
{
public static partial class StreamExtensions
......@@ -92,7 +93,7 @@ namespace Syroot.BinaryData
if (attribute.StringCoding == StringCoding.Raw)
return stream.ReadString(attribute.Length);
else
return stream.ReadString(attribute.StringCoding, converter: converter);
return stream.ReadString(attribute.StringCoding, encoding: attribute.StringEncoding != null ? Encoding.GetEncoding(attribute.StringEncoding) : null, converter: converter);
}
else if (type.IsEnumerable())
throw new InvalidOperationException("Multidimensional arrays cannot be read directly.");
......
......@@ -4,7 +4,8 @@ using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace Syroot.BinaryData
{
public static partial class StreamExtensions
......@@ -66,7 +67,7 @@ namespace Syroot.BinaryData
if (value == null)
return;
else if (type == typeof(String))
stream.Write((String)value, attribute.StringCoding, converter: converter);
stream.Write((String)value, attribute.StringCoding, encoding: attribute.StringEncoding != null ? Encoding.GetEncoding(attribute.StringEncoding) : null, converter: converter);
else if (type.TryGetEnumerableElementType(out Type elementType))
{
foreach (object element in (IEnumerable)value)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment