-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathIOOperationsExample.cs
83 lines (71 loc) · 3.09 KB
/
IOOperationsExample.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
/*
* 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;
/// <summary>
/// This sample shows some different IO Options when using FlatSharp.
/// </summary>
public class IOOperationsExample : IFlatSharpSample
{
public bool HasConsoleOutput => false;
public 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"
};
// ISpanWriter is the core code that writes data to a span. Flatsharp provides one: SpanWriter
// However, you can always implement ISpanWriter yourself.
SpanWriter spanWriter = new SpanWriter();
// Allocate a new buffer that can hold this person.
byte[] buffer = new byte[Person.Serializer.GetMaxSize(person)];
// Write the person into the buffer using the given spanwriter.
int bytesWritten = Person.Serializer.Write(spanWriter, buffer, person);
// For reading data, we use InputBuffer. There are more options here:
// - ArrayInputBuffer for regular arrays
// - ArraySegmentInputBuffer for ArraySegment<byte>
// - MemoryInputBuffer for Memory<byte>
// - ReadOnlyMemoryInputBuffer for ReadOnlyMemory<byte>. This one won't work for WriteThrough
// or objects that have a Memory<byte> vector in them.
var p1 = Person.Serializer.Parse(new ArrayInputBuffer(buffer));
var p2 = Person.Serializer.Parse(new ArraySegmentInputBuffer(new ArraySegment<byte>(buffer)));
var p3 = Person.Serializer.Parse(new MemoryInputBuffer(new Memory<byte>(buffer)));
var p4 = Person.Serializer.Parse(new ReadOnlyMemoryInputBuffer(new ReadOnlyMemory<byte>(buffer)));
}
}