public UserModel(JsonValue json, IJsonContext context) { var dict = JsonDictionary.FromValue(json); bool wasRelaxed = context.RelaxedNumericConversion; context.RelaxedNumericConversion = true; UserName = dict.Get<string>("username", context); AccountType = dict.Get<string>("account_type", context, "free"); SignUpDate = new DateTime(1970,1,1).AddSeconds(dict.Get<double>("sign_up_date", context)); if (dict.Items.ContainsKey("profile_image")) ProfileImage = new Uri(dict.Get<string>("profile_image", context), UriKind.Absolute); Sets = dict.Get<List<SetModel>>("sets", context); FavouriteSets = dict.Get<List<SetModel>>("favorite_sets", context); Groups = dict.Get<List<GroupModel>>("groups", context); if (dict.Items.ContainsKey("statistics")) { var stats = dict.GetSubDictionary("statistics"); foreach (var k in stats.Items) { switch (k.Key) { case "study_session_count": StudySessionCount = context.FromJson<int>(k.Value); break; case "message_count": MessageCount = context.FromJson<int>(k.Value); break; case "total_answer_count": TotalAnswerCount = context.FromJson<int>(k.Value); break; case "public_sets_created": PublicSetsCreated = context.FromJson<int>(k.Value); break; case "public_terms_entered": PublicTermsEntered = context.FromJson<int>(k.Value); break; } } } context.RelaxedNumericConversion = wasRelaxed; }
public PracticeItem(JsonValue json, IJsonContext ctx) { var dict = JsonDictionary.FromValue(json); term = ctx.FromJson <string>(dict["term"]); definition = ctx.FromJson <string>(dict["definition"]); }
public SetModel(JsonValue json, IJsonContext context) { var dict = JsonDictionary.FromValue(json); SetID = dict.Get <long>("id", context); Uri = new Uri(dict.Get <string>("url", context)); Title = dict.Get <string>("title", context); Author = dict.Get <string>("created_by", context); Description = dict.Get <string>("description", context, null); TermCount = dict.Get <int>("term_count", context); Created = new DateTime(1970, 1, 1).AddSeconds(dict.Get <double>("created_date", context)); Modified = new DateTime(1970, 1, 1).AddSeconds(dict.Get <double>("modified_date", context)); Subjects = context.FromJson <List <string> >(dict["subjects"]); Visibility = SetPermissions.ParseVisibility(dict.Get <string>("visibility", context)); EditPermissions = SetPermissions.ParseEditPermissions(dict.Get <string>("editable", context)); HasAccess = dict.Get <bool>("has_access", context); HasDiscussion = dict.Get <bool>("has_discussion", context, false); TermLanguageCode = dict.Get <string>("lang_terms", context, null); DefinitionLanguageCode = dict.Get <string>("lang_definitions", context, null); if (dict.Items.ContainsKey("terms")) { Terms = context.FromJson <List <TermModel> >(dict["terms"]); } }
public RoundInfo(JsonValue json, IJsonContext ctx) { var dict = JsonDictionary.FromValue(json); RoundNumber = ctx.FromJson <int>(dict["roundNumber"]); Score = ctx.FromJson <int>(dict["score"]); ItemCount = ctx.FromJson <int>(dict["itemCount"]); }
public TermModel(JsonValue json, IJsonContext context) { if (json == null) throw new ArgumentNullException(nameof(json)); if (context == null) throw new ArgumentNullException(nameof(context)); var dict = JsonDictionary.FromValue(json); TermID = context.FromJson<long>(dict["id"]); Term = context.FromJson<string>(dict["term"]); Definition = context.FromJson<string>(dict["definition"]); if (dict.Items.ContainsKey("image") && dict.Items["image"] is JsonDictionary) { var imageDict = dict.GetSubDictionary("image"); ImageWidth = imageDict.Get<int>("width", context); ImageHeight = imageDict.Get<int>("height", context); } }
public UserModel(JsonValue json, IJsonContext context) { var dict = JsonDictionary.FromValue(json); bool wasRelaxed = context.RelaxedNumericConversion; context.RelaxedNumericConversion = true; UserName = dict.Get <string>("username", context); AccountType = dict.Get <string>("account_type", context, "free"); SignUpDate = new DateTime(1970, 1, 1).AddSeconds(dict.Get <double>("sign_up_date", context)); if (dict.Items.ContainsKey("profile_image")) { ProfileImage = new Uri(dict.Get <string>("profile_image", context), UriKind.Absolute); } Sets = dict.Get <List <SetModel> >("sets", context); FavouriteSets = dict.Get <List <SetModel> >("favorite_sets", context); Groups = dict.Get <List <GroupModel> >("groups", context); if (dict.Items.ContainsKey("statistics")) { var stats = dict.GetSubDictionary("statistics"); foreach (var k in stats.Items) { switch (k.Key) { case "study_session_count": StudySessionCount = context.FromJson <int>(k.Value); break; case "message_count": MessageCount = context.FromJson <int>(k.Value); break; case "total_answer_count": TotalAnswerCount = context.FromJson <int>(k.Value); break; case "public_sets_created": PublicSetsCreated = context.FromJson <int>(k.Value); break; case "public_terms_entered": PublicTermsEntered = context.FromJson <int>(k.Value); break; } } } context.RelaxedNumericConversion = wasRelaxed; }
public T Get <T>(string key, IJsonContext context, T defaultValue) { JsonValue json; if (Items.TryGetValue(key, out json)) { return(context.FromJson <T>(json)); } return(defaultValue); }
public SetModel(JsonValue json, IJsonContext context) { var dict = JsonDictionary.FromValue(json); SetID = dict.Get<long>("id", context); Uri = new Uri(dict.Get<string>("url", context)); Title = dict.Get<string>("title", context); Author = dict.Get<string>("created_by", context); Description = dict.Get<string>("description", context, null); TermCount = dict.Get<int>("term_count", context); Created = new DateTime(1970, 1, 1).AddSeconds(dict.Get<double>("created_date", context)); Modified = new DateTime(1970, 1, 1).AddSeconds(dict.Get<double>("modified_date", context)); Subjects = context.FromJson<List<string>>(dict["subjects"]); Visibility = SetPermissions.ParseVisibility(dict.Get<string>("visibility", context)); EditPermissions = SetPermissions.ParseEditPermissions(dict.Get<string>("editable", context)); HasAccess = dict.Get<bool>("has_access", context); HasDiscussion = dict.Get<bool>("has_discussion", context, false); TermLanguageCode = dict.Get<string>("lang_terms", context, null); DefinitionLanguageCode = dict.Get<string>("lang_definitions", context, null); if (dict.Items.ContainsKey("terms")) Terms = context.FromJson<List<TermModel>>(dict["terms"]); }
protected MruList(JsonValue json, IJsonContext context) { var dict = json as JsonDictionary; if (dict == null) { throw new JsonConvertException("Expected a JSON dictionary"); } foreach (var k in dict.Items) { switch (k.Key) { case "Entries": Entries = context.FromJson <List <T> >(k.Value); break; case "MaximumSize": MaximumSize = context.FromJson <int>(k.Value); break; } } }
public TermModel(JsonValue json, IJsonContext context) { if (json == null) { throw new ArgumentNullException("json"); } if (context == null) { throw new ArgumentNullException("context"); } var dict = JsonDictionary.FromValue(json); TermID = context.FromJson <long>(dict["id"]); Term = context.FromJson <string>(dict["term"]); Definition = context.FromJson <string>(dict["definition"]); if (dict.Items.ContainsKey("image") && dict.Items["image"] is JsonDictionary) { var imageDict = dict.GetSubDictionary("image"); ImageWidth = imageDict.Get <int>("width", context); ImageHeight = imageDict.Get <int>("height", context); } }
public Group(JsonValue json, IJsonContext context) : this() { var dict = json as JsonDictionary; if (dict == null) throw new JsonConvertException("Expected a JSON dictionary to be converted to a GroupInfo"); bool hasID = false, hasName = false; foreach (var item in dict.Items) { switch (item.Key) { case "id": hasID = true; ID = context.FromJson<long>(item.Value); break; case "name": hasName = true; Name = context.FromJson<string>(item.Value); break; case "description": Description = context.FromJson<string>(item.Value); break; case "user_count": UserCount = context.FromJson<int>(item.Value); break; case "created_date": Created = new DateTime(context.FromJson<long>(item.Value)); break; case "is_public": IsPublic = context.FromJson<bool>(item.Value); break; case "has_password": HasPassword = context.FromJson<bool>(item.Value); break; case "has_access": HasAccess = context.FromJson<bool>(item.Value); break; case "has_discussion": HasDiscussion = context.FromJson<bool>(item.Value); break; case "member_add_sets": MemberAddSets = context.FromJson<bool>(item.Value); break; case "sets": var sets = context.FromJson<List<Set>>(item.Value); AddSets(sets); break; } } if (!hasID || !hasName) throw new JsonConvertException("Server did not supply group ID or name"); }
public PracticeItem(JsonValue json, IJsonContext ctx) { var dict = JsonDictionary.FromValue(json); term = ctx.FromJson<string>(dict["term"]); definition = ctx.FromJson<string>(dict["definition"]); }
public PracticeViewModel(JsonValue json, IJsonContext ctx) { var dict = JsonDictionary.FromValue(json); items = ctx.FromJson <List <PracticeItem> >(dict["items"]); nextItems = ctx.FromJson <List <PracticeItem> >(dict["nextItems"]); failed = ctx.FromJson <bool>(dict["failed"]); currentItem = ctx.FromJson <PracticeItem>(dict["currentItem"]); currentIndex = ctx.FromJson <int>(dict["currentIndex"]); itemCount = ctx.FromJson <int>(dict["itemCount"]); score = ctx.FromJson <int>(dict["score"]); round = ctx.FromJson <int>(dict["round"]); lastRoundInfo = ctx.FromJson <RoundInfo>(dict["lastRoundInfo"]); allRounds = new ObservableCollection <RoundInfo>(ctx.FromJson <List <RoundInfo> >(dict["allRounds"])); maxItemCount = ctx.FromJson <int>(dict["maxItemCount"]); title = ctx.FromJson <string>(dict["title"]); }
public Set(JsonValue json, IJsonContext context) : this() { var dict = json as JsonDictionary; if (dict == null) { throw new JsonConvertException("Expected a JSON dictionary to be converted to a SetInfo"); } if (!dict.Items.ContainsKey("id")) { throw new JsonConvertException("Expected SetInfo JSON to contain an 'id' property"); } foreach (var k in dict.Items) { switch (k.Key) { case "id": ID = context.FromJson <long>(k.Value); break; case "url": Uri = context.FromJson <string>(k.Value); break; case "title": Title = context.FromJson <string>(k.Value); break; case "created_by": Author = context.FromJson <string>(k.Value); break; case "description": Description = context.FromJson <string>(k.Value); break; case "term_count": TermCount = context.FromJson <int>(k.Value); break; case "created_date": Created = new DateTime(1970, 1, 1).AddSeconds(context.FromJson <long>(k.Value)); break; case "modified_date": Modified = new DateTime(1970, 1, 1).AddSeconds(context.FromJson <long>(k.Value)); break; case "subjects": AddSubjects(context.FromJson <List <string> >(k.Value)); break; case "visibility": Visibility = SetPermissions.ParseVisibility(context.FromJson <string>(k.Value)); break; case "editable": Editable = SetPermissions.ParseEditPermissions(context.FromJson <string>(k.Value)); break; case "has_access": HasAccess = context.FromJson <bool>(k.Value); break; case "has_discussion": HasDiscussion = context.FromJson <bool>(k.Value); break; case "lang_terms": TermLanguageCode = context.FromJson <string>(k.Value); break; case "lang_definitions": DefinitionLanguageCode = context.FromJson <string>(k.Value); break; case "terms": var list = new List <Term>(); if (k.Value is JsonArray) { foreach (var termJson in ((JsonArray)k.Value).Items) { if (!(termJson is JsonDictionary)) { throw new JsonConvertException("Expected SetInfo.Terms to be an array of JSON dictionaries"); } var term = new Term { ID = context.FromJson <long>(((JsonDictionary)termJson).Items["id"]), TermValue = context.FromJson <string>(((JsonDictionary)termJson).Items["term"]), Definition = context.FromJson <string>(((JsonDictionary)termJson).Items["definition"]) }; if (term.TermValue == null || term.Definition == null) { throw new JsonConvertException("Either term or definition was not group when converting from JSON to Set"); } list.Add(term); } } else { throw new JsonConvertException("Expected SetInfo.Terms to be an array"); } AddTerms(list); break; } } // TODO: Validate that important fields are defined }
public Group(JsonValue json, IJsonContext context) : this() { var dict = json as JsonDictionary; if (dict == null) { throw new JsonConvertException("Expected a JSON dictionary to be converted to a GroupInfo"); } bool hasID = false, hasName = false; foreach (var item in dict.Items) { switch (item.Key) { case "id": hasID = true; ID = context.FromJson <long>(item.Value); break; case "name": hasName = true; Name = context.FromJson <string>(item.Value); break; case "description": Description = context.FromJson <string>(item.Value); break; case "user_count": UserCount = context.FromJson <int>(item.Value); break; case "created_date": Created = new DateTime(context.FromJson <long>(item.Value)); break; case "is_public": IsPublic = context.FromJson <bool>(item.Value); break; case "has_password": HasPassword = context.FromJson <bool>(item.Value); break; case "has_access": HasAccess = context.FromJson <bool>(item.Value); break; case "has_discussion": HasDiscussion = context.FromJson <bool>(item.Value); break; case "member_add_sets": MemberAddSets = context.FromJson <bool>(item.Value); break; case "sets": var sets = context.FromJson <List <Set> >(item.Value); AddSets(sets); break; } } if (!hasID || !hasName) { throw new JsonConvertException("Server did not supply group ID or name"); } }
public RoundInfo(JsonValue json, IJsonContext ctx) { var dict = JsonDictionary.FromValue(json); RoundNumber = ctx.FromJson<int>(dict["roundNumber"]); Score = ctx.FromJson<int>(dict["score"]); ItemCount = ctx.FromJson<int>(dict["itemCount"]); }
public UserInfo(JsonValue json, IJsonContext context) : this() { var dict = json as JsonDictionary; if (dict == null) { throw new JsonConvertException("Expected a JSON dictionary to be converted to a UserInfo"); } bool wasRelaxed = context.RelaxedNumericConversion; context.RelaxedNumericConversion = true; UserName = context.FromJson <string>(dict["username"]); AccountType = "free"; SignUpDate = new DateTime(1970, 1, 1).AddSeconds(context.FromJson <long>(dict["sign_up_date"])); Sets = context.FromJson <List <SetInfo> >(dict["sets"]); Groups = context.FromJson <List <GroupInfo> >(dict["groups"]); foreach (var k in dict.Items) { switch (k.Key) { case "account_type": AccountType = context.FromJson <string>(k.Value); break; case "study_session_count": StudySessionCount = context.FromJson <int>(k.Value); break; case "message_count": MessageCount = context.FromJson <int>(k.Value); break; case "total_answer_count": TotalAnswerCount = context.FromJson <int>(k.Value); break; case "public_sets_created": PublicSetsCreated = context.FromJson <int>(k.Value); break; case "public_terms_entered": PublicTermsEntered = context.FromJson <int>(k.Value); break; case "profile_image": ProfileImage = new Uri(context.FromJson <string>(k.Value), UriKind.Absolute); break; } } context.RelaxedNumericConversion = wasRelaxed; }
public Set(JsonValue json, IJsonContext context) : this() { var dict = json as JsonDictionary; if (dict == null) throw new JsonConvertException("Expected a JSON dictionary to be converted to a SetInfo"); if (!dict.Items.ContainsKey("id")) throw new JsonConvertException("Expected SetInfo JSON to contain an 'id' property"); foreach (var k in dict.Items) { switch (k.Key) { case "id": ID = context.FromJson<long>(k.Value); break; case "url": Uri = context.FromJson<string>(k.Value); break; case "title": Title = context.FromJson<string>(k.Value); break; case "created_by": Author = context.FromJson<string>(k.Value); break; case "description": Description = context.FromJson<string>(k.Value); break; case "term_count": TermCount = context.FromJson<int>(k.Value); break; case "created_date": Created = new DateTime(1970, 1, 1).AddSeconds(context.FromJson<long>(k.Value)); break; case "modified_date": Modified = new DateTime(1970, 1, 1).AddSeconds(context.FromJson<long>(k.Value)); break; case "subjects": AddSubjects(context.FromJson<List<string>>(k.Value)); break; case "visibility": Visibility = SetPermissions.ParseVisibility(context.FromJson<string>(k.Value)); break; case "editable": Editable = SetPermissions.ParseEditPermissions(context.FromJson<string>(k.Value)); break; case "has_access": HasAccess = context.FromJson<bool>(k.Value); break; case "has_discussion": HasDiscussion = context.FromJson<bool>(k.Value); break; case "lang_terms": TermLanguageCode = context.FromJson<string>(k.Value); break; case "lang_definitions": DefinitionLanguageCode = context.FromJson<string>(k.Value); break; case "terms": var list = new List<Term>(); if (k.Value is JsonArray) { foreach (var termJson in ((JsonArray)k.Value).Items) { if (!(termJson is JsonDictionary)) throw new JsonConvertException("Expected SetInfo.Terms to be an array of JSON dictionaries"); var term = new Term { ID = context.FromJson<long>(((JsonDictionary)termJson).Items["id"]), TermValue = context.FromJson<string>(((JsonDictionary)termJson).Items["term"]), Definition = context.FromJson<string>(((JsonDictionary)termJson).Items["definition"]) }; if (term.TermValue == null || term.Definition == null) throw new JsonConvertException("Either term or definition was not group when converting from JSON to Set"); list.Add(term); } } else { throw new JsonConvertException("Expected SetInfo.Terms to be an array"); } AddTerms(list); break; } } // TODO: Validate that important fields are defined }
public PracticeViewModel(JsonValue json, IJsonContext ctx) { var dict = JsonDictionary.FromValue(json); items = ctx.FromJson<List<PracticeItem>>(dict["items"]); nextItems = ctx.FromJson<List<PracticeItem>>(dict["nextItems"]); failed = ctx.FromJson<bool>(dict["failed"]); currentItem = ctx.FromJson<PracticeItem>(dict["currentItem"]); currentIndex = ctx.FromJson<int>(dict["currentIndex"]); itemCount = ctx.FromJson<int>(dict["itemCount"]); score = ctx.FromJson<int>(dict["score"]); round = ctx.FromJson<int>(dict["round"]); lastRoundInfo = ctx.FromJson<RoundInfo>(dict["lastRoundInfo"]); allRounds = new ObservableCollection<RoundInfo>(ctx.FromJson<List<RoundInfo>>(dict["allRounds"])); maxItemCount = ctx.FromJson<int>(dict["maxItemCount"]); title = ctx.FromJson<string>(dict["title"]); }
public T Get <T>(string key, IJsonContext context) { return(context.FromJson <T>(this[key])); }
protected ListInfo(JsonValue value, IJsonContext context) { var dict = value as JsonDictionary; if (dict == null) throw new JsonConvertException("Value was not a dictionary"); foreach (var k in dict.Items) { switch (k.Key) { case "ID": if (k.Value != null) { var n = k.Value as JsonNumber; if (n == null) throw new JsonConvertException("ListInfo.ID was not a number"); ID = n.LongValue; } break; case "Name": case "Author": case "Language": case "Url": if (k.Value != null) { var s = k.Value as JsonString; if (s == null) throw new JsonConvertException("ListInfo." + k.Key + " was not a number"); SetStringProperty(k.Key, s.Value); } break; case "Date": if (k.Value != null) { var s = k.Value as JsonString; if (s == null) throw new JsonConvertException("ListInfo.Date was not a string"); Date = DateTime.Parse(s.Value); } break; case "TermCount": if (k.Value != null) { var n = k.Value as JsonNumber; if (n == null) throw new JsonConvertException("ListInfo.TermCount was not a number"); TermCount = n.LongValue; } break; case "SyncID": SyncID = context.FromJson<long>(k.Value); break; case "SyncDate": SyncDate = context.FromJson<long>(k.Value).DateTimeFromUnixTime(); break; case "SyncNeeded": SyncNeeded = context.FromJson<bool>(k.Value); break; } } }
protected ListInfo(JsonValue value, IJsonContext context) { var dict = value as JsonDictionary; if (dict == null) { throw new JsonConvertException("Value was not a dictionary"); } foreach (var k in dict.Items) { switch (k.Key) { case "ID": if (k.Value != null) { var n = k.Value as JsonNumber; if (n == null) { throw new JsonConvertException("ListInfo.ID was not a number"); } ID = n.LongValue; } break; case "Name": case "Author": case "Language": case "Url": if (k.Value != null) { var s = k.Value as JsonString; if (s == null) { throw new JsonConvertException("ListInfo." + k.Key + " was not a number"); } SetStringProperty(k.Key, s.Value); } break; case "Date": if (k.Value != null) { var s = k.Value as JsonString; if (s == null) { throw new JsonConvertException("ListInfo.Date was not a string"); } Date = DateTime.Parse(s.Value); } break; case "TermCount": if (k.Value != null) { var n = k.Value as JsonNumber; if (n == null) { throw new JsonConvertException("ListInfo.TermCount was not a number"); } TermCount = n.LongValue; } break; case "SyncID": SyncID = context.FromJson <long>(k.Value); break; case "SyncDate": SyncDate = context.FromJson <long>(k.Value).DateTimeFromUnixTime(); break; case "SyncNeeded": SyncNeeded = context.FromJson <bool>(k.Value); break; } } }
public UserInfo(JsonValue json, IJsonContext context) : this() { var dict = json as JsonDictionary; if (dict == null) throw new JsonConvertException("Expected a JSON dictionary to be converted to a UserInfo"); bool wasRelaxed = context.RelaxedNumericConversion; context.RelaxedNumericConversion = true; UserName = context.FromJson<string>(dict["username"]); AccountType = "free"; SignUpDate = new DateTime(1970, 1, 1).AddSeconds(context.FromJson<long>(dict["sign_up_date"])); Sets = context.FromJson<List<SetInfo>>(dict["sets"]); Groups = context.FromJson<List<GroupInfo>>(dict["groups"]); foreach (var k in dict.Items) { switch (k.Key) { case "account_type": AccountType = context.FromJson<string>(k.Value); break; case "study_session_count": StudySessionCount = context.FromJson<int>(k.Value); break; case "message_count": MessageCount = context.FromJson<int>(k.Value); break; case "total_answer_count": TotalAnswerCount = context.FromJson<int>(k.Value); break; case "public_sets_created": PublicSetsCreated = context.FromJson<int>(k.Value); break; case "public_terms_entered": PublicTermsEntered = context.FromJson<int>(k.Value); break; case "profile_image": ProfileImage = new Uri(context.FromJson<string>(k.Value), UriKind.Absolute); break; } } context.RelaxedNumericConversion = wasRelaxed; }