Skip to content
Open
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1211,10 +1211,13 @@ There are a few restrictions on the types that can be generated. Among the primi
| `: ICollection<T>` | `T[] \| null` | Supports all `ICollection<T>` implemented type like `List<T>`
| `: ISet<T>` | `Set<T> \| null` | Supports all `ISet<T>` implemented type like `HashSet<T>`
| `: IDictionary<K,V>` | `Map<K, V> \| null` | Supports all `IDictionary<K,V>` implemented type like `Dictionary<K,V>`.
| `[MemoryPackable]` | `class` | Supports class only
| `[MemoryPackable]` | `class` | Supports class and struct (see notes below)
| `[MemoryPackUnion]` | `abstract class` |

`[GenerateTypeScript]` can only be applied to classes and is currently not supported by struct.
`[GenerateTypeScript]` supports both classes and structs. There are two serialization modes depending on the struct type:

- **Managed struct** (contains reference-type fields, e.g. `struct Foo { string Name; int Value; }`): uses the same object-header wire format as classes. The generated TypeScript `class` has non-nullable serialize signatures and omits the null-object-header write, matching the C# value-type semantics.
- **Unmanaged struct** (contains only unmanaged fields, e.g. `struct Point { int X; int Y; }`): serialized as a raw memory blit with no object header, matching `WriteUnmanaged`/`ReadUnmanaged` in C#. The generated TypeScript `class` writes each field individually at its correct byte offset (including any padding required by natural alignment), and deserialize always returns a non-nullable value.

### Configure import file extension and member name casing

Expand Down
14 changes: 13 additions & 1 deletion sandbox/SandboxWebApp/Controllers/MemoryPackController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MemoryPack;
using MemoryPack;
using Microsoft.AspNetCore.Mvc;

namespace SandboxWebApp.Controllers;
Expand Down Expand Up @@ -26,4 +26,16 @@ public NullableFloatTest PostNullableTest([FromBody] NullableFloatTest input)

return ret;
}

[Route("vector3")]
[HttpPost]
public Vector3 PostVector3([FromBody] Vector3 value) => value;

[Route("colorTag")]
[HttpPost]
public ColorTag PostColorTag([FromBody] ColorTag value) => value;

[Route("gameObject")]
[HttpPost]
public GameObject PostGameObject([FromBody] GameObject value) => value;
}
37 changes: 36 additions & 1 deletion sandbox/SandboxWebApp/Models.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MemoryPack;
using MemoryPack;
using System.ComponentModel;
using System.Security.Principal;

Expand Down Expand Up @@ -205,3 +205,38 @@ public partial class NullableFloatTest
public float? NullableFloat { get; set; }
public double? NullableDouble { get; set; }
}

[MemoryPackable]
[GenerateTypeScript]
public partial struct Vector3
{
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
}

[MemoryPackable]
[GenerateTypeScript]
public partial struct ColorTag
{
public string? Name { get; set; }
public int Code { get; set; }
}

[MemoryPackable]
[GenerateTypeScript]
public partial struct BoundingBox
{
public Vector3 Min { get; set; }
public Vector3 Max { get; set; }
}

[MemoryPackable]
[GenerateTypeScript]
public partial class GameObject
{
public string? Name { get; set; }
public Vector3 Position { get; set; }
public BoundingBox Bounds { get; set; }
public ColorTag Tag { get; set; }
}
9 changes: 8 additions & 1 deletion sandbox/SandboxWebApp/Pages/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@page
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
Expand All @@ -11,6 +11,9 @@
import { testNullableFloatWithNulls } from "./js/file.js";
import { hoge } from "./js/file.js";
import { huga } from "./js/file.js";
import { testVector3 } from "./js/file.js";
import { testColorTag } from "./js/file.js";
import { testStructs } from "./js/file.js";

//hoge();
//huga();
Expand All @@ -20,6 +23,10 @@
testNullableFloatWithValues();
testNullableFloatWithNulls();

testVector3();
testColorTag();
testStructs();

</script>

<div class="text-center">
Expand Down
93 changes: 92 additions & 1 deletion sandbox/SandboxWebApp/wwwroot/js/file.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading