Skip to content

Commit 705b09b

Browse files
committed
Merge branch 'develop' of https://github.com/ReferenceType/NetworkExperiments into develop
2 parents ff67c02 + 7555c1f commit 705b09b

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

temp.txt

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using NetSerializer;
2+
using NetworkLibrary.Components;
3+
using Newtonsoft.Json;
4+
using ProtoBuf;
5+
using System;
6+
using System.Buffers;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using System.Linq;
10+
using System.Runtime.Serialization;
11+
using System.Runtime.Serialization.Formatters.Binary;
12+
using System.Text;
13+
using System.Text.Json;
14+
using System.Threading.Tasks;
15+
using System.Xml.Serialization;
16+
using TestExternalType;
17+
using ZeroFormatter;
18+
19+
namespace ProcessStartTest
20+
{
21+
[ProtoContract]
22+
[Serializable]
23+
[DataContract(Name = "a", Namespace = "")]
24+
public class Data
25+
{
26+
[DataMember(IsRequired = false, Order = 1, EmitDefaultValue =false)]
27+
[ProtoMember(1)]
28+
public Guid Id { get; set; }
29+
30+
[DataMember(IsRequired = false, Order = 2, EmitDefaultValue = false)]
31+
[ProtoMember(2)]
32+
33+
public DateTime Time { get; set; }
34+
35+
[DataMember(IsRequired = false, Order = 3, EmitDefaultValue = false)]
36+
[ProtoMember(3)]
37+
38+
public Dictionary<string, string> KVPairs { get; set; }
39+
40+
}
41+
class BinarySerialization
42+
{
43+
public static void SerializeBinary()
44+
{
45+
var data = new Data();
46+
data.Id = Guid.NewGuid();
47+
data.Time = DateTime.Now;
48+
data.KVPairs = new Dictionary<string, string>() { { "Key1", "Value1" } };
49+
50+
MemoryStream ms = new MemoryStream();
51+
52+
BinaryFormatter formatter = new BinaryFormatter();
53+
formatter.Serialize(ms, data);
54+
ms.Position = 0;
55+
Data dabaBack = formatter.Deserialize(ms) as Data;
56+
}
57+
58+
public static void SerializeDataContract()
59+
{
60+
var data = new Data();
61+
data.Id = Guid.NewGuid();
62+
data.Time = DateTime.Now;
63+
data.KVPairs = new Dictionary<string, string>() { { "Key1", "Value1" } };
64+
65+
var ms = new MemoryStream();
66+
67+
DataContractSerializer serialzier = new DataContractSerializer(typeof(Data));
68+
serialzier.WriteObject(ms, data);
69+
//var bytes = ms.GetBuffer();
70+
//var res = UTF8Encoding.UTF8.GetString(bytes);
71+
72+
73+
74+
ms.Position = 0;
75+
var ret = serialzier.ReadObject(ms) as Data;
76+
}
77+
static PooledMemoryStream ms = new PooledMemoryStream();
78+
public static void SerializeJson()
79+
{
80+
var data = new Data();
81+
data.Id = Guid.NewGuid();
82+
data.Time = DateTime.Now;
83+
data.KVPairs = new Dictionary<string, string>() { { "Key1", "Value1" } };
84+
85+
86+
Utf8JsonWriter writer = new Utf8JsonWriter(ms);
87+
88+
System.Text.Json.JsonSerializer.Serialize(writer, data);
89+
90+
var buff = ms.GetBuffer();
91+
92+
var returned = System.Text.Json.JsonSerializer.Deserialize<Data>(new ReadOnlySpan<byte>(buff,0, (int)writer.BytesCommitted));
93+
}
94+
95+
static NetSerializer.Serializer ser = new NetSerializer.Serializer(new List<Type>() { typeof(Data) });
96+
internal static void SerializeNetSerializer()
97+
{
98+
var data = new Data();
99+
data.Id = Guid.NewGuid();
100+
data.Time = DateTime.Now;
101+
data.KVPairs = new Dictionary<string, string>() { { "Key1", "Value1" } };
102+
103+
104+
105+
var ms = new MemoryStream();
106+
107+
ser.Serialize(ms, data);
108+
//ser.SerializeDirect<Data>(ms, data);
109+
ms.Position = 0;
110+
111+
//var types = ser.GetTypeMap();
112+
//if (!types.ContainsKey(typeof(Data1)))
113+
//{
114+
// ser.AddTypes(new List<Type>() { typeof(Data1) });
115+
//}
116+
117+
var ob = (Data)ser.Deserialize(ms);
118+
//ser.DeserializeDirect<Data>(ms, out Data value);
119+
}
120+
121+
122+
public static void SerializeProtobuf()
123+
{
124+
var data = new Data();
125+
data.Id = Guid.NewGuid();
126+
data.Time = DateTime.Now;
127+
data.KVPairs = new Dictionary<string, string>() { { "Key1", "Value1" } };
128+
129+
var ms = new MemoryStream();
130+
131+
ProtoBuf.Serializer.Serialize(ms, data);
132+
//ms.Position = 0;
133+
134+
var result = ProtoBuf.Serializer.Deserialize<Data>(new ReadOnlySpan<byte>(ms.GetBuffer(),0,(int)ms.Position));
135+
136+
137+
}
138+
139+
140+
}
141+
}

0 commit comments

Comments
 (0)