using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections.Generic; namespace MatrixDotNetLib { public class MatrixSyncRooms { // once the JSON from "join" is parsed, this is set to its contents as an object public Dictionary Joined { get; private set;} public Dictionary Invited { get; private set; } public Dictionary Left { get; private set; } public JObject Join { get { return null; } set { // takes JSON children and parses them into objects // we have to do this bc the entity names under "join" are dynamic // each entity holds a well-defined object though // create dict for Joined Joined = new Dictionary(); // these AREN'T an array ParseRooms(value, "Joined"); } } public JObject Invite { get { return null; } set { // create dict for Joined Invited = new Dictionary(); // these AREN'T an array ParseRooms(value, "Invited"); } } public JObject Leave { get { return null; } set { // create dict for Joined Left = new Dictionary(); // these AREN'T an array ParseRooms(value, "Left"); } } public void ParseRooms(JObject roomsJson, string valType) { // get the children of this object JEnumerable kids = roomsJson.Children(); Dictionary roomList = new Dictionary(); foreach (JProperty kid in kids) { MatrixSyncRoom msr = JsonConvert.DeserializeObject(kid.Value.ToString()); roomList.Add(kid.Name, msr); } // get the target this.GetType().GetProperty(valType).SetValue(this,roomList); } } } //// simplest implementation //// might not work for UWP //// sauce: https://stackoverflow.com/questions/5527316/how-to-set-the-content-of-an-httpwebrequest-in-c //HttpContent requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json"); //HttpClient client = new HttpClient(); //client.BaseAddress = new Uri(loginUrl); //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //HttpResponseMessage responseMessage = client.PostAsync(loginUrl, requestContent).Result; //string responseString = responseMessage.Content.ReadAsStringAsync().Result; //if(responseString.Contains("errcode")) //{ // // deserialize into error object // MatrixError error = JsonConvert.DeserializeObject(responseString); // // convert error object to a string // string errMsg = error.ErrorCode + ": " + error.ErrorMessage; // // throw exception (can be caught and handled gracefully) // throw new Exception(errMsg); //} //else //{ // MatrixLoginResponse response = JsonConvert.DeserializeObject(responseString); // return response; //}