forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionarySerializerFactory.cs
More file actions
87 lines (78 loc) · 3.37 KB
/
Copy pathDictionarySerializerFactory.cs
File metadata and controls
87 lines (78 loc) · 3.37 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
// -----------------------------------------------------------------------
// <copyright file="DictionarySerializerFactory.cs" company="Asynkron HB">
// Copyright (C) 2015-2017 Asynkron HB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Wire.Extensions;
using Wire.ValueSerializers;
namespace Wire.SerializerFactories
{
public class DictionarySerializerFactory : ValueSerializerFactory
{
public override bool CanSerialize(Serializer serializer, Type type) => IsInterface(type);
private static bool IsInterface(Type type)
{
return type
.GetTypeInfo()
.GetInterfaces()
.Select(
t =>
t.GetTypeInfo().IsGenericType &&
t.GetTypeInfo().GetGenericTypeDefinition() == typeof(IDictionary<,>))
.Any(isDict => isDict);
}
public override bool CanDeserialize(Serializer serializer, Type type) => IsInterface(type);
public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
var ser = new ObjectSerializer(type);
typeMapping.TryAdd(type, ser);
var elementSerializer = serializer.GetSerializerByType(typeof(DictionaryEntry));
object Reader(Stream stream, DeserializerSession session)
{
throw new NotSupportedException("Generic IDictionary<TKey,TValue> are not yet supported");
#pragma warning disable CS0162 // Unreachable code detected
var instance = Activator.CreateInstance(type);
#pragma warning restore CS0162 // Unreachable code detected
if (preserveObjectReferences)
{
session.TrackDeserializedObject(instance);
}
var count = stream.ReadInt32(session);
var entries = new DictionaryEntry[count];
for (var i = 0; i < count; i++)
{
var entry = (DictionaryEntry) stream.ReadObject(session);
entries[i] = entry;
}
//TODO: populate dictionary
return instance;
}
void Writer(Stream stream, object obj, SerializerSession session)
{
if (preserveObjectReferences)
{
session.TrackSerializedObject(obj);
}
var dict = obj as IDictionary;
// ReSharper disable once PossibleNullReferenceException
Int32Serializer.WriteValueImpl(stream, dict.Count, session);
foreach (var item in dict)
{
stream.WriteObject(item, typeof(DictionaryEntry), elementSerializer, serializer.Options.PreserveObjectReferences, session);
// elementSerializer.WriteValue(stream,item,session);
}
}
ser.Initialize(Reader, Writer);
return ser;
}
}
}