Skip to content

Commit 878662a

Browse files
committed
Starting on DynaJSON, added test project
1 parent 38b9394 commit 878662a

File tree

10 files changed

+288
-3
lines changed

10 files changed

+288
-3
lines changed

DynaJSON.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Web.Script.Serialization;
6+
7+
namespace HastyAPI {
8+
public class DynaJSON {
9+
public void Parse(string text) {
10+
var js = new JavaScriptSerializer().DeserializeObject(text);
11+
}
12+
}
13+
}

DynaXML.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static dynamic Parse(XDocument xml) {
2020
return result;
2121
}
2222

23-
public static void AddElement(XElement el, IDictionary<string, object> parent) {
23+
private static void AddElement(XElement el, IDictionary<string, object> parent) {
2424
var name = el.Name.LocalName;
2525

2626
// check for multiple elements with the same name and convert to list
@@ -76,7 +76,7 @@ private static string Indent(int nestlevel) {
7676
return new string(' ', 4 * nestlevel);;
7777
}
7878

79-
public static string DynamicToString(IDictionary<string, object> obj, int nestlevel = 0) {
79+
private static string DynamicToString(IDictionary<string, object> obj, int nestlevel = 0) {
8080
var indent = Indent(nestlevel);
8181

8282
var str = "";

Dynamic.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace HastyAPI {
7+
public static class Dynamic {
8+
public static List<dynamic> AsList(object obj) {
9+
var list = obj as List<dynamic>;
10+
if(list != null) return list;
11+
12+
return new List<dynamic>(new dynamic[] { obj });
13+
}
14+
}
15+
}

HastyAPI.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<Reference Include="System" />
3535
<Reference Include="System.Core" />
3636
<Reference Include="System.Web" />
37+
<Reference Include="System.Web.Extensions" />
3738
<Reference Include="System.Xml.Linq" />
3839
<Reference Include="System.Data.DataSetExtensions" />
3940
<Reference Include="Microsoft.CSharp" />
@@ -43,12 +44,17 @@
4344
<ItemGroup>
4445
<Compile Include="APIRequest.cs" />
4546
<Compile Include="APIResponse.cs" />
47+
<Compile Include="DynaJSON.cs" />
48+
<Compile Include="Dynamic.cs" />
4649
<Compile Include="DynaXML.cs" />
4750
<Compile Include="Properties\AssemblyInfo.cs" />
4851
<Compile Include="Extensions.cs" />
52+
<Compile Include="URL.cs" />
4953
</ItemGroup>
5054
<ItemGroup>
51-
<None Include="license" />
55+
<None Include="HastyAPI.nuspec">
56+
<SubType>Designer</SubType>
57+
</None>
5258
<None Include="readme.md" />
5359
</ItemGroup>
5460
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

HastyAPI.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 11.00
33
# Visual Studio 2010
44
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HastyAPI", "HastyAPI.csproj", "{AC6ED39A-07BB-4192-89A0-30B07E0F7C2F}"
55
EndProject
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{A62093AE-981B-4DA7-B29F-DA4B633EBFD1}"
7+
EndProject
68
Global
79
GlobalSection(SolutionConfigurationPlatforms) = preSolution
810
Debug|Any CPU = Debug|Any CPU
@@ -13,6 +15,10 @@ Global
1315
{AC6ED39A-07BB-4192-89A0-30B07E0F7C2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
1416
{AC6ED39A-07BB-4192-89A0-30B07E0F7C2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
1517
{AC6ED39A-07BB-4192-89A0-30B07E0F7C2F}.Release|Any CPU.Build.0 = Release|Any CPU
18+
{A62093AE-981B-4DA7-B29F-DA4B633EBFD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{A62093AE-981B-4DA7-B29F-DA4B633EBFD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{A62093AE-981B-4DA7-B29F-DA4B633EBFD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{A62093AE-981B-4DA7-B29F-DA4B633EBFD1}.Release|Any CPU.Build.0 = Release|Any CPU
1622
EndGlobalSection
1723
GlobalSection(SolutionProperties) = preSolution
1824
HideSolutionNode = FALSE

Tests/DynaJSONFacts.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tests {
7+
public class DynaJSONFacts {
8+
}
9+
}

Tests/DynaXMLFacts.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.IO;
6+
using System.Xml.Linq;
7+
using HastyAPI;
8+
9+
namespace Tests {
10+
public class DynaXMLFacts {
11+
public static XDocument GetXML(string name) {
12+
var path = Path.Combine(Directory.GetCurrentDirectory(), @"..\..\Tests\Data\" + name + ".xml");
13+
return XDocument.Load(path);
14+
}
15+
16+
//[Fact]
17+
public void Can_Get_Root() {
18+
var o = GetXML("client_get_response").ToDynamic();
19+
//Assert.NotNull(o.response);
20+
}
21+
22+
//[Fact]
23+
public void Can_Get_One_Child() {
24+
var o = GetXML("client_get_response").ToDynamic();
25+
//Assert.NotNull(o.response.client);
26+
}
27+
28+
//[Fact]
29+
public void Can_Get_Content_Value() {
30+
var o = GetXML("client_get_response").ToDynamic();
31+
//Assert.Equal("12", o.response.client.client_id);
32+
}
33+
34+
//[Fact]
35+
public void Can_Get_Attribute_Value_As_Well_As_Content_Value() {
36+
var o = GetXML("client_get_response").ToDynamic();
37+
38+
//Assert.Equal("AUD", o.response.client.credit.currency);
39+
//Assert.Equal("true", o.response.client.credit.deprecated);
40+
//Assert.Equal("0", o.response.client.credit.value);
41+
}
42+
43+
//[Fact]
44+
public void Simple_Element_Lists_Work() {
45+
var o = GetXML("dummy_lists").ToDynamic();
46+
47+
//Assert.Equal(2, o.lists.simple.item.Count);
48+
//Assert.Throws<Microsoft.CSharp.RuntimeBinder.RuntimeBinderException>(() => o.lists.simple.value);
49+
//Assert.Equal("one", o.lists.simple.item[0]);
50+
//Assert.Equal("two", o.lists.simple.item[1]);
51+
}
52+
53+
//[Fact]
54+
public void Complex_Element_Lists_Work() {
55+
var o = GetXML("dummy_lists").ToDynamic();
56+
57+
//Assert.Equal(3, o.lists.complex.item.Count);
58+
//Assert.Equal("one", o.lists.complex.item[0].text);
59+
//Assert.Equal("complex", o.lists.complex.item[1].type);
60+
//Assert.Equal("very_complex", o.lists.complex.item[2].type);
61+
}
62+
63+
//[Fact]
64+
public void Can_Call_As_List_On_List() {
65+
var o = GetXML("dummy_lists").ToDynamic();
66+
67+
var list = Dynamic.AsList(o.lists.simple.item);
68+
//Assert.Equal(2, list.Count);
69+
}
70+
71+
//[Fact]
72+
public void Can_Call_As_List_On_A_Non_List() {
73+
var o = GetXML("dummy_lists").ToDynamic();
74+
75+
var list = Dynamic.AsList(o.lists.simple);
76+
//Assert.Equal(1, list.Count);
77+
}
78+
79+
public class Serialization {
80+
81+
public void Test_Dummy() {
82+
var o = GetXML("dummy_lists").ToDynamic();
83+
Console.WriteLine(o.ToString());
84+
}
85+
86+
public void Test_Real() {
87+
var o = GetXML("client_get_response").ToDynamic();
88+
89+
Console.WriteLine(o.response.client.credit.ToString());
90+
}
91+
}
92+
}
93+
}

Tests/Properties/AssemblyInfo.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Tests")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Tests")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("78366d16-038b-438f-b1d1-ae78492e2d6a")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Tests/Tests.csproj

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{A62093AE-981B-4DA7-B29F-DA4B633EBFD1}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Tests</RootNamespace>
12+
<AssemblyName>Tests</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Xml" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="DynaJSONFacts.cs" />
44+
<Compile Include="DynaXMLFacts.cs" />
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<ProjectReference Include="..\HastyAPI.csproj">
49+
<Project>{AC6ED39A-07BB-4192-89A0-30B07E0F7C2F}</Project>
50+
<Name>HastyAPI</Name>
51+
</ProjectReference>
52+
</ItemGroup>
53+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
55+
Other similar extension points exist, see Microsoft.Common.targets.
56+
<Target Name="BeforeBuild">
57+
</Target>
58+
<Target Name="AfterBuild">
59+
</Target>
60+
-->
61+
</Project>

URL.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.ComponentModel;
6+
7+
namespace HastyAPI {
8+
/// <summary>
9+
/// A more practical version of Uri.
10+
/// </summary>
11+
[TypeConverter(typeof(URLConverter))]
12+
public class URL {
13+
string _value;
14+
15+
public URL(string value) {
16+
_value = value;
17+
}
18+
19+
// convert from URL to string
20+
public static implicit operator string(URL value) {
21+
return value._value;
22+
}
23+
24+
// convert from string to URL
25+
public static implicit operator URL(String value) {
26+
if(!Uri.IsWellFormedUriString(value, UriKind.Absolute)) {
27+
throw new Exception("That doesn't look like a URL");
28+
}
29+
return new URL(value);
30+
}
31+
32+
public static string operator +(URL basePath, string relPath) {
33+
Uri combined;
34+
if(Uri.TryCreate(new Uri(basePath._value), relPath, out combined)) return new URL(combined.ToString());
35+
throw new ArgumentException("Couldn't combine base path \"" + basePath + "\" with relative path \"" + relPath + "\"");
36+
}
37+
38+
public class URLConverter : TypeConverter {
39+
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
40+
if(value is string) return new URL((string)value);
41+
42+
return base.ConvertFrom(context, culture, value);
43+
}
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)