public class RPDERestUtils
public static HttpResponseMessage CreateJSONResponse(RpdePage page, HttpRequestMessage req)
var e = JsonConvert.SerializeObject(page,
Newtonsoft.Json.Formatting.None,
new JsonSerializerSettings
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = NoEmptyStringsContractResolver.Instance
var resp = req.CreateResponse(HttpStatusCode.OK);
resp.Headers.CacheControl = new CacheControlHeaderValue()
// Recommended cache settings from:
// https://developer.openactive.io/publishing-data/data-feeds/scaling-feeds
MaxAge = page?.items?.Count > 0 ? TimeSpan.FromHours(1) : TimeSpan.FromSeconds(8)
resp.Content = new StringContent(e, Encoding.UTF8, "application/json");
public class NoEmptyStringsContractResolver : DefaultContractResolver
public static readonly NoEmptyStringsContractResolver Instance = new NoEmptyStringsContractResolver();
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (property.PropertyType == typeof(string))
// Do not include empty strings in JSON output (as per OpenActive Modelling Specification)
property.ShouldSerialize = instance =>
return !string.IsNullOrWhiteSpace(instance.GetType().GetRuntimeProperty(member.Name).GetValue(instance, null) as string);
public static List<TSource> ToListOrNullIfEmpty<TSource>(this IEnumerable<TSource> source)
if (source != null && source.Count() > 0)