void Populate (JsonValue json, RootElement root, JsonValue data) { if (json.ContainsKey(Constants.Title)) root.Caption = json[Constants.Title]; JsonValue jsonRoot = null; try { jsonRoot = json[Constants.Root]; } catch (Exception){ Console.WriteLine("Bad JSON: could not find the root element - "+json.ToString()) ; return; } if (json.ContainsKey(Constants.DataRoot)){ var dataroot = json[Constants.DataRoot].CleanString(); if (data!=null && data.ContainsKey(dataroot)) data = data[dataroot]; } foreach (JsonObject section in jsonRoot){ var sec = new FormSectionBuilder(this._controller).Build(section, data); foreach (var el in sec.Elements) { if (!string.IsNullOrEmpty(el.ID) && !_elements.ContainsKey(el.ID)) _elements.Add(el.ID, el); } root.Add(sec); } }
public void InvalidPropertyTest() { JsonValue target = AnyInstance.AnyJsonPrimitive; Assert.True(target.Count == 0); Assert.False(target.ContainsKey(String.Empty)); Assert.False(target.ContainsKey(AnyInstance.AnyString)); }
public void PropertiesTest() { JsonValue target = AnyInstance.DefaultJsonValue; Assert.Equal(JsonType.Default, target.JsonType); Assert.Equal(0, target.Count); Assert.Equal(false, target.ContainsKey("hello")); Assert.Equal(false, target.ContainsKey(String.Empty)); }
internal Parser(JsonValue json) { _sourceparser = json; MissingTokens = json.ContainsKey("missing_tokens") ? new HashSet<string>(json["missing_tokens"].Select(x => (string) x)) : new HashSet<string>(); }
public static string GetString(JsonValue obj, string key) { if (obj.ContainsKey(key)) if (obj[key].JsonType == JsonType.String) return (string)obj[key]; return null; }
public static ulong GetULongValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return(0UL); }
private static bool ContainsKey (JsonValue jsonValue, string key) { if (!jsonValue.ContainsKey (key)) { LogContext.Current.Log<JsonUtil> ("JsonValueString: Missing key", key); return false; } else return true; }
public static char GetCharValue(this JsonValue self, string key, char defaultValue) { if (self.ContainsKey(key)) { return(self[key]); } return(defaultValue); }
public static Guid GetGuidValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return(Guid.Empty); }
public static char GetCharValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return('\0'); }
public static short GetShortValue(this JsonValue self, string key, short defaultValue) { if (self.ContainsKey(key)) { return(self[key]); } return(defaultValue); }
public static JsonArray GetJsonArray(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key] as JsonArray); } return(null); }
public static Double GetDoubleValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return(0.0D); }
public static decimal GetDecimalValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return(0); }
public static long GetLongValue(this JsonValue self, string key, long defaultValue) { if (self.ContainsKey(key)) { return(self[key]); } return(defaultValue); }
public static Uri GetUriValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return(null); }
public static DateTime GetDateTimeValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return(new DateTime(1900, 1, 1)); }
public static bool GetBooleanValue(this JsonValue self, string key, bool defaultValue) { if (self.ContainsKey(key)) { return(self[key]); } return(defaultValue); }
public static Byte GetByteValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return(0); }
public static JsonObject GetJsonObject(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key] as JsonObject); } return(null); }
public FullQuestion(JsonValue jsonValue, Spin spin) { this.Spin = spin; if (jsonValue.ContainsKey("powerup_question")) this.PowerupQuestion = new Question(jsonValue["powerup_question"], this); this.Question = new Question(jsonValue["question"], this); }
public static DateTime GetDateTimeValue(this JsonValue self, string key, DateTime defaultValue) { if (self.ContainsKey(key)) { return(self[key]); } return(defaultValue); }
public static TimeSpan GetTimeSpanValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return(new TimeSpan()); }
public static float GetFloatValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return(0.0f); }
public static ushort GetUShortValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return(0); }
public static string GetStringValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return(string.Empty); }
public static Guid GetGuidValue(this JsonValue self, string key, Guid defaultValue) { if (self.ContainsKey(key)) { return(self[key]); } return(defaultValue); }
public static DateTimeOffset GetDateTimeOffsetValue(this JsonValue self, string key) { if (self.ContainsKey(key)) { return(self[key]); } return(new DateTimeOffset()); }
public static string CheckForLandmark (JsonValue searchResult, string locationName) { string result = ""; result = ( searchResult.ContainsKey ("types") ? (searchResult ["types"] as JsonArray).ToList () : new List<JsonValue> () ).Exists (v => v == "stadium" // build this out as necessary with valid types from here https://developers.google.com/places/documentation/supported_types ) ? locationName : ""; return result; }
internal void AddApp(int appId, JsonValue app, JsonValue engines) { var matches = new List<PathMatch>(); if (app.ContainsKey(Key)) { matches.AddRange(app[Key].Select(match => new PathMatch(match.Value))); } if (app.ContainsKey("engine")) { var args = app["engine"]; var engine = engines[(string) args["name"]]; if (engine.ContainsKey(Key)) { matches.AddRange(engine[Key].Select(match => new PathMatch(match.Value, args))); } } Apps[appId] = matches; }
static QuietTime GetQuietTime(JsonValue jsonValue) { if (jsonValue.ContainsKey("quiettime")) { var quiettime = jsonValue["quiettime"]; return new QuietTime { Start = quiettime.StringValue("start"), End = quiettime.StringValue("end"), }; } return null; }
public static LoginResponse DecodeLoginResponse (JsonValue json) { LoginResponse response = new LoginResponse (); if (json == null) { response.StatusCode = Response.UnknownError; return response; } if (json.ContainsKey (STATUS)) { response.StatusCode = json [STATUS]; } else if (json.ContainsKey (EXCEPTION)) { response.StatusCode = json [EXCEPTION]; } if (json.ContainsKey (UserInfo.SESSIONID)) { response.SessionId = json [UserInfo.SESSIONID]; } if (json.ContainsKey (UserInfo.NAME)) { response.Name = json [UserInfo.NAME]; } if (json.ContainsKey (UserInfo.EMAIL)) { response.email = json [UserInfo.EMAIL]; } // if (!response.IsOk) { // if (json.ContainsKey (MESSAGE)) { // response.Message = json [MESSAGE]; // } else if (json.ContainsKey ("msg")) { // response.Message = json ["msg"]; // } // } return response; }
public Session(JsonValue json) : this() { Id = json["id"]; Title = json["title"]; Abstract = json["abstract"]; Location = json["location"]; var begins = json["begins"].ToString().Trim ('"').Substring (0, 19).Replace ("T", " "); Begins = DateTime.Parse (begins); if (json.ContainsKey("ends")) { var ends = json["ends"].ToString().Trim('"').Substring(0, 19).Replace("T", " "); Ends = DateTime.Parse(ends, System.Globalization.CultureInfo.InvariantCulture); } }
public VBUser(JsonValue json) { DB_Communicator db = DB_Communicator.getInstance(); this.idUser = db.convertAndInitializeToInt(db.containsKey(json, "id", DB_Communicator.JSON_TYPE_INT)); this.name = db.convertAndInitializeToString(db.containsKey(json, "name", DB_Communicator.JSON_TYPE_STRING)); this.email = db.convertAndInitializeToString(db.containsKey(json, "email", DB_Communicator.JSON_TYPE_STRING)); this.state = db.convertAndInitializeToString(db.containsKey(json, "state", DB_Communicator.JSON_TYPE_STRING)); this.setUserType(db.convertAndInitializeToString(db.containsKey(json, "userType", DB_Communicator.JSON_TYPE_STRING))); this.listTeamRole = new List<VBTeamrole>(); if(json.ContainsKey("teamroles")) { if(json["teamroles"] is JsonObject) { JsonValue teamrole = json["teamroles"]["TeamRole"]; this.listTeamRole.Add(new VBTeamrole(teamrole)); } else { foreach(JsonValue teamrole in json["teamroles"]) { this.listTeamRole.Add(new VBTeamrole(teamrole["TeamRole"])); } } } }
public Section Build(JsonObject section, JsonValue data) { var sec = new Section(section.asString("caption"), section.asString("footer")); if (!string.IsNullOrEmpty(section.asString("captionImage"))) { var imgDevice = section.asString("captionImage").Split(' '); var img = UIDevice.CurrentDevice.UserInterfaceIdiom==UIUserInterfaceIdiom.Phone ? imgDevice[0] : imgDevice[1]; sec.HeaderView = new UIImageView(UIImage.FromBundle(img)); } if (section.ContainsKey("elements")){ foreach (JsonObject elem in section["elements"]) { var dataForElement = data; var bindExpression = elem.asString("bind"); if (bindExpression!=null) { try { if (data!=null && data.JsonType==JsonType.Object){ var bind = elem.asString("bind"); if (data != null && !string.IsNullOrEmpty(bind) && data.ContainsKey(bind)) { dataForElement = data[bind]; } } else if (bindExpression.StartsWith("#")) { dataForElement = _controller.GetValue(bindExpression.Replace("#", "")); } } catch (Exception e){ Console.WriteLine("Exception when binding element " + elem.ToString()+ " - " + e.ToString()); } } _parseElement(elem, sec, dataForElement); } } else if (section.ContainsKey("iterate") && data != null){ string iterationname = section["iterate"]; JsonArray iterationdata = null; if (iterationname.Contains("#")){ // for when list is in member of object var splittedName = iterationname.Split('#'); var obj = (JsonObject)data[splittedName[0]]; iterationdata = (JsonArray)obj[splittedName[1]]; } else { iterationdata = string.IsNullOrEmpty(iterationname) ? (JsonArray)data : (JsonArray)data[iterationname]; } string emptyMessage = section.ContainsKey("empty") ? section["empty"].CleanString() : "Empty"; var template = (JsonObject)section["template"]; var items = iterationdata.ToList(); if (items.Count>0) { foreach(JsonValue v in items){ _parseElement(template, sec, v); } } else { sec.Add(new EmptyListElement(emptyMessage)); } } else if (section.ContainsKey("iterateproperties") && data != null){ string iterationname = section["iterateproperties"]; string emptyMessage = section.ContainsKey("empty") ? section["empty"].CleanString() : "Empty"; var iterationdata = string.IsNullOrEmpty(iterationname) ? (JsonObject)data : data.ContainsKey(iterationname) ? (JsonObject)data[iterationname] : null; var template = (JsonObject)section["template"]; var items = iterationdata == null ? new List<string>() : iterationdata.Keys; if (items.Count>0) { foreach(string v in items){ var obj = new JsonObject(); obj.Add(v, iterationdata[v]); _parseElement(template, sec, obj); } } else { sec.Add(new EmptyListElement(emptyMessage)); } } return sec; }
public static JsonValue valueOrDefault(JsonValue jsonObject, string key, string defaultValue) { // Returns jsonObject[key] if it exists. If the key doesn't exist returns defaultValue and // logs the anomaly into the Chronojump log. if (jsonObject.ContainsKey (key)) { return jsonObject [key]; } else { LogB.Information ("JsonUtils::valueOrDefault: returning default (" + defaultValue + ") from JSON: " + jsonObject.ToString ()); return defaultValue; } }
private async Task<int> EventHandling(DataBaseWrapper db, JsonValue result) { if (result.ContainsKey ("events")) { JsonArray arr = (JsonArray) result ["events"]; foreach (JsonValue v in arr) { var e = new Event () { type = v ["type"], msg = v ["msg"], nick = v ["nick"], text = v ["text"], time = v ["time"], author = v ["author"] }; db.Insert (e); } if (backgroundService != null) { await backgroundService.UpdateNotifications(); } } if (result.ContainsKey ("onLoginError")) { return 1; } return 0; }
private void _configureDialog(JsonValue json, JsonObject valuesJson){ if (json.ContainsKey("grouped")) { Style = bool.Parse(json["grouped"].ToString()) ? UITableViewStyle.Grouped : UITableViewStyle.Plain; } if (json.ContainsKey("title")) Title = json["title"]; if (json.ContainsKey("dataroot")) DataRootName = json["dataroot"]; if (json.ContainsKey("rightbaritem")){ var item = (JsonObject)json["rightbaritem"]; string datavalue = null, id = null; id = item.s("id"); if (valuesJson!=null && !string.IsNullOrEmpty(id)){ datavalue = valuesJson.s(id); } if (item.ContainsKey("action")) { rightBarItem = item.ContainsKey("url") ? new SubmitElement(item.s("caption"), datavalue ?? item.s("url"), null, null) : new ActionElement(item.s("caption"), datavalue ?? item.s("action"), null); rightBarItem.ID = id; } if (item.ContainsKey("image")){ NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIImage.FromBundle(item.s("image")), UIBarButtonItemStyle.Plain, (object o, EventArgs a)=>{ InvokeAction(this.rightBarItem); }); } else { NavigationItem.RightBarButtonItem = new UIBarButtonItem(item.s("caption"), UIBarButtonItemStyle.Plain, (object o, EventArgs a)=>{ InvokeAction(this.rightBarItem); }); } } if (json.ContainsKey("leftbaritem")){ var item = (JsonObject)json["leftbaritem"]; if (item.ContainsKey("action")) { leftBarItem = item.ContainsKey("url") ? new SubmitElement(item.s("caption"), item.s("url"), null, null) : new ActionElement(item.s("caption"), item.s("action"), null); leftBarItem.ID = item.s("id"); } NavigationItem.LeftBarButtonItem = new UIBarButtonItem(item.s("caption"), UIBarButtonItemStyle.Plain, (object o, EventArgs a)=>{ InvokeAction(this.leftBarItem); }); } }
string GetValue (JsonValue json, string field) { if (!json.ContainsKey (field)) return string.Empty; return json [field].ToString ().Trim ('"'); }
public void BuildFromJsonObject(JsonValue json) { Id = json ["Id"]; Name = json ["Name"]; Icon = json ["Icon"]; MinYear = Convert.ToInt32 (json ["MinYear"].ToString ()); MaxYear = Convert.ToInt32 (json ["MaxYear"].ToString ()); if (json.ContainsKey ("Publishers")) { if (json ["Publishers"] is JsonArray) { Publishers = new List<string> (); foreach (var publisherJson in ((JsonArray)json["Publishers"])) { Publishers.Add (publisherJson); } } } if (json.ContainsKey ("Genres")) { if (json ["Genres"] is JsonArray) { Genres = new List<string> (); foreach (var genreJson in ((JsonArray)json["Genres"])) { Genres.Add (genreJson); } } } if (json.ContainsKey ("Platforms")) { if (json ["Platforms"] is JsonArray) { Platforms = new List<string> (); foreach (var platformJson in ((JsonArray)json["Platforms"])) { Platforms.Add (platformJson); } } } if (json.ContainsKey ("RequiredGameIds")) { if (json ["RequiredGameIds"] is JsonArray) { RequiredGameIds = new Dictionary<int, int[]> (); foreach (var jsonQuestion in ((JsonArray)json["RequiredGameIds"])) { int gameId = Convert.ToInt32 (jsonQuestion ["GameId"].ToString ()); List<int> answers = new List<int> (); foreach (var jsonAnswer in ((JsonArray)jsonQuestion["Answers"])) { int answerGameId = Convert.ToInt32 (jsonAnswer.ToString ()); answers.Add (answerGameId); } RequiredGameIds.Add (gameId, answers.ToArray()); } } } }
/// <summary> /// Parse Json config /// </summary> /// <param name="json">Json.</param> public void BuildFromJsonObject(JsonValue json) { // Example // { "version": 1, "last_edit": 2013-07-10T11:44:12+02:00, "config": "{ "modes": [ {"Mode": 0, "Difficulty": 0, "Time": 10, "Score": 100, "QuestionsCount": 20},} // Parse "config" if (json.ContainsKey ("config")) { JsonValue config = json ["config"]; if (config.ContainsKey ("modes")) { JsonValue modesConfig = config ["modes"]; if (modesConfig is JsonArray) { foreach (JsonValue c in modesConfig) { // Parsing each game mode configuration GameMode mode = GameMode.SCORE; GameDifficulty difficulty = GameDifficulty.EASY; int? time = null; int? score = null; int? questionCount = null; mode = (GameMode)Enum.Parse (typeof(GameMode), c ["Mode"].ToString ()); difficulty = (GameDifficulty)Enum.Parse (typeof(GameDifficulty), c ["Difficulty"].ToString ()); if (c.ContainsKey ("Time")) { time = Convert.ToInt32 (c ["Time"].ToString ()); } if (c.ContainsKey ("Score")) { score = Convert.ToInt32 (c ["Score"].ToString ()); } if (c.ContainsKey ("QuestionsCount")) { questionCount = Convert.ToInt32 (c ["QuestionsCount"].ToString ()); } ModeConfigurationItem modeConfig = new ModeConfigurationItem () { Mode = mode, Difficulty = difficulty, Time = time, Score = score, QuestionCount = questionCount }; ModesConfiguration.Add (modeConfig); } } } // Parse other "Properties" if (json.ContainsKey ("properties")) { JsonValue properties = json ["properties"]; if (properties is JsonArray) { foreach (JsonValue p in properties) { string key = p ["key"].ToString (); string value = p ["value"].ToString (); Properties.Add (new PropertyConfigurationItem (key, value)); } } } } }
private void _configureDialog(JsonValue json, JsonObject valuesJson){ if (json.ContainsKey(Constants.Grouped)) { Style = bool.Parse(json[Constants.Grouped].ToString()) ? UITableViewStyle.Grouped : UITableViewStyle.Plain; } if (json.ContainsKey(Constants.Title)) Title = json[Constants.Title]; if (json.ContainsKey(Constants.DataRoot)) DataRootName = json[Constants.DataRoot]; if (json.ContainsKey(Constants.RightBarItem)){ NavigationItem.RightBarButtonItem = _createButtonItemFor(Constants.RightBarItem, json, valuesJson); } if (json.ContainsKey(Constants.LeftBarItem)){ NavigationItem.LeftBarButtonItem = _createButtonItemFor(Constants.LeftBarItem, json, valuesJson); } }
internal void ReloadGame(JsonValue jsonValue) { this.Status = (string)jsonValue["game_status"]; this.Id = (int)jsonValue["id"]; this.UnreadMessages = (int)jsonValue["unread_messages"]; this.MyPlayerNumber = (int)jsonValue["my_player_number"]; this.RoundNumber = (int)jsonValue["round_number"]; this.IsRandom = (bool)jsonValue["is_random"]; this.MyTurn = (bool)jsonValue["my_turn"]; this.Opponent = new User((string)jsonValue["opponent"]["username"], (int)jsonValue["opponent"]["id"]); if (jsonValue.ContainsKey("win")) this.Win = (bool)jsonValue["win"]; this.Player1Charges = (int)jsonValue["player_one"]["charges"]; this.Player1Crowns = new List<string>(); if (jsonValue["player_one"].ContainsKey("crowns")) { JsonArray player1CrownsJsonArray = (JsonArray)jsonValue["player_one"]["crowns"]; foreach (JsonValue crown in player1CrownsJsonArray) this.Player1Crowns.Add((string)crown); } this.Player2Charges = (int)jsonValue["player_two"]["charges"]; this.Player2Crowns = new List<string>(); if (jsonValue["player_two"].ContainsKey("crowns")) { JsonArray player2CrownsJsonArray = (JsonArray)jsonValue["player_two"]["crowns"]; foreach (JsonValue crown in player2CrownsJsonArray) this.Player2Crowns.Add((string)crown); } this.Spins = new List<Spin>(); if (jsonValue.ContainsKey("spins_data")) { JsonArray spinsJsonArray = (JsonArray)jsonValue["spins_data"]["spins"]; foreach (JsonValue spinJsonValue in spinsJsonArray) this.Spins.Add(new Spin(spinJsonValue, this)); } }
public JsonValue containsKey(JsonValue value, string key, int type) { JsonPrimitive nullValue = new JsonPrimitive(""); switch(type) { case JSON_TYPE_INT: nullValue = new JsonPrimitive(0); break; case 1: nullValue = new JsonPrimitive(""); break; case 2: nullValue = new JsonPrimitive(new DateTime()); break; } if(value == null) return nullValue; return (value.ContainsKey(key)) ? value[key] : nullValue; }
void Populate (JsonValue json, RootElement root, JsonValue data) { if (json.ContainsKey("title")) root.Caption = json["title"]; JsonValue jsonRoot = null; try { jsonRoot = json["root"]; } catch (Exception){ Console.WriteLine("Bad JSON: could not find the root element - "+json.ToString()) ; return; } foreach (JsonObject section in jsonRoot){ var sec = new Section(section.s("caption"), section.s("footer")); if (section.ContainsKey("elements")){ foreach (JsonObject elem in section["elements"]) { var dataForElement = data; var bindExpression = elem.s("bind"); if (bindExpression!=null) { try { if (data!=null && data.JsonType==JsonType.Object){ var bind = elem.s("bind"); if (data != null && !string.IsNullOrEmpty(bind) && data.ContainsKey(bind)) { dataForElement = data[bind]; } } else if (bindExpression.StartsWith("#")) { dataForElement = _controller.GetValue(bindExpression.Replace("#", "")); } } catch (Exception){ Console.WriteLine("Exception when binding element " + elem.ToString()); } } _parseElement(elem, sec, dataForElement); } } else if (section.ContainsKey("iterate") && data != null){ string iterationname = section["iterate"]; string emptyMessage = section.ContainsKey("empty") ? section["empty"].CleanString() : "Empty"; var iterationdata = string.IsNullOrEmpty(iterationname) ? (JsonArray)data : (JsonArray)data[iterationname]; var template = (JsonObject)section["template"]; var items = iterationdata.ToList(); if (items.Count>0) { foreach(JsonValue v in items){ _parseElement(template, sec, v); } } else { sec.Add(new EmptyListElement(emptyMessage)); } } else if (section.ContainsKey("iterateproperties") && data != null){ string iterationname = section["iterateproperties"]; string emptyMessage = section.ContainsKey("empty") ? section["empty"].CleanString() : "Empty"; var iterationdata = string.IsNullOrEmpty(iterationname) ? (JsonObject)data : (JsonObject)data[iterationname]; var template = (JsonObject)section["template"]; var items = iterationdata.Keys; if (items.Count>0) { foreach(string v in items){ var obj = new JsonObject(); obj.Add(v, iterationdata[v]); _parseElement(template, sec, obj); } } else { sec.Add(new EmptyListElement(emptyMessage)); } } root.Add(sec); } }
public void toastJson(Context context, JsonValue json, ToastLength length, string alternativeMessage) { Context c = (context != null) ? context : mainActivity; string message = (json.ContainsKey("message")) ? json["message"].ToString() : alternativeMessage; Toast.MakeText(c, message, length).Show(); }