Skip to content

Commit

Permalink
Support for List<T> added
Browse files Browse the repository at this point in the history
  • Loading branch information
steffen-liersch committed Dec 28, 2018
1 parent d529cfd commit 898a2c5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Constructor constraint added to SLJsonDeserializer.Deserialize
- Function SLJsonParser.Parse(string jsonExpression, bool allowArraysAndValues) added
- Functions CreateEmptyArray and CreateEmptyObject added to SLJsonNode
- Support for List<T> added to SLJsonDeserializer and SLJsonSerializer
### Changed
- Signing assemblies
- Using AssemblyInfo.cs for all library projects
Expand Down
47 changes: 38 additions & 9 deletions Demo/Classes/ExamplesWithReflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//----------------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace Liersch.Json
{
Expand All @@ -28,7 +29,11 @@ public static void Run()
Console.WriteLine();

var e1=new Example();
e1.IntegerArray=new int[] { 10, 20, 30, 700, 800 };
e1.PersonList=new List<Person>();
e1.PersonList.Add(new Person() { LastName="Doe", FirstName="John" });
e1.PersonList.Add(new Person() { LastName="Smith", FirstName="Jane" });
e1.IntegerList=new List<int> { 10, 20, 30 };
e1.IntegerArray=new int[] { 700, 800 };
e1.StringValue="Example Text";
e1.NotSerializedString="Other Text";

Expand All @@ -38,22 +43,35 @@ public static void Run()
string f="{0,-24} => {1,16} - {2}";
Console.WriteLine(string.Format(f, "Object", "e1", "e2"));

int c1=e1.IntegerArray.Length;
int c2=e2.IntegerArray.Length;

Console.WriteLine(string.Format(f, "IntegerArray.Length", c1, c2));

int c=Math.Min(c1, c2);
for(int i=0; i<c; i++)
Console.WriteLine(string.Format(f, "IntegerArray["+i+"]", e1.IntegerArray[i], e2.IntegerArray[i]));
CompareLists(f, "PersonList", e1.PersonList, e2.PersonList);
CompareLists(f, "IntegerList", e1.IntegerList, e2.IntegerList);
CompareLists(f, "IntegerArray", e1.IntegerArray, e2.IntegerArray);

Console.WriteLine(string.Format(f, "StringValue", e1.StringValue, e2.StringValue));
Console.WriteLine(string.Format(f, "NotSerializedString", e1.NotSerializedString, e2.NotSerializedString));
Console.WriteLine();
}

static void CompareLists<T>(string format, string name, IList<T> list1, IList<T> list2)
{
int c1=list1.Count;
int c2=list2.Count;

Console.WriteLine(string.Format(format, name+".Count", c1, c2));

int c=Math.Min(c1, c2);
for(int i=0; i<c; i++)
Console.WriteLine(string.Format(format, name+"["+i+"]", list1[i], list2[i]));
}

class Example
{
[SLJsonMember("PersonList", SLJsonMemberType.ObjectArray)]
public List<Person> PersonList;

[SLJsonMember("IntegerList", SLJsonMemberType.ValueArray)]
public List<int> IntegerList;

[SLJsonMember("IntegerArray", SLJsonMemberType.ValueArray)]
public int[] IntegerArray;

Expand All @@ -62,6 +80,17 @@ class Example

public string NotSerializedString;
}

class Person
{
[SLJsonMember("LastName")]
public string LastName;

[SLJsonMember("FirstName")]
public string FirstName;

public override string ToString() { return FirstName+" "+LastName; }
}
}

//--------------------------------------------------------------------------
Expand Down
48 changes: 35 additions & 13 deletions Source/JsonReflectionBased/SLJsonDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//----------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
Expand Down Expand Up @@ -114,17 +115,35 @@ object DeserializeObjectInternal(SLJsonNode node, SLJsonMemberType memberType, T

object DeserializeArray(Type type, SLJsonNode array, bool asObject)
{
if(!type.IsArray)
throw new NotSupportedException("Type "+type.FullName+" is not an array");
int c=array.Count;

Type t=type.GetElementType();
Type elemType;
Array resArray;
IList resList;

object defaultValue=null;
if(!asObject && !t.GetTypeInfo().IsClass)
defaultValue=Activator.CreateInstance(t);
if(type.IsArray)
{
if(type.GetArrayRank()!=1)
throw new NotSupportedException("Multi-dimensional arrays are not supported");

int c=array.Count;
var res=Array.CreateInstance(t, c);
elemType=type.GetElementType();
resArray=Array.CreateInstance(elemType, c);
resList=null;
}
else
{
Type[] args=type.GetGenericArguments();
elemType=args.Length==1 ? args[0] : null;
if(elemType==null || !typeof(List<>).MakeGenericType(elemType).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
throw new NotSupportedException("Type "+type.FullName+" is neither an array nor a List<>");

resList=(IList)Activator.CreateInstance(type);
resArray=null;
}

object defaultValue=null;
if(!asObject && !elemType.GetTypeInfo().IsClass)
defaultValue=Activator.CreateInstance(elemType);

for(int i=0; i<c; i++)
{
Expand All @@ -135,18 +154,21 @@ object DeserializeArray(Type type, SLJsonNode array, bool asObject)
if(asObject)
{
if(n.IsObject)
v=DeserializeObject(t, n);
v=DeserializeObject(elemType, n);
}
else
{
if(n.IsValue)
v=DeserializeValue(t, n);
v=DeserializeValue(elemType, n);
}
}
res.SetValue(v, i);
}

return res;
if(resArray!=null)
resArray.SetValue(v, i);
else resList.Add(v);
}

return resArray ?? resList;
}

object DeserializeValue(Type type, SLJsonNode value) { return ParseValue(type, value.AsString); }
Expand Down
7 changes: 5 additions & 2 deletions Source/JsonReflectionBased/SLJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ void SerializeProperty(SLJsonMemberAttribute attribute, Type type, object value)

void SerializeArray(Type type, object array, bool asObject)
{
if(!type.IsArray)
throw new NotSupportedException("Type "+type.FullName+" is not an array");
if(type.IsArray && type.GetArrayRank()!=1)
throw new NotSupportedException("Multi-dimensional arrays are not supported");

if(!typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
throw new NotSupportedException("Type "+type.FullName+" does not implement IEnumerable");

if(array==null)
{
Expand Down

0 comments on commit 898a2c5

Please sign in to comment.