I’m using JSON.NET implementation to serialize/deserialize .NET objects to JS and vice versa, all works fine until running GetWCFData() in the following:
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class WebLookup
{
WCFService.WCFServiceClient svc;
IsoDateTimeConverter DateConverter = new IsoDateTimeConverter();
List<WCFContract.Language> Languages { get; set; }
List<WCFContract.Group> Groups { get; set; }
List<WCFContract.User> Users { get; set; }
public WebLookup()
{
DateConverter.DateTimeFormat = "dd/MM/yyyy";
DateConverter.DateTimeStyles = System.Globalization.DateTimeStyles.AssumeLocal;
}
public string GetWCFData()
{
svc = new WCFService.WCFServiceClient();
WebLookup weblookup = new WebLookup();
weblookup.Languages = svc.GetWCFLanguages().ToList();
weblookup.Groups = svc.GetWCFGroups().ToList();
weblookup.Users = svc.GetWCFUsers().ToList();
return JsonConvert.SerializeObject(weblookup, DateConverter);
}
}
Members Languages, Groups, and Users get populated successfully when calling the WCF service but
JsonConvert.SerializeObject(lookup, DateConverter) always returns an empty JSON string to the client (web browser), this is strange as it usually works fine for me in other areas, the only difference is that here I have the populated WebLookup members declared as public properties in the class itself.
Languages, Groups and Users properties need to be declared as Public members in order to be Serialised by either JSON.NET or the built-in JavaScriptSerializer, this is not the case in your code.