protected static IEnumerable<JsonObject> GetRawRows(JsonObject response) { var rowsArray = response.TryGetValue("rows") as JsonArray; if (rowsArray == null) throw new ParseException("Query result is expected to contain array 'rows' property"); return rowsArray.Cast<JsonObject>(); }
public Tweet(JsonObject json) { Text = json["text"]; // explicit conversion will unescape json Text = Text.UnescapeXml(); // unescape again for & escapes Source = json["source"]; Id = json["id_str"]; IsRetweet = json["retweeted"]; InReplyToStatusId = json["in_reply_to_status_id_str"]; InReplyToScreenName = json["in_reply_to_screen_name"]; CreatedAt = json.GetDateTime("created_at"); JsonValue entities; if (json.TryGetValue("entities", out entities)) Entities = new Entities(entities); JsonValue retweetedStatus; if (json.TryGetValue("retweeted_status", out retweetedStatus)) RetweetedStatus = new Tweet((JsonObject)retweetedStatus); User = new User(json["user"]); }
public void JsonObjectCopytoFunctionalTest() { int seed = 1; for (int i = 0; i < iterationCount / 10; i++) { seed++; Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); bool retValue = true; JsonObject sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength); KeyValuePair <string, JsonValue>[] destJson = new KeyValuePair <string, JsonValue> [arrayLength]; if (sourceJson != null && destJson != null) { sourceJson.CopyTo(destJson, 0); } else { Log.Info("[JsonObjectCopytoFunctionalTest] sourceJson.ToString() = " + sourceJson.ToString()); Log.Info("[JsonObjectCopytoFunctionalTest] destJson.ToString() = " + destJson.ToString()); Assert.False(true, "[JsonObjectCopytoFunctionalTest] failed to create the source JsonObject object."); return; } if (destJson.Length == arrayLength) { for (int k = 0; k < destJson.Length; k++) { JsonValue temp; sourceJson.TryGetValue(k.ToString(), out temp); if (!(temp != null && destJson[k].Value == temp)) { retValue = false; } } } else { retValue = false; } Assert.True(retValue, "[JsonObjectCopytoFunctionalTest] JsonObject.CopyTo() failed to function properly. destJson.GetLength(0) = " + destJson.GetLength(0)); } }
public void JsonObjectItemsFunctionalTest() { int seed = 1; for (int i = 0; i < iterationCount / 10; i++) { seed++; Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); bool retValue = true; JsonObject sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength); // JsonObject[key].set_Item sourceJson["1"] = new JsonPrimitive(true); if (sourceJson["1"].ToString() != "true") { Log.Info("[JsonObjectItemsFunctionalTest] JsonObject[key].set_Item failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectItemsFunctionalTest] JsonObject[key].set_Item passed test."); } // ICollection<KeyValuePair<string, JsonValue>>.Contains(KeyValuePair<string, JsonValue> item) KeyValuePair <string, System.Json.JsonValue> kp = new KeyValuePair <string, JsonValue>("5", sourceJson["5"]); if (!((ICollection <KeyValuePair <string, JsonValue> >)sourceJson).Contains(kp)) { Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Contains(KeyValuePair<string, JsonValue> item) failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Contains(KeyValuePair<string, JsonValue> item) passed test."); } // ICollection<KeyValuePair<string, JsonValue>>.IsReadOnly if (((ICollection <KeyValuePair <string, JsonValue> >)sourceJson).IsReadOnly) { Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.IsReadOnly failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.IsReadOnly passed test."); } // ICollection<KeyValuePair<string, JsonValue>>.Add(KeyValuePair<string, JsonValue> item) kp = new KeyValuePair <string, JsonValue>("100", new JsonPrimitive(100)); ((ICollection <KeyValuePair <string, JsonValue> >)sourceJson).Add(kp); if (sourceJson.Count != arrayLength + 1) { Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Add(KeyValuePair<string, JsonValue> item) failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Add(KeyValuePair<string, JsonValue> item) passed test."); } // ICollection<KeyValuePair<string, JsonValue>>.Remove(KeyValuePair<string, JsonValue> item) ((ICollection <KeyValuePair <string, JsonValue> >)sourceJson).Remove(kp); if (sourceJson.Count != arrayLength) { Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Remove(KeyValuePair<string, JsonValue> item) failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Remove(KeyValuePair<string, JsonValue> item) passed test."); } // ICollection<KeyValuePair<string, JsonValue>>.GetEnumerator() JsonObject jo = new JsonObject { { "member 1", 123 }, { "member 2", new JsonArray { 1, 2, 3 } } }; List <string> expected = new List <string> { "member 1 - 123", "member 2 - [1,2,3]" }; expected.Sort(); IEnumerator <KeyValuePair <string, JsonValue> > ko = ((ICollection <KeyValuePair <string, JsonValue> >)jo).GetEnumerator(); List <string> actual = new List <string>(); ko.Reset(); ko.MoveNext(); do { actual.Add(String.Format("{0} - {1}", ko.Current.Key, ko.Current.Value.ToString())); Log.Info("added one item: {0}", String.Format("{0} - {1}", ko.Current.Key, ko.Current.Value)); ko.MoveNext(); }while (ko.Current.Value != null); actual.Sort(); if (!JsonValueVerifier.CompareStringLists(expected, actual)) { Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.GetEnumerator() failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.GetEnumerator() passed test."); } // JsonObject.Values sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength); JsonValue[] manyValues = SpecialJsonValueHelper.CreatePrePopulatedJsonValueArray(seed, arrayLength); JsonObject jov = new JsonObject(); for (int j = 0; j < manyValues.Length; j++) { jov.Add("member" + j, manyValues[j]); } List <string> expectedList = new List <string>(); foreach (JsonValue v in manyValues) { expectedList.Add(v.ToString()); } expectedList.Sort(); List <string> actualList = new List <string>(); foreach (JsonValue v in jov.Values) { actualList.Add(v.ToString()); } actualList.Sort(); if (!JsonValueVerifier.CompareStringLists(expectedList, actualList)) { Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.Values failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.Values passed test."); } for (int j = 0; j < sourceJson.Count; j++) { // JsonObject.Contains(Key) if (!sourceJson.ContainsKey(j.ToString())) { Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.Contains(Key) failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.Contains(Key) passed test."); } // JsonObject.TryGetValue(String, out JsonValue) JsonValue retJson; if (!sourceJson.TryGetValue(j.ToString(), out retJson)) { Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.TryGetValue(String, out JsonValue) failed to function properly."); retValue = false; } else if (retJson != sourceJson[j.ToString()]) { // JsonObjectthis[string key] Log.Info("[JsonObjectItemsFunctionalTest] JsonObject[string key] or JsonObject.TryGetValue(String, out JsonValue) failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.TryGetValue(String, out JsonValue) & JsonObject[string key] passed test."); } } Assert.True(retValue); } }
public void TryGetValueTest() { string key1 = AnyInstance.AnyString; string key2 = AnyInstance.AnyString2; JsonValue value1 = AnyInstance.AnyJsonValue1; JsonValue value2 = AnyInstance.AnyJsonValue2; JsonObject target = new JsonObject { { key1, value1 }, { key2, value2 } }; JsonValue value; Assert.IsTrue(target.TryGetValue(key2, out value)); Assert.AreEqual(value2, value); Assert.IsFalse(target.TryGetValue("not a key", out value)); Assert.IsNull(value); }