|
| 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 | +} |
0 commit comments