forked from jamescourtney/FlatSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOOperationsExample.cs
More file actions
93 lines (82 loc) · 3.88 KB
/
Copy pathIOOperationsExample.cs
File metadata and controls
93 lines (82 loc) · 3.88 KB
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
/*
* Copyright 2020 James Courtney
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Samples.IOOptionsExample
{
using System;
using System.Collections.Generic;
using FlatSharp;
using FlatSharp.Unsafe;
/// <summary>
/// This sample shows some different IO Options when using FlatSharp.
/// </summary>
public class IOOperationsExample
{
public static void Run()
{
Dog tony = new Dog
{
Breed = DogBreed.BostonTerrier,
Vitals = new AnimalVitals { Age = 11, Gender = Gender.Male, Name = "Tony" }
};
Dog rocket = new Dog
{
Breed = DogBreed.GoldenRetriever,
Vitals = new AnimalVitals { Age = 8, Gender = Gender.Female, Name = "Rocket" }
};
Dog peaches = new Dog
{
Breed = DogBreed.GermanShepard,
Vitals = new AnimalVitals { Age = 14, Gender = Gender.Female, Name = "Peaches" }
};
Cat grumpyCat = new Cat
{
Breed = CatBreed.GrumpyCat,
Vitals = new AnimalVitals { Age = 17, Gender = Gender.Female, Name = "Tardar Sauce" }
};
Person person = new Person
{
Age = 24,
Cats = new[] { grumpyCat },
Dogs = new[] { tony, rocket, peaches },
FavoritePet = new FavoritePet(rocket),
Name = "Nikola Tesla"
};
// SpanWriter is the core code that writes data to a span. Flatsharp provides a couple:
SpanWriter spanWriter = new SpanWriter();
var unsafeSpanWriter = new UnsafeSpanWriter();
byte[] buffer = new byte[Person.Serializer.GetMaxSize(person)];
int bytesWritten = Person.Serializer.Write(spanWriter, buffer, person);
bytesWritten = Person.Serializer.Write(unsafeSpanWriter, buffer, person);
// For reading data, we use InputBuffer. There are more options here:
// Array and Memory input buffers are general purpose and support all scenarios.
var p1 = Person.Serializer.Parse(new ArrayInputBuffer(buffer));
var p2 = Person.Serializer.Parse(new MemoryInputBuffer(new Memory<byte>(buffer)));
// ReadOnlyMemory input buffer will fail to Parse any objects that have Memory<T> in them (that is -- non read only memory).
var p3 = Person.Serializer.Parse(new ReadOnlyMemoryInputBuffer(new ReadOnlyMemory<byte>(buffer)));
// The unsafe variants are available in the FlatSharp.Unsafe package and use pointers and other unsafe code to squeeze
// out some more performance. These unsafe inputs generally have the most benefit when running on .NET Framework due
// to legacy code requirements.
var p4 = Person.Serializer.Parse(new UnsafeArrayInputBuffer(buffer));
using (var unsafeMemoryInput = new UnsafeMemoryInputBuffer(new Memory<byte>(buffer)))
{
// Unsafe memory input buffer must be disposed of because it pins the memory in place.
// The lifetime of the buffer must be tied to the lifetime of the deserialized object
// unless using lazy deserialization.
var p5 = Person.Serializer.Parse(unsafeMemoryInput);
}
}
}
}