Skip to content

Commit 5e3a2e4

Browse files
committed
Add initial files.
0 parents  commit 5e3a2e4

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed

Json.Mapper.csproj

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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>{4761C220-6EA5-4EF0-8184-251D409009F2}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>JsonMapper</RootNamespace>
12+
<AssemblyName>JsonMapper</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.Web" />
37+
<Reference Include="System.Web.Extensions" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Xml" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="Mapper.cs" />
46+
<Compile Include="Properties\AssemblyInfo.cs" />
47+
</ItemGroup>
48+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
49+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
50+
Other similar extension points exist, see Microsoft.Common.targets.
51+
<Target Name="BeforeBuild">
52+
</Target>
53+
<Target Name="AfterBuild">
54+
</Target>
55+
-->
56+
</Project>

Mapper.cs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 Json
8+
{
9+
public class Mapper
10+
{
11+
/// <summary>
12+
/// Dictionary object used to hold the generated string contents.
13+
/// </summary>
14+
public Dictionary<string, List<string>> Contents { get; set; }
15+
16+
/// <summary>
17+
/// Ctor.
18+
/// </summary>
19+
public Mapper(string keyOneName, string keyTwoName, string data)
20+
{
21+
Contents = new Dictionary<string, List<string>>();
22+
23+
// Use static method to deserialize the json data to an object
24+
var results = Deserialize(data);
25+
26+
// Identify the first and the second languages from available
27+
var keyOne = ((Dictionary<string, object>)results.Where(t => t.Key == keyOneName).First().Value);
28+
var keyTwo = ((Dictionary<string, object>)results.Where(t => t.Key == keyTwoName).First().Value);
29+
30+
MapPropertyContents(keyOne);
31+
MapPropertyContents(keyTwo);
32+
}
33+
34+
/// <summary>
35+
/// Static method that just deserialized a json string.
36+
/// </summary>
37+
/// <param name="data"></param>
38+
/// <returns></returns>
39+
public static IDictionary<string, object> Deserialize(string data)
40+
{
41+
JavaScriptSerializer serializer = new JavaScriptSerializer();
42+
return serializer.Deserialize<IDictionary<string, object>>(data);
43+
}
44+
45+
/// <summary>
46+
/// Override to map property values to strings.
47+
/// </summary>
48+
/// <param name="props"></param>
49+
public void MapPropertyContents(Dictionary<string, object> props)
50+
{
51+
MapPropertyContents(props, string.Empty);
52+
}
53+
54+
/// <summary>
55+
/// Maps property values to strings.
56+
/// </summary>
57+
/// <param name="props"></param>
58+
/// <param name="prefix"></param>
59+
public void MapPropertyContents(Dictionary<string, object> props, string prefix)
60+
{
61+
// Loop through each property on the object
62+
foreach (var prop in props)
63+
{
64+
// If the property value is a string, then identify it and add it to the contents.
65+
// If it is anything but a string, then prepend the key as the prefix and attach the dot notation
66+
if (prop.Value.GetType().Equals(typeof(string)))
67+
{
68+
if (Contents.ContainsKey(prefix + prop.Key))
69+
{
70+
Contents.Where(item => item.Key == prefix + prop.Key).First().Value.Add(prop.Value.ToString());
71+
}
72+
else
73+
{
74+
Contents.Add(prefix + prop.Key, new List<string> { prop.Value.ToString() });
75+
}
76+
}
77+
else
78+
{
79+
prefix += prop.Key + ".";
80+
MapPropertyContents((Dictionary<string, object>)prop.Value, prefix);
81+
}
82+
}
83+
}
84+
85+
/// <summary>
86+
/// A ToString override to return the generated results.
87+
/// </summary>
88+
/// <returns></returns>
89+
public override string ToString()
90+
{
91+
StringBuilder builder = new StringBuilder();
92+
93+
// Loop through each composite key in the contents
94+
foreach (var item in Contents)
95+
{
96+
builder.Append("\"" + item.Key + "\"");
97+
98+
// Loop through each translated value in the contents. This would be dynamic to support multiple languages
99+
foreach (var val in item.Value)
100+
{
101+
builder.Append(", \"" + val + "\"");
102+
}
103+
104+
builder.AppendLine();
105+
}
106+
107+
return builder.ToString();
108+
}
109+
}
110+
}

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("JsonMapper")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("JsonMapper")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
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("89625ae0-ef03-49b9-9b49-6996d3a3383c")]
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")]

0 commit comments

Comments
 (0)