forked from Cysharp/MemoryPack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
598 lines (421 loc) · 13 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#pragma warning disable CS8600
#pragma warning disable CS0169
using MemoryPack;
using MemoryPack.Compression;
using MemoryPack.Formatters;
using MemoryPack.Streaming;
using Samples;
using SandboxConsoleApp;
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.IO;
using System.IO.Compression;
using System.IO.Pipelines;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml.Linq;
Console.WriteLine("---");
//var bin = MemoryPackSerializer.Serialize("hogehoge");
//var takotako = MemoryPackSerializer.Deserialize<string>(bin);
//Console.WriteLine(takotako);
// ---
//var arrayBufferWriter = new ArrayBufferWriter<byte>();
//var writer = new ArrayBufferWriter<byte>();
//var seq = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes("hogehogehugahugahage"));
// PipeWriter.Create(
//PipeWriter.Create().AsStream();
//Write(seq, writer);
//var compressed = writer.WrittenMemory;
//var stream = new BrotliStream(new MemoryStream(compressed.ToArray()), CompressionMode.Decompress, false);
//var dest = new byte[1024];
//var len = stream.Read(dest);
//var len2 = stream.Read(dest);
//var ok = BrotliDecoder.TryDecompress(compressed.Span, dest, out var written);
//var seq2 = new ReadOnlySequence<byte>(compressed);
//var writer2 = new ArrayBufferWriter<byte>();
//Read(seq2, writer2);
//MemoryPackSerializer.Serialize(brotli, "hogehogehugahuga", MemoryPackSerializeOptions.Default);
//var foobarbaz = brotli.ToArray();
//var dest2 = new byte[10];
//var decoder = new BrotliDecoder();
//var status = OperationStatus.DestinationTooSmall;
//while (status == OperationStatus.DestinationTooSmall)
//{
// status = decoder.Decompress(tako, dest2, out var consumed, out var written);
//}
//Console.WriteLine(status);
//var status = BrotliDecoder.TryDecompress(tako, dest2, out var written2);
//Console.WriteLine(status + ":" + written2);
//var hogehoge = dest2.AsMemory(0, written2);
//var tako2 = MemoryPackSerializer.Deserialize<string>(hogehoge.Span);
//Console.WriteLine(foobarbaz.SequenceEqual(tako));
//BrotliCompression.
//var decoder = new BrotliDecoder();
//decoder.Decompress(
//public class DecompressReadOnlySequence
//{
// ReadOnlySequence<byte> buffer;
// BrotliDecoder decoder;
// public DecompressReadOnlySequence()
// {
// // buffer.FirstSpan.
// BrotliEncoder.GetMaxCompressedLength
// //decoder.Decompress(
// }
//}
//brotli.Dispose();
//var written = arrayBufferWriter.WrittenMemory;
//new BrotliStream(
//BrotliDecoder.TryDecompress(written,
//// new BrotliDecoder().Decompress(
////encoder.Compress(, , , , true);
//encoder.Dispose();
[MemoryPackable]
[GenerateTypeScript]
public partial class FooBarBaz
{
//public int[] MyPropertyArray { get; set; } = default!;
//public int[] MyPropertyArray { get; set; } = default!;
public Hoge HogeDozo { get; set; }
public byte[]? BytesProp { get; set; }
public string? YoStarDearYomoda { get; private set; }
public int[] MyPropertyArray { get; set; } = default!;
public int[][] MyPropertyArray2 { get; set; } = default!;
public int? MyProperty4 { get; set; }
public Dictionary<int, List<int?>> Dictman { get; set; } = default!;
public HashSet<int> SetMan { get; set; } = default!;
public Sonota1 SonotaProp { get; set; } = default!;
public Guid guid { get; set; } = default!;
public Guid? NullTtoguid { get; set; } = default!;
public DateTime dtt { get; set; } = default!;
public Sonota2 SonotaProp2 { get; set; } = default!;
// public Huga? Nuga { get; set; } = default!;
// TODO: check GUID, Date
//public int MyProperty1 { get; set; }
//public int? MyProperty2 { get; set; }
//public Hoge? MyProperty3 { get; set; }
}
public enum Hoge : sbyte
{
Huga,
Yo,
SOSOSO
}
public enum Huga : int
{
ZZZ = 10,
NONUM,
HOKEPON
}
[MemoryPackable]
[MemoryPackUnion(0, typeof(SampleUnion1))]
[MemoryPackUnion(1, typeof(SampleUnion2))]
[GenerateTypeScript]
public partial interface IMogeUnion
{
}
[MemoryPackable]
[GenerateTypeScript]
public partial class SampleUnion1 : IMogeUnion
{
public int? MyProperty { get; set; }
}
[MemoryPackable]
[GenerateTypeScript]
public partial class SampleUnion2 : IMogeUnion
{
public string? MyProperty { get; set; }
}
[MemoryPackable(GenerateType.Object)]
[GenerateTypeScript]
public partial class Sonota1
{
// public NoSerializableObject? MyProperty { get; set; }
public int HokuHoku { get; set; }
}
public class NoSerializableObject
{
}
[MemoryPackable(SerializeLayout.Explicit)]
[GenerateTypeScript]
public partial class Sonota2
{
[MemoryPackOrder(1)]
public int MyProperty1 { get; set; }
[MemoryPackOrder(0)]
public int MyProperty2 { get; set; }
}
[MemoryPackable(GenerateType.Object, SerializeLayout.Explicit)]
public partial class Sonota3
{
[MemoryPackOrder(0)]
public int MyProperty { get; set; }
}
//var person = new Person();
//var bin = MemoryPackSerializer.Serialize(person);
//// overwrite data to existing instance.
//MemoryPackSerializer.Deserialize(bin, ref person);
//internal void WriteCore(ReadOnlySpan<byte> buffer, bool isFinalBlock = false)
//{
// if (_mode != CompressionMode.Compress)
// throw new InvalidOperationException(SR.BrotliStream_Decompress_UnsupportedOperation);
// EnsureNotDisposed();
// OperationStatus lastResult = OperationStatus.DestinationTooSmall;
// Span<byte> output = new Span<byte>(_buffer);
// while (lastResult == OperationStatus.DestinationTooSmall)
// {
// int bytesConsumed;
// int bytesWritten;
// lastResult = _encoder.Compress(buffer, output, out bytesConsumed, out bytesWritten, isFinalBlock);
// if (lastResult == OperationStatus.InvalidData)
// throw new InvalidOperationException(SR.BrotliStream_Compress_InvalidData);
// if (bytesWritten > 0)
// _stream.Write(output.Slice(0, bytesWritten));
// if (bytesConsumed > 0)
// buffer = buffer.Slice(bytesConsumed);
// }
//}
//internal static partial class BrotliUtils
//{
// public const int WindowBits_Min = 10;
// public const int WindowBits_Default = 22;
// public const int WindowBits_Max = 24;
// public const int Quality_Min = 0;
// public const int Quality_Default = 4;
// public const int Quality_Max = 11;
// public const int MaxInputSize = int.MaxValue - 515; // 515 is the max compressed extra bytes
// internal static int GetQualityFromCompressionLevel(CompressionLevel compressionLevel) =>
// compressionLevel switch
// {
// CompressionLevel.NoCompression => Quality_Min,
// CompressionLevel.Fastest => 1,
// CompressionLevel.Optimal => Quality_Default,
// CompressionLevel.SmallestSize => Quality_Max,
// _ => throw new ArgumentException(SR.ArgumentOutOfRange_Enum, nameof(compressionLevel))
// };
//}
[MemoryPackable(GenerateType.Collection)]
public partial class ListGenerics<T> : List<T>
{
}
[MemoryPackable]
public partial class Person
{
public int Age { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
}
[MemoryPackable]
public partial record struct FooStruct(int x, int y);
[MemoryPackable]
public partial class Nu
{
public UnionType? XXX;
}
[MemoryPackable]
[MemoryPackUnion(0, typeof(A))]
public partial interface UnionType
{
}
[MemoryPackable]
public partial class A : UnionType
{
}
[MemoryPackable]
public partial class Foo
{
//Foo(int x)
//{
//}
//public Foo()
//{
//}
}
[MemoryPackable]
public partial class MonoMono
{
public FooBarFruit Yey { get; set; } = default!;
}
public enum FooBarFruit
{
APple, orange, grape
}
#pragma warning disable CS8618
[MemoryPackable]
public partial class HogeHoge
{
public BigInteger P1;
public Version P2;
public Uri P3;
public TimeZoneInfo P4;
public BitArray P5;
public StringBuilder P6;
public Type P7;
public int[,] P8;
public int[,,] P9;
public int[,,,] P10;
// ng
// public int[,,,,] A5;
// generics
public KeyValuePair<int, int> P11;
public Lazy<int> P12;
public Nullable<int> P13;
// collecition
public ArraySegment<int> P14;
public Memory<int> P15;
public ReadOnlyMemory<int> P16;
public ReadOnlySequence<int> P17;
public List<int> P18;
public Stack<int> P19;
public Queue<int> P20;
public LinkedList<int> P21;
public HashSet<int> P22;
public PriorityQueue<int, int> P23;
public ObservableCollection<int> P24;
public Collection<int> P25;
public ConcurrentQueue<int> P26;
public ConcurrentStack<int> P27;
public ConcurrentBag<int> P28;
public Dictionary<int, int> P29;
public SortedDictionary<int, int> P30;
public SortedList<int, int> P31;
public ConcurrentDictionary<int, int> P32;
public ReadOnlyCollection<int> P33;
public ReadOnlyObservableCollection<int> P34;
public BlockingCollection<int> P35;
public ImmutableArray<int> P36;
public ImmutableList<int> P37;
public ImmutableQueue<int> P38;
public ImmutableStack<int> P39;
public ImmutableDictionary<int, int> P40;
// public ImmutableSortedDictionary<int, int> P41;
public ImmutableSortedSet<int> P42;
public ImmutableHashSet<int> P43;
public IImmutableList<int> P44;
public IImmutableQueue<int> P45;
public IImmutableStack<int> P46;
public IImmutableDictionary<int, int> P47;
public IImmutableSet<int> P48;
public IEnumerable<int> P49;
public ICollection<int> P50;
public IReadOnlyCollection<int> P51;
public IList<int> P52;
public IReadOnlyList<int> P53;
public IDictionary<int, int> P54;
public IReadOnlyDictionary<int, int> P55;
public ILookup<int, int> P56;
public IGrouping<int, int> P57;
public ISet<int> P58;
public IReadOnlySet<int> P59;
// tuples
public Tuple<int, string, int> T3;
public ValueTuple<int, string, int> VT3;
// more
public Nullable<MyStruct> N1;
public KeyValuePair<string, string> N2;
}
[MemoryPackable]
public partial struct MyStruct
{
public string? V;
}
[MemoryPackable(GenerateType.Collection)]
public partial class ListInt : List<int>
{
}
[MemoryPackable(GenerateType.Collection)]
public partial class SetInt : HashSet<int>
{
}
[MemoryPackable(GenerateType.Collection)]
public partial class DictionaryIntInt : Dictionary<int, int>
{
}
[MemoryPackable(GenerateType.Collection)]
public partial class SetGenerics<T> : HashSet<T>
{
}
[MemoryPackable(GenerateType.Collection)]
public partial class DictionaryGenerics<TK, TV> : Dictionary<TK, TV>
where TK : notnull
{
}
//public class MyCollection<T> : List<T>, IMemoryPackFormatterRegister
//{
// static MyCollection()
// {
// if (!MemoryPackFormatterProvider.IsRegistered<MyCollection<T>>())
// {
// MemoryPackFormatterProvider.Register<MyCollection<T>>();
// }
// }
// static void IMemoryPackFormatterRegister.RegisterFormatter()
// {
// MemoryPackFormatterProvider.RegisterCollection<MyCollection<T?>, T>();
// }
//}
//[MemoryPackable]
//public partial class Packable<T>
//{
// public int TakoyakiX { get; set; }
// [MemoryPackIgnore]
// public object? ObjectObject { get; set; }
// [MemoryPackIgnore]
// public Array? StandardArray { get; set; }
// public int[]? Array { get; set; }
// public int[,]? MoreArray { get; set; }
// public List<int>? List { get; set; }
// public Version? Version { get; set; }
// public T? TTTTT { get; set; }
// [MemoryPackFormatter]
// public Nazo? MyProperty { get; set; }
// [MemoryPackFormatter]
// public Nazo2? MyProperty2 { get; set; }
//}
//public class Nazo
//{
//}
//public class Nazo2
//{
//}
//public class Tadano
//{
// public int MyProperty { get; set; }
//}
//public class C
//{
// public int Foo { get; init; }
// public required int Bar { get; init; }
[MemoryPackable]
public partial class Sample
{
// these types are serialized by default
public int PublicField;
public readonly int PublicReadOnlyField;
public int PublicProperty { get; set; }
public int PrivateSetPublicProperty { get; private set; }
public int ReadOnlyPublicProperty { get; }
public int InitProperty { get; init; }
public required int RequiredInitProperty { get; init; }
// these types are not serialized by default
int privateProperty { get; set; }
int privateField;
readonly int privateReadOnlyField;
// use [MemoryPackIgnore] to remove target of public member
[MemoryPackIgnore]
public int PublicProperty2 => PublicProperty + PublicField;
// use [MemoryPackInclude] to promote private member to serialization target
[MemoryPackInclude]
int privateField2;
[MemoryPackInclude]
int privateProperty2 { get; set; }
}