Skip to content

Commit

Permalink
Add support to SimpleJson for IReadOnlyDictionary
Browse files Browse the repository at this point in the history
I need to submit a PR for this upstream as well.
  • Loading branch information
haacked committed Jan 4, 2015
1 parent 0e25b14 commit 316e456
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Octokit/SimpleJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
#if !SIMPLE_JSON_NO_LINQ_EXPRESSION
using System.Linq.Expressions;
#endif
Expand Down Expand Up @@ -1417,6 +1419,20 @@ public virtual object DeserializeObject(object value, Type type)
dict.Add(kvp.Key, DeserializeObject(kvp.Value, valueType));

obj = dict;

#if SIMPLE_JSON_READONLY_COLLECTIONS
// Wrap dictionary in a ReadOnlyDictionary<,>
var genericTypeDefinition = type.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(IReadOnlyDictionary<,>) ||
genericTypeDefinition == typeof(ReadOnlyDictionary<,>))
{
var ctorType = typeof(IDictionary<,>).MakeGenericType(keyType, valueType);
var genericReadonlyType = typeof(ReadOnlyDictionary<,>).MakeGenericType(keyType, valueType);
var ctor = ReflectionUtils.GetContructor(genericReadonlyType, new Type[] { ctorType });
Debug.Assert(ctor != null);
obj = ctor.Invoke(new[] { obj });
}
#endif
}
else
{
Expand Down Expand Up @@ -1725,7 +1741,11 @@ public static bool IsTypeDictionary(Type type)
return false;

Type genericDefinition = type.GetGenericTypeDefinition();
return genericDefinition == typeof(IDictionary<,>);
return genericDefinition == typeof(IDictionary<,>)
#if SIMPLE_JSON_READONLY_COLLECTIONS
|| genericDefinition == typeof(IReadOnlyDictionary<,>)
#endif
;
}

public static bool IsNullableType(Type type)
Expand Down

0 comments on commit 316e456

Please sign in to comment.