Skip to content

Commit da72635

Browse files
ladeakladeak
andauthored
Support serializing ISet and IReadOnlySet (this latter for .NET6 and above only) (#1118)
Co-authored-by: ladeak <[email protected]>
1 parent a263ab6 commit da72635

5 files changed

Lines changed: 498 additions & 5 deletions

File tree

‎docs/releasenotes.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Packages are available on NuGet: [protobuf-net](https://www.nuget.org/packages/p
1414

1515
## unreleased
1616

17+
- support of deserializing `ISet<T>` and `IReadOnlySet<T>` (ladeak)
18+
1719
## 3.2.30
1820

1921
- support `DateOnly` and `TimeOnly` (#1100 by @mgravell, fixes #977)

‎src/Examples/SetTests.cs‎

Lines changed: 378 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,378 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using ProtoBuf;
4+
using Xunit;
5+
6+
namespace Examples
7+
{
8+
public class HashSetSerializerTests
9+
{
10+
[ProtoContract]
11+
class HashSetData<T>
12+
{
13+
[ProtoMember(1)]
14+
public HashSet<T> Data { get; set; }
15+
}
16+
17+
[ProtoContract]
18+
class NotEmptyHashSetData
19+
{
20+
public NotEmptyHashSetData() => Data = new HashSet<int>(new[] { 1 });
21+
22+
public NotEmptyHashSetData(HashSet<int> data) => Data = data;
23+
24+
[ProtoMember(1)]
25+
public HashSet<int> Data { get; }
26+
}
27+
28+
[Fact]
29+
public void TestEmptyNestedSetWithStrings()
30+
{
31+
var set = new HashSet<string>();
32+
var input = new HashSetData<string>() { Data = set };
33+
34+
var clone = Serializer.DeepClone(input);
35+
36+
Assert.NotSame(input, clone);
37+
Assert.Null(clone.Data);
38+
}
39+
40+
[Fact]
41+
public void TestNullNestedSetWithStrings()
42+
{
43+
var input = new HashSetData<string>() { Data = null };
44+
45+
var clone = Serializer.DeepClone(input);
46+
47+
Assert.NotSame(input, clone);
48+
Assert.Equal(input.Data, clone.Data);
49+
}
50+
51+
[Fact]
52+
public void TestNestedSetWithStrings()
53+
{
54+
var set = new HashSet<string>();
55+
set.Add("hello");
56+
set.Add("world");
57+
var input = new HashSetData<string>() { Data = set };
58+
59+
var clone = Serializer.DeepClone(input);
60+
61+
Assert.NotSame(input, clone);
62+
AssertEqual(input.Data, clone.Data);
63+
}
64+
65+
[Fact]
66+
public void TestNestedSetWithInt32()
67+
{
68+
var set = new HashSet<int>();
69+
set.Add(1);
70+
set.Add(2);
71+
set.Add(3);
72+
set.Add(3);
73+
var input = new HashSetData<int>() { Data = set };
74+
75+
var clone = Serializer.DeepClone(input);
76+
77+
Assert.NotSame(input, clone);
78+
AssertEqual(input.Data, clone.Data);
79+
}
80+
81+
[Fact]
82+
public void RoundtripHashSet()
83+
{
84+
HashSet<int> lookup = new HashSet<int>(new[] { 1, 2, 3 });
85+
86+
var clone = Serializer.DeepClone(lookup);
87+
88+
AssertEqual(lookup, clone);
89+
}
90+
91+
[Fact]
92+
public void TestNonEmptySetStrings()
93+
{
94+
var set = new HashSet<int>();
95+
set.Add(1);
96+
set.Add(2);
97+
set.Add(2);
98+
var input = new NotEmptyHashSetData(set);
99+
100+
var clone = Serializer.DeepClone(input);
101+
102+
Assert.NotSame(input, clone);
103+
AssertEqual(input.Data, clone.Data);
104+
}
105+
106+
[Fact]
107+
public void TestDefaultCtorValueOverwritten()
108+
{
109+
var set = new HashSet<int>();
110+
set.Add(3);
111+
var input = new NotEmptyHashSetData(set);
112+
113+
var clone = Serializer.DeepClone(input);
114+
Assert.NotSame(input, clone);
115+
Assert.Contains(new NotEmptyHashSetData().Data.Single(), clone.Data);
116+
Assert.Contains(3, clone.Data);
117+
}
118+
119+
static void AssertEqual<T>(
120+
HashSet<T> expected,
121+
HashSet<T> actual)
122+
{
123+
Assert.Equal(expected.Count, actual.Count);
124+
foreach (var value in expected)
125+
Assert.Contains(value, actual);
126+
}
127+
}
128+
129+
public class SetSerializerTests
130+
{
131+
[ProtoContract]
132+
class SetData<T>
133+
{
134+
[ProtoMember(1)]
135+
public ISet<T> Data { get; set; }
136+
}
137+
138+
[ProtoContract]
139+
class NotEmptySetData
140+
{
141+
public NotEmptySetData() => Data = new HashSet<int>(new[] { 1 });
142+
143+
public NotEmptySetData(ISet<int> data) => Data = data;
144+
145+
[ProtoMember(1)]
146+
public ISet<int> Data { get; }
147+
}
148+
149+
[Fact]
150+
public void TestEmptyNestedSetWithStrings()
151+
{
152+
var set = new HashSet<string>();
153+
var input = new SetData<string>() { Data = set };
154+
155+
var clone = Serializer.DeepClone(input);
156+
157+
Assert.NotSame(input, clone);
158+
Assert.Null(clone.Data);
159+
}
160+
161+
[Fact]
162+
public void TestNullNestedSetWithStrings()
163+
{
164+
var input = new SetData<string>() { Data = null };
165+
166+
var clone = Serializer.DeepClone(input);
167+
168+
Assert.NotSame(input, clone);
169+
Assert.Equal(input.Data, clone.Data);
170+
}
171+
172+
[Fact]
173+
public void TestNestedSetWithStrings()
174+
{
175+
var set = new HashSet<string>();
176+
set.Add("hello");
177+
set.Add("world");
178+
var input = new SetData<string>() { Data = set };
179+
180+
var clone = Serializer.DeepClone(input);
181+
182+
Assert.NotSame(input, clone);
183+
AssertEqual(input.Data, clone.Data);
184+
}
185+
186+
[Fact]
187+
public void TestNestedSetWithInt32()
188+
{
189+
var set = new HashSet<int>();
190+
set.Add(1);
191+
set.Add(2);
192+
set.Add(3);
193+
set.Add(3);
194+
var input = new SetData<int>() { Data = set };
195+
196+
var clone = Serializer.DeepClone(input);
197+
198+
Assert.NotSame(input, clone);
199+
AssertEqual(input.Data, clone.Data);
200+
}
201+
202+
[Fact]
203+
public void RoundtripSet()
204+
{
205+
ISet<int> lookup = new HashSet<int>(new[] { 1, 2, 3 });
206+
207+
var clone = Serializer.DeepClone(lookup);
208+
209+
AssertEqual(lookup, clone);
210+
}
211+
212+
[Fact]
213+
public void TestNonEmptySetStrings()
214+
{
215+
var set = new HashSet<int>();
216+
set.Add(1);
217+
set.Add(2);
218+
set.Add(2);
219+
var input = new NotEmptySetData(set);
220+
221+
var clone = Serializer.DeepClone(input);
222+
223+
Assert.NotSame(input, clone);
224+
AssertEqual(input.Data, clone.Data);
225+
}
226+
227+
[Fact]
228+
public void TestDefaultCtorValueOverwritten()
229+
{
230+
var set = new HashSet<int>();
231+
set.Add(3);
232+
var input = new NotEmptySetData(set);
233+
234+
var clone = Serializer.DeepClone(input);
235+
Assert.NotSame(input, clone);
236+
Assert.True(clone.Data.Contains(new NotEmptySetData().Data.Single()));
237+
Assert.True(clone.Data.Contains(3));
238+
}
239+
240+
static void AssertEqual<T>(
241+
ISet<T> expected,
242+
ISet<T> actual)
243+
{
244+
Assert.Equal(expected.Count, actual.Count);
245+
foreach (var value in expected)
246+
Assert.True(actual.Contains(value));
247+
}
248+
}
249+
250+
#if NET6_0_OR_GREATER
251+
public class ReadOnlySetSerializerTests
252+
{
253+
[ProtoContract]
254+
class ReadOnlySetData<T>
255+
{
256+
[ProtoMember(1)]
257+
public IReadOnlySet<T> Data { get; init; }
258+
}
259+
260+
[ProtoContract]
261+
class NotEmptyReadOnlySetData
262+
{
263+
public NotEmptyReadOnlySetData()
264+
{
265+
Data = new HashSet<int>(new[] { 1 });
266+
}
267+
268+
public NotEmptyReadOnlySetData(IReadOnlySet<int> data)
269+
{
270+
Data = data;
271+
}
272+
273+
[ProtoMember(1)]
274+
public IReadOnlySet<int> Data { get; }
275+
}
276+
277+
[Fact]
278+
public void TestEmptyNestedSetWithStrings()
279+
{
280+
var set = new HashSet<string>();
281+
var input = new ReadOnlySetData<string>() { Data = set };
282+
283+
var clone = Serializer.DeepClone(input);
284+
285+
Assert.NotSame(input, clone);
286+
Assert.Null(clone.Data);
287+
}
288+
289+
[Fact]
290+
public void TestNullNestedSetWithStrings()
291+
{
292+
var input = new ReadOnlySetData<string>() { Data = null };
293+
294+
var clone = Serializer.DeepClone(input);
295+
296+
Assert.NotSame(input, clone);
297+
Assert.Equal(input.Data, clone.Data);
298+
}
299+
300+
[Fact]
301+
public void TestNestedSetWithStrings()
302+
{
303+
var set = new HashSet<string>();
304+
set.Add("hello");
305+
set.Add("world");
306+
var input = new ReadOnlySetData<string>() { Data = set };
307+
308+
var clone = Serializer.DeepClone(input);
309+
310+
Assert.NotSame(input, clone);
311+
AssertEqual(input.Data, clone.Data);
312+
}
313+
314+
[Fact]
315+
public void TestNestedSetWithInt32()
316+
{
317+
var set = new HashSet<int>();
318+
set.Add(1);
319+
set.Add(2);
320+
set.Add(3);
321+
set.Add(3);
322+
var input = new ReadOnlySetData<int>() { Data = set };
323+
324+
var clone = Serializer.DeepClone(input);
325+
326+
Assert.NotSame(input, clone);
327+
AssertEqual(input.Data, clone.Data);
328+
}
329+
330+
[Fact]
331+
public void RoundtripReadOnlySet()
332+
{
333+
IReadOnlySet<int> lookup = new HashSet<int>(new[] { 1, 2, 3 });
334+
335+
var clone = Serializer.DeepClone(lookup);
336+
337+
AssertEqual(lookup, clone);
338+
}
339+
340+
[Fact]
341+
public void TestNonEmptySetStrings()
342+
{
343+
var set = new HashSet<int>();
344+
set.Add(1);
345+
set.Add(2);
346+
set.Add(2);
347+
var input = new NotEmptyReadOnlySetData(set);
348+
349+
var clone = Serializer.DeepClone(input);
350+
351+
Assert.NotSame(input, clone);
352+
AssertEqual(input.Data, clone.Data);
353+
}
354+
355+
[Fact]
356+
public void TestDefaultCtorValueOverwritten()
357+
{
358+
var set = new HashSet<int>();
359+
set.Add(3);
360+
var input = new NotEmptyReadOnlySetData(set);
361+
362+
var clone = Serializer.DeepClone(input);
363+
Assert.NotSame(input, clone);
364+
Assert.True(clone.Data.Contains(new NotEmptyReadOnlySetData().Data.Single()));
365+
Assert.True(clone.Data.Contains(3));
366+
}
367+
368+
static void AssertEqual<T>(
369+
IReadOnlySet<T> expected,
370+
IReadOnlySet<T> actual)
371+
{
372+
Assert.Equal(expected.Count, actual.Count);
373+
foreach (var value in expected)
374+
Assert.True(actual.Contains(value));
375+
}
376+
}
377+
#endif
378+
}

0 commit comments

Comments
 (0)