Added Dictionary<,> mapping support

master
Harald Wolff 2019-09-01 19:33:28 +02:00
parent 72ec888c45
commit 1a24ea939e
7 changed files with 4754 additions and 11 deletions

View File

@ -1,17 +1,18 @@
<Properties StartupConfiguration="{49FFBD9F-655E-4C74-A078-99B5E09059C6}|Default">
<MonoDevelop.Ide.Workbench ActiveDocument="json.test/Program.cs">
<MonoDevelop.Ide.Workbench ActiveDocument="mapping/JSONMapper.cs">
<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>
<Pads>
<Pad Id="ProjectPad">
<State name="__root__">
<Node name="sharp.json" expanded="True">
<Node name="json.test" expanded="True">
<Node name="Program.cs" selected="True" />
</Node>
<Node name="json.test" 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>
</State>

View File

@ -53,6 +53,7 @@
<Compile Include="mapping\JSONRPCResultMapping.cs" />
<Compile Include="attributes\JSONMappingAttribute.cs" />
<Compile Include="mapping\JSONEnumMapping.cs" />
<Compile Include="mapping\JSONDictionaryMapping.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="mapping\" />

View File

@ -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;
}
}
}

View File

@ -127,13 +127,23 @@ namespace ln.json.mapping
if (targetType.IsArray)
{
Add(new JSONArrayMapping(targetType));
} else if (targetType.IsEnum)
}
else if (targetType.IsEnum)
{
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])));
} else
Type genericTypeDefinition = targetType.GetGenericTypeDefinition();
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));
}

View File

@ -40,7 +40,7 @@ namespace ln.json.mapping
foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
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));
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