Comments (3)
@neuecc Proposal: Add API to get an index (or id) of a formatter and a formatter by index (or id):
public class KnownObjectFormatter : MemoryPackFormatter<Object?> {
public override void Serialize<TBufferWriter>(
ref MemoryPackWriter<TBufferWriter> writer,
scoped ref Object? value) {
if (value == null) {
writer.WriteNullObjectHeader();
return;
}
Type type = value.GetType();
ushort index = MemoryPackFormatterProvider.GetFormatterIndex(type);
var formatter = MemoryPackFormatterProvider.GetFormatter(type);
writer.WriteValue(index);
formatter.Serialize(writer, value);
}
public override void Deserialize(ref MemoryPackReader reader, scoped ref Object? value) {
if (!reader.TryReadObjectHeader(out var count)) {
value = null;
return;
}
ushort index = reader.ReadValue<ushort>();
var formatter = MemoryPackFormatterProvider.GetFormatterByIndex();
value = formatter.Deserialize(reader);
}
}
It is slower than when using static types, but not that much. I would expect it to be much faster that when having to wrap every value into an object on the heap. And there is no need to write boiler plate wrapper classes for primitives.
from memorypack.
I helped myself with the following formatter:
public class KnownObjectFormatter : MemoryPackFormatter<Object?> {
public class KnownObjectFormatterException(string message) : Exception(message);
private static readonly Bictionary<Type, ushort> RegisteredTypes = new Bictionary<Type, ushort>();
public static void RegisterType(Type type) {
if (!RegisteredTypes.ContainsKeyForward(type)) {
ushort index = (ushort)RegisteredTypes.Count;
RegisteredTypes.Add(type, index);
}
}
public override void Serialize<TBufferWriter>(
ref MemoryPackWriter<TBufferWriter> writer,
scoped ref Object? value) {
if (value == null) {
writer.WriteNullObjectHeader();
return;
}
Type type = value.GetType();
if (!RegisteredTypes.TryGetValueForward(type, out ushort index)) {
throw new KnownObjectFormatterException($"Type {type} not registered");
}
writer.WriteUnmanaged<ushort>(index);
var formatter = writer.GetFormatter(type);
formatter.Serialize(ref writer, ref value);
}
public override void Deserialize(ref MemoryPackReader reader, scoped ref Object? value) {
if (reader.PeekIsNull()) {
reader.Advance(1);
value = null;
return;
}
ushort index = reader.ReadUnmanaged<ushort>();
if (!RegisteredTypes.TryGetValueReverse(index, out var type)) {
throw new KnownObjectFormatterException($"No type with index {index} registered");
}
var formatter = reader.GetFormatter(type!);
formatter.Deserialize(ref reader, ref value);
}
public class Attribute : MemoryPackCustomFormatterAttribute<object?> {
private static readonly KnownObjectFormatter Formatter = new KnownObjectFormatter();
public override IMemoryPackFormatter<object?> GetFormatter() {
return Formatter;
}
}
}
That boils down to usage:
KnownObjectFormatter.RegisterType(typeof(int));
KnownObjectFormatter.RegisterType(typeof(int?));
KnownObjectFormatter.RegisterType(typeof(int[]));
KnownObjectFormatter.RegisterType(typeof(bool));
KnownObjectFormatter.RegisterType(typeof(bool?));
KnownObjectFormatter.RegisterType(typeof(bool[]));
KnownObjectFormatter.RegisterType(typeof(string));
KnownObjectFormatter.RegisterType(typeof(string[]));
...
KnownObjectFormatter.RegisterType(typeof(MyCustomType));
KnownObjectFormatter.RegisterType(typeof(MyCustomType?));
KnownObjectFormatter.RegisterType(typeof(MyCustomType[]));
...
It still is tedious to do that for every possible already mapped type, but it at least is better than having to write wrapper classes.
So proposal as changed to:
- Expose types defined in
src/MemoryPack.Core/MemoryPackFormatterProvider.WellknownTypes.cs
to be consumed by such formatter (above code)
from memorypack.
Related Issues (20)
- Generics issue.
- When will memorypack support .net8?
- How to get written count when use BrotliCompressor? HOT 2
- .Net 8 NativeAOT Crash when serialize any type contains generic property. HOT 1
- BrotliDecompressor take too many CPU HOT 5
- NativeAOT compatibility: publish warnings and how to make MemoryPack fully NativeAOT compatible?
- C# Generic class support HOT 2
- Unexpected sequence reached end exception HOT 4
- Serializing nullable fields takes up more bytes HOT 5
- MemoryPackStreamingSerializer.DeserializeAsync doesn't work HOT 1
- UnmanagedType Specialization HOT 1
- Question: Does MemoryPackStreamingSerializer.DeserializeAsync function works like "lazy load"? HOT 2
- Bug : Generic Struct with Unmanaged Constraint
- MemoryPack.Generator.Roslyn3.dll is seems to be old HOT 1
- MemoryPack does not work in LinqPad HOT 2
- inherit class in another project, the parent class properties will not generate in source generator code! HOT 1
- VersionTolerant serialization of abstract class fails to compile HOT 1
- memorypack is missing NuGet package README file
- MemoryPack not fully compatible with Visual Studio "Find references" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from memorypack.