using System.Collections.Generic; using System.Text; namespace IFramework.Serialization { public class DictionaryFormatter : StringFormatter> { const string keyChar = "k"; const string valueChar = "v"; Dictionary dic = new Dictionary(); StringFormatter k = StringConvert.GetFormatter(typeof(K)) as StringFormatter; StringFormatter v = StringConvert.GetFormatter(typeof(V)) as StringFormatter; public override void ConvertToString(Dictionary t, StringBuilder builder) { if (t == null || t.Count == 0) return; builder.Append(StringConvert.midLeftBound); int count = 0; foreach (var item in t) { var _key = item.Key; var _value = item.Value; count++; builder.Append(StringConvert.leftBound); builder.Append(keyChar); builder.Append(StringConvert.colon); k.ConvertToString(_key, builder); builder.Append(StringConvert.dot); builder.Append(valueChar); builder.Append(StringConvert.colon); v.ConvertToString(_value, builder); builder.Append(StringConvert.rightBound); if (count != t.Count) { builder.Append(StringConvert.dot.ToString()); } } builder.Append(StringConvert.midRightBound); } private void Read(string self, Dictionary result) { K lastKey = default(K); ObjectFormatter.ReadObject(self, (fieldName, inner) => { if (fieldName == keyChar) { k.TryConvert(inner, out lastKey); } else if (fieldName == valueChar) { V value; if (v.TryConvert(inner, out value)) { result.Add(lastKey, value); } else { result.Add(lastKey, default(V)); } } }); } public override bool TryConvert(string self, out Dictionary result) { dic.Clear(); bool bo = ListFormatter.ReadArray(self, (inner) => { if (inner.EndsWith(StringConvert.dot.ToString())) { inner = inner.Remove(inner.Length - 1, 1); } Read(inner, dic); }); if (bo) { result = new Dictionary(dic); } else { result = MakeDefault(); } return bo; } } }