Skip to content

Commit

Permalink
fix char[] array cannot be serialized or deserialized. #199
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Nov 9, 2023
1 parent 9aa51ce commit 9dac52f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { MemoryPackWriter } from "./MemoryPackWriter.js";
import { MemoryPackReader } from "./MemoryPackReader.js";
import { NoMarkByteEnum } from "./NoMarkByteEnum.js";
import { NumberedUShortEnum } from "./NumberedUShortEnum.js";
import { NestedObject } from "./NestedObject.js";
import { IMogeUnion } from "./IMogeUnion.js";
import { NoMarkByteEnum } from "./NoMarkByteEnum.js";
import { NumberedUShortEnum } from "./NumberedUShortEnum.js";
import { NestedObject } from "./NestedObject.js";
import { IMogeUnion } from "./IMogeUnion.js";

export class AllConvertableType {
myBool: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MemoryPackWriter } from "./MemoryPackWriter.js";
import { MemoryPackReader } from "./MemoryPackReader.js";
import { NoMarkByteEnum } from "./NoMarkByteEnum.js";
import { NestedObject } from "./NestedObject.js";
import { IMogeUnion } from "./IMogeUnion.js";
import { NoMarkByteEnum } from "./NoMarkByteEnum.js";
import { NestedObject } from "./NestedObject.js";
import { IMogeUnion } from "./IMogeUnion.js";

export class ArrayGenericsCheck {
array1: (NestedObject | null)[] | null;
Expand Down
2 changes: 1 addition & 1 deletion sandbox/SandboxWebApp/wwwroot/js/memorypack/Person.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MemoryPackWriter } from "./MemoryPackWriter.js";
import { MemoryPackReader } from "./MemoryPackReader.js";
import { Gender } from "./Gender.js";
import { Gender } from "./Gender.js";

export class Person {
id: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MemoryPackWriter } from "./MemoryPackWriter.js";
import { MemoryPackReader } from "./MemoryPackReader.js";
import { IMogeUnion } from "./IMogeUnion.js";
import { IMogeUnion } from "./IMogeUnion.js";

export class SampleUnion1 implements IMogeUnion {
myProperty: number | null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MemoryPackWriter } from "./MemoryPackWriter.js";
import { MemoryPackReader } from "./MemoryPackReader.js";
import { IMogeUnion } from "./IMogeUnion.js";
import { IMogeUnion } from "./IMogeUnion.js";

export class SampleUnion2 implements IMogeUnion {
myProperty: string | null;
Expand Down
3 changes: 2 additions & 1 deletion src/MemoryPack.Core/Internal/TypeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace MemoryPack.Internal;
internal static class TypeHelpers
{
static readonly MethodInfo isReferenceOrContainsReferences = typeof(RuntimeHelpers).GetMethod("IsReferenceOrContainsReferences")!;
static readonly MethodInfo unsafeSizeOf = typeof(Unsafe).GetMethod("SizeOf")!;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsReferenceOrNullable<T>()
Expand Down Expand Up @@ -67,7 +68,7 @@ static Cache()
if (!containsReference)
{
IsUnmanagedSZArray = true;
UnmanagedSZArrayElementSize = Marshal.SizeOf(elementType!);
UnmanagedSZArrayElementSize = (int)unsafeSizeOf.MakeGenericMethod(elementType!).Invoke(null, null)!;
}
}
#if NET7_0_OR_GREATER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace MemoryPack.Internal {
internal static class TypeHelpers
{
static readonly MethodInfo isReferenceOrContainsReferences = typeof(RuntimeHelpers).GetMethod("IsReferenceOrContainsReferences")!;
static readonly MethodInfo unsafeSizeOf = typeof(Unsafe).GetMethod("SizeOf")!;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsReferenceOrNullable<T>()
Expand Down Expand Up @@ -76,7 +77,7 @@ static Cache()
if (!containsReference)
{
IsUnmanagedSZArray = true;
UnmanagedSZArrayElementSize = Marshal.SizeOf(elementType!);
UnmanagedSZArrayElementSize = (int)unsafeSizeOf.MakeGenericMethod(elementType!).Invoke(null, null)!;
}
}
#if NET7_0_OR_GREATER
Expand Down
11 changes: 10 additions & 1 deletion tests/MemoryPack.Tests/ArrayFormatterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void ArrayTes()
Convert(xs).ToArray().Should().Equal(xs.ToArray());
}
{
var xs =new ReadOnlyMemory<int>(new int[] { 1, 10, 100 });
var xs = new ReadOnlyMemory<int>(new int[] { 1, 10, 100 });
Convert(xs).ToArray().Should().Equal(xs.ToArray());
}
//{
Expand All @@ -53,6 +53,15 @@ public void ArrayTes()
}
}

[Fact]
public void CharArrayTest()
{
var input = new[] { 'a', 'b', 'c' };
var bytes = MemoryPackSerializer.Serialize(input);
var output = MemoryPackSerializer.Deserialize<char[]>(bytes)!;
Assert.True(input.SequenceEqual(output));
}

[Theory]
[InlineData(100, 100, 10, 5)]
[InlineData(10, 20, 15, 5)]
Expand Down

0 comments on commit 9dac52f

Please sign in to comment.