Skip to content

Instantly share code, notes, and snippets.

@amilos
Last active May 29, 2017 18:29
Show Gist options
  • Save amilos/257cdb368f1f3795d39675401e9b2985 to your computer and use it in GitHub Desktop.
Save amilos/257cdb368f1f3795d39675401e9b2985 to your computer and use it in GitHub Desktop.
Example of model setup for JSON serialization with extension data
namespace Asseco.Rest.Models
{
[DataContract]
public partial class EventRecord
{
[JsonExtensionData]
internal Dictionary<string, object> udf;
public EventRecord(string EventKind = default(string))
{
this.EventKind = EventKind;
udf = new Dictionary<string, object>();
}
[DataMember(Name = "event-kind")]
public string EventKind { get; set; }
[DataMember(Name = "version")]
public int? Version { get; set; }
[DataMember(Name = "occurred-on")]
public DateTime? OccurredOn { get; set; }
[DataMember(Name = "event-id")]
public Guid? EventId { get; set; }
[DataMember(Name = "aggregate-kind")]
public string AggregateKind { get; set; }
[DataMember(Name = "aggregate-id")]
public string AggregateId { get; set; }
[DataMember(Name = "customer-number")]
public string CustomerNumber { get; set; }
[DataMember(Name = "sync-timestamp")]
public string SyncTimestamp { get; set; }
//convenient access to udf fields
public object this[string key]
{
get
{
// If this key is in the dictionary, return its value.
object tmp;
if (udf.TryGetValue(key, out tmp))
{
return tmp;
}
else
{
return null;
}
}
set
{
// If this key is in the dictionary, change its value.
if (udf.ContainsKey(key))
{
// The key was found; change its value.
udf[key] = value;
}
else
{
// This key is not in the dictionary; add this key/value pair.
udf.Add(key, value);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment