diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0fcb27e --- /dev/null +++ b/.gitignore @@ -0,0 +1,331 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +.vs/ + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ +output/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml +*.publishproj + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +############# +## Windows detritus +############# + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store + + +############# +## Python +############# + +*.py[cod] + +# Packages +*.egg +*.egg-info +dist/ +build/ +eggs/ +parts/ +var/ +sdist/ +develop-eggs/ +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg +# Build Folders (you can keep bin if you'd like, to store dlls and pdbs) +[Bb]in/ +[Oo]bj/ + +# mstest test results +TestResults + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Rr]elease/ +x64/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.log +*.vspscc +*.vssscc +.builds + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper* + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Publish Web Output +*.Publish.xml + +# NuGet Packages Directory +packages + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +[Bb]in +[Oo]bj +sql +TestResults +[Tt]est[Rr]esult* +*.Cache +ClientBin +[Ss]tyle[Cc]op.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML + +*.DotSettings +clrdev.setting +.DS_Store \ No newline at end of file diff --git a/UnitTests/Program.cs b/UnitTests/Program.cs new file mode 100644 index 0000000..3f440e7 --- /dev/null +++ b/UnitTests/Program.cs @@ -0,0 +1,12 @@ + +namespace Test +{ + static class Program + { + static void Main() + { + //tests.CustomTypes_Generic(); + tests.CustomTypes_Generic2(); + } + } +} \ No newline at end of file diff --git a/UnitTests/Tests.cs b/UnitTests/Tests.cs index 1905dbe..a31d7e3 100644 --- a/UnitTests/Tests.cs +++ b/UnitTests/Tests.cs @@ -291,7 +291,7 @@ public class RetNestedclass #endregion - [TestFixtureSetUp] + [OneTimeSetUp] public static void setup() { //fastJSON.JSON.Parameters = new JSONParameters(); @@ -1864,24 +1864,202 @@ public static void comments() Assert.AreEqual(2, (o as IDictionary).Count); } - public class ctype + public struct Point { - public System.Net.IPAddress ip; + public int x; + public float y; + } + public class Complex + { + public int i; + public List l; } [Test] - public static void CustomTypes() + public static void CustomTypes_Generic() + { + JSON.RegisterCustomType( + pt => string.Format("[{0},{1}]", pt.x, pt.y), + (o, cb) => + { + var l = o as List; + return new Point() { x = Convert.ToInt32(l[0]), y = Convert.ToSingle(l[1]) }; + }); + // + JSON.RegisterCustomType( + c => string.Format("[{0},{1}]", c.i, JSON.ToJSON(c.l)), + (o, cb) => + { + var l = o as List; + return new Complex() { i = Convert.ToInt32(l[0]), l = (List)cb(l[1], typeof(List)) }; + }); + + var pt1 = new Point() { x = 1, y = 2.3f }; + var s = JSON.ToJSON(pt1); + Console.WriteLine(s); + var pt2 = JSON.ToObject(s); + Assert.AreEqual(pt1, pt2); + + var c1 = new Complex() + { + i = 9, + l = new List() { + new Point[] { + new Point() { x = 1, y = 2.3f }, + new Point() { x = 2, y = 3.4f }, + }, + new Point[] { + new Point() { x = 5, y = 6.7f }, + }, + } + }; + s = JSON.ToJSON(c1); + Console.WriteLine(s); + var c2 = JSON.ToObject(s); + Assert.AreEqual(s, JSON.ToJSON(c2)); + + var d1 = new Dictionary>>() { + {"a", new List> { + new List { + new Point() { x = 1, y = 2.3f }, + new Point() { x = 2, y = 3.4f }, + }, + new List { + new Point() { x = 5, y = 6.7f }, + }, + }} + }; + s = JSON.ToJSON(d1); + Console.WriteLine(s); + var d2 = JSON.ToObject(s, d1.GetType()); + Assert.AreEqual(s, JSON.ToJSON(d2)); + } + + + [Test] + public static void CustomTypes_Generic2() { - var ip = new ctype(); - ip.ip = System.Net.IPAddress.Loopback; + JSON.RegisterCustomType>( + //since we know that our point will be serialized to list of objects + pt => string.Format("[{0},{1}]", pt.x, pt.y), + (l, cb) => + { + return new Point() { x = Convert.ToInt32(l[0]), y = Convert.ToSingle(l[1]) }; + }); + // + JSON.RegisterCustomType>( + //since we know that our complex type will be serialized to list of objects + c => string.Format("[{0},{1}]", c.i, JSON.ToJSON(c.l)), + (l, cb) => + { + return new Complex() { i = Convert.ToInt32(l[0]), l = (List)cb(l[1], typeof(List)) }; + }); + + //---------------- + var pt1 = new Point() { x = 1, y = 2.3f }; + var s = JSON.ToJSON(pt1); + Console.WriteLine(s); + var pt2 = JSON.ToObject(s); + Assert.AreEqual(pt1, pt2); - JSON.RegisterCustomType(typeof(System.Net.IPAddress), - (x) => { return x.ToString(); }, - (x) => { return System.Net.IPAddress.Parse(x); }); + var c1 = new Complex() + { + i = 9, + l = new List() { + new Point[] { + new Point() { x = 1, y = 2.3f }, + new Point() { x = 2, y = 3.4f }, + }, + new Point[] { + new Point() { x = 5, y = 6.7f }, + }, + } + }; + s = JSON.ToJSON(c1); + Console.WriteLine(s); + var c2 = JSON.ToObject(s); + Assert.AreEqual(s, JSON.ToJSON(c2)); + + var d1 = new Dictionary>>() { + {"a", new List> { + new List { + new Point() { x = 1, y = 2.3f }, + new Point() { x = 2, y = 3.4f }, + }, + new List { + new Point() { x = 5, y = 6.7f }, + }, + }} + }; + s = JSON.ToJSON(d1); + Console.WriteLine(s); + var d2 = JSON.ToObject(s, d1.GetType()); + Assert.AreEqual(s, JSON.ToJSON(d2)); + } + [Test] + public static void CustomTypes() + { + JSON.RegisterCustomType(typeof(Point), + (o) => + { + var pt = (Point)o; + return string.Format("[{0},{1}]", pt.x, pt.y); + }, + (o, cb) => + { + var l = o as List; + return new Point() { x = Convert.ToInt32(l[0]), y = Convert.ToSingle(l[1]) }; + }); + JSON.RegisterCustomType(typeof(Complex), + (o) => + { + var c = (Complex)o; + return string.Format("[{0},{1}]", c.i, JSON.ToJSON(c.l)); + }, + (o, cb) => + { + var l = o as List; + return new Complex() { i = Convert.ToInt32(l[0]), l = (List)cb(l[1], typeof(List)) }; + }); - var s = JSON.ToJSON(ip); + var pt1 = new Point() { x = 1, y = 2.3f }; + var s = JSON.ToJSON(pt1); + Console.WriteLine(s); + var pt2 = JSON.ToObject(s); + Assert.AreEqual(pt1, pt2); - var o = JSON.ToObject(s); - Assert.AreEqual(ip.ip, o.ip); + var c1 = new Complex() + { + i = 9, + l = new List() { + new Point[] { + new Point() { x = 1, y = 2.3f }, + new Point() { x = 2, y = 3.4f }, + }, + new Point[] { + new Point() { x = 5, y = 6.7f }, + }, + } + }; + s = JSON.ToJSON(c1); + Console.WriteLine(s); + var c2 = JSON.ToObject(s); + Assert.AreEqual(s, JSON.ToJSON(c2)); + + var d1 = new Dictionary>>() { + {"a", new List> { + new List { + new Point() { x = 1, y = 2.3f }, + new Point() { x = 2, y = 3.4f }, + }, + new List { + new Point() { x = 5, y = 6.7f }, + }, + }} + }; + s = JSON.ToJSON(d1); + Console.WriteLine(s); + var d2 = JSON.ToObject(s, d1.GetType()); + Assert.AreEqual(s, JSON.ToJSON(d2)); } [Test] @@ -2813,18 +2991,18 @@ public static void ConvertTest() var data2 = JSON.ToObject(jsonData); // OK, since data member name is "foo" which is all in lower case - Assert.AreEqual(data.Foo ,data2.Foo); + Assert.AreEqual(data.Foo, data2.Foo); // Fails, since data member name is "Bar", but the library looks for "bar" when setting the value - Assert.AreEqual(data.Bar , data2.Bar); + Assert.AreEqual(data.Bar, data2.Bar); } - public class test { } + public class test { } [Test] public static void ArrayOfObjectExtOff() { - var s = JSON.ToJSON(new test[] { new test(), new test() }, new JSONParameters { UseExtensions = false}); + var s = JSON.ToJSON(new test[] { new test(), new test() }, new JSONParameters { UseExtensions = false }); var o = JSON.ToObject(s); Console.WriteLine(o.GetType().ToString()); Assert.AreEqual(typeof(test[]), o.GetType()); diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index ce328b1..c7bccba 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -1,12 +1,13 @@  + Debug AnyCPU 8.0.30703 2.0 {F4BFF0D2-45F6-413B-B1E7-BC4A0D6596FC} - Library + Exe Properties UnitTests UnitTests @@ -21,6 +22,8 @@ 3.5 ..\ true + + true @@ -42,21 +45,33 @@ 4 false + + + - C:\Program Files (x86)\NUnit 2.6.4\bin\framework\nunit.framework.dll + + ..\packages\NUnit.3.10.1\lib\net40\nunit.framework.dll + + + + + + + + {207e91b1-c9f8-4913-88e1-3549ef5f3273} @@ -64,6 +79,12 @@ + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + +