Skip to content

Instantly share code, notes, and snippets.

@mdissel
Last active August 29, 2015 14:06
Show Gist options
  • Save mdissel/6c9d8967a0c9e17675a5 to your computer and use it in GitHub Desktop.
Save mdissel/6c9d8967a0c9e17675a5 to your computer and use it in GitHub Desktop.
mailchimp mergevar with fieldvalues, MergeVar implements IDictionary to store field/value information and a custom (de-)serialize option
public class MergeVarFieldValues : MergeVar, IDictionary<string, object>
{
private IDictionary<string, object> _local = new Dictionary<string, object>(StringComparer.CurrentCultureIgnoreCase);
public void Add(string key, object value) {
_local.Add(key, value);
}
public bool ContainsKey(string key) {
return _local.ContainsKey(key);
}
public ICollection<string> Keys {
get { return _local.Keys; }
}
public bool Remove(string key) {
return _local.Remove(key);
}
public bool TryGetValue(string key, out object value) {
return _local.TryGetValue(key, out value);
}
public ICollection<object> Values {
get { return _local.Values; }
}
public object this[string key] {
get {
return _local[key];
}
set {
_local[key] = value;
}
}
public void Add(KeyValuePair<string, object> item) {
_local.Add(item);
}
public void Clear() {
_local.Clear();
}
public bool Contains(KeyValuePair<string, object> item) {
return _local.Contains(item);
}
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex) {
_local.CopyTo(array, arrayIndex);
}
public int Count {
get { return _local.Count; }
}
public bool IsReadOnly {
get { return _local.IsReadOnly; }
}
public bool Remove(KeyValuePair<string, object> item) {
return _local.Remove(item);
}
public IEnumerator<KeyValuePair<string, object>> GetEnumerator() {
return _local.GetEnumerator(); ;
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return _local.GetEnumerator();
}
public static void RegisterMergeVarFieldValuesJsonConversion() {
JsConfig<MergeVarFieldValues>.RawSerializeFn = x => {
var dict = x.ToDictionary(a => a.Key, b=> b.Value);
foreach (System.Reflection.PropertyInfo item in x.GetType().GetSerializableProperties()) {
switch (item.Name) {
case "Keys":
case "Count":
case "Values":
case "IsReadOnly":
continue;
default:
break;
}
object val = item.GetValue(x, null);
if (val == null) {
continue;
}
string name = item.Name;
System.Runtime.Serialization.DataMemberAttribute dataMember = item.GetDataMember();
if (dataMember != null) {
name = dataMember.Name;
}
dict[name] = val;
}
return dict.ToJson();
};
JsConfig<MergeVarFieldValues>.RawDeserializeFn = x => {
return JsonObject.Parse(x).ConvertTo<MergeVarFieldValues>( y => {
MergeVarFieldValues result = new MergeVarFieldValues();
foreach (KeyValuePair<string, string> item in y) {
switch (item.Key.ToUpper()) {
case "GROUPINGS":
result.Groupings = y.Get<List<Grouping>>(item.Key);
break;
case "groupings":
result.Groupings = y.Get<List<Grouping>>(item.Key);
break;
case "mc_language":
result.Language = y.Get(item.Key);
break;
case "mc_location":
result.LocationData = y.Get<MCLocation>(item.Key);
break;
case "new-email":
result.NewEmail = y.Get(item.Key);
break;
case "mc_notes-email":
result.Notes = y.Get<List<MCNote>>(item.Key);
break;
case "optin_ip":
result.OptInIP = y.Get(item.Key);
break;
case "optin_time":
result.OptInTime = y.Get(item.Key);
break;
default:
result.Add(item.Key, y.Get<object>(item.Key));
break;
}
}
return result;
});
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment