Added Dictionary<,> mapping support
parent
72ec888c45
commit
1a24ea939e
|
@ -1,17 +1,18 @@
|
||||||
<Properties StartupConfiguration="{49FFBD9F-655E-4C74-A078-99B5E09059C6}|Default">
|
<Properties StartupConfiguration="{49FFBD9F-655E-4C74-A078-99B5E09059C6}|Default">
|
||||||
<MonoDevelop.Ide.Workbench ActiveDocument="json.test/Program.cs">
|
<MonoDevelop.Ide.Workbench ActiveDocument="mapping/JSONMapper.cs">
|
||||||
<Files>
|
<Files>
|
||||||
<File FileName="json.test/Program.cs" Line="54" Column="9" />
|
<File FileName="json.test/Program.cs" Line="48" Column="13" />
|
||||||
|
<File FileName="mapping/JSONMapper.cs" Line="100" Column="13" />
|
||||||
</Files>
|
</Files>
|
||||||
<Pads>
|
<Pads>
|
||||||
<Pad Id="ProjectPad">
|
<Pad Id="ProjectPad">
|
||||||
<State name="__root__">
|
<State name="__root__">
|
||||||
<Node name="sharp.json" expanded="True">
|
<Node name="sharp.json" expanded="True">
|
||||||
<Node name="json.test" expanded="True">
|
<Node name="json.test" expanded="True" />
|
||||||
<Node name="Program.cs" selected="True" />
|
|
||||||
</Node>
|
|
||||||
<Node name="ln.json" expanded="True">
|
<Node name="ln.json" expanded="True">
|
||||||
<Node name="mapping" expanded="True" />
|
<Node name="mapping" expanded="True">
|
||||||
|
<Node name="JSONMapper.cs" selected="True" />
|
||||||
|
</Node>
|
||||||
</Node>
|
</Node>
|
||||||
</Node>
|
</Node>
|
||||||
</State>
|
</State>
|
||||||
|
|
|
@ -53,6 +53,7 @@
|
||||||
<Compile Include="mapping\JSONRPCResultMapping.cs" />
|
<Compile Include="mapping\JSONRPCResultMapping.cs" />
|
||||||
<Compile Include="attributes\JSONMappingAttribute.cs" />
|
<Compile Include="attributes\JSONMappingAttribute.cs" />
|
||||||
<Compile Include="mapping\JSONEnumMapping.cs" />
|
<Compile Include="mapping\JSONEnumMapping.cs" />
|
||||||
|
<Compile Include="mapping\JSONDictionaryMapping.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="mapping\" />
|
<Folder Include="mapping\" />
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
// /**
|
||||||
|
// * File: JSONDictionaryMapping.cs
|
||||||
|
// * Author: haraldwolff
|
||||||
|
// *
|
||||||
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
||||||
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
||||||
|
// *
|
||||||
|
// *
|
||||||
|
// **/
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
namespace ln.json.mapping
|
||||||
|
{
|
||||||
|
public class JSONDictionaryMapping<K,V> : JSONMapping
|
||||||
|
{
|
||||||
|
public JSONDictionaryMapping()
|
||||||
|
:base(typeof(Dictionary<K,V>))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override JSONValue ToJson(JSONMapper mapper, object value)
|
||||||
|
{
|
||||||
|
JSONObject jObject = new JSONObject();
|
||||||
|
|
||||||
|
foreach (KeyValuePair<K,V> item in (Dictionary<K,V>)value)
|
||||||
|
{
|
||||||
|
jObject[item.Key.ToString()] = mapper.ToJson(item.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return jObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override object FromJson(JSONMapper mapper, JSONValue json)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -127,13 +127,23 @@ namespace ln.json.mapping
|
||||||
if (targetType.IsArray)
|
if (targetType.IsArray)
|
||||||
{
|
{
|
||||||
Add(new JSONArrayMapping(targetType));
|
Add(new JSONArrayMapping(targetType));
|
||||||
} else if (targetType.IsEnum)
|
}
|
||||||
|
else if (targetType.IsEnum)
|
||||||
{
|
{
|
||||||
Add(new JSONEnumMapping(targetType));
|
Add(new JSONEnumMapping(targetType));
|
||||||
} else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(IEnumerable<>)))
|
}
|
||||||
|
else if (targetType.IsGenericType)
|
||||||
{
|
{
|
||||||
Add((JSONMapping)Activator.CreateInstance(typeof(JSONEnumerableMapping<>).MakeGenericType(targetType.GetGenericArguments()[0])));
|
Type genericTypeDefinition = targetType.GetGenericTypeDefinition();
|
||||||
} else
|
if (genericTypeDefinition.Equals(typeof(IEnumerable<>)))
|
||||||
|
{
|
||||||
|
Add((JSONMapping)Activator.CreateInstance(typeof(JSONEnumerableMapping<>).MakeGenericType(targetType.GetGenericArguments()[0])));
|
||||||
|
} else if (genericTypeDefinition.Equals(typeof(Dictionary<,>)))
|
||||||
|
{
|
||||||
|
Add((JSONMapping)Activator.CreateInstance(typeof(JSONDictionaryMapping<,>).MakeGenericType(targetType.GetGenericArguments())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
Add(new JSONObjectMapping(targetType));
|
Add(new JSONObjectMapping(targetType));
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ namespace ln.json.mapping
|
||||||
foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
|
foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
|
||||||
{
|
{
|
||||||
JSONMappingAttribute mappingAttribute = propertyInfo.GetCustomAttribute<JSONMappingAttribute>();
|
JSONMappingAttribute mappingAttribute = propertyInfo.GetCustomAttribute<JSONMappingAttribute>();
|
||||||
if ((mappingAttribute == null) || !mappingAttribute.Private)
|
if ((propertyInfo.GetIndexParameters().Length == 0) && ((mappingAttribute == null) || !mappingAttribute.Private))
|
||||||
{
|
{
|
||||||
setters.Add(propertyInfo.Name, (object arg1, object arg2) => propertyInfo.SetValue(arg1, arg2));
|
setters.Add(propertyInfo.Name, (object arg1, object arg2) => propertyInfo.SetValue(arg1, arg2));
|
||||||
getters.Add(propertyInfo.Name, (object arg) => propertyInfo.GetValue(arg));
|
getters.Add(propertyInfo.Name, (object arg) => propertyInfo.GetValue(arg));
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue