-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialoze.cs
61 lines (48 loc) · 1.76 KB
/
Serialoze.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
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using System;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class PersonSerializer : IBsonSerializer<Person>
{
public Type ValueType => typeof(Person);
public Person Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var bsonReader = context.Reader;
bsonReader.ReadStartDocument();
var name = bsonReader.ReadString("Name");
var age = bsonReader.ReadInt32("Age");
bsonReader.ReadEndDocument();
return new Person { Name = name, Age = age };
}
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Person value)
{
var bsonWriter = context.Writer;
bsonWriter.WriteStartDocument();
bsonWriter.WriteString("Name", value.Name);
bsonWriter.WriteInt32("Age", value.Age);
bsonWriter.WriteEndDocument();
}
}
class Program
{
static void Main()
{
// Register the custom serializer
BsonSerializer.RegisterSerializer(new PersonSerializer());
// Create a sample Person object
var person = new Person { Name = "Alice", Age = 25 };
// Serialize the Person object
var serializedData = person.ToBson();
Console.WriteLine("Serialized Data:");
Console.WriteLine(BitConverter.ToString(serializedData));
// Deserialize the byte array back into a Person object
var deserializedPerson = BsonSerializer.Deserialize<Person>(serializedData);
Console.WriteLine("\nDeserialized Object:");
Console.WriteLine($"Name: {deserializedPerson.Name}, Age: {deserializedPerson.Age}");
}
}