private void VerifyIsPathIncluded(JsonFilter filter, string path, JToken beforeToken, JToken afterToken) { JObject beforeObject = beforeToken as JObject; JObject afterObject = afterToken as JObject; if (beforeObject != null && afterObject != null) { foreach (var property in beforeObject) { string childPath = (path == null ? "" : (path + ".")) + property.Key; JToken afterChildToken; bool isIncluded = afterObject.TryGetValue(property.Key, out afterChildToken); Assert.AreEqual(filter.IsPathIncluded(childPath), isIncluded, "{0} was {1}", childPath, isIncluded ? "included" : "not included"); VerifyIsPathIncluded(filter, childPath, property.Value, afterChildToken); } } else { JArray beforeArray = beforeToken as JArray; JArray afterArray = afterToken as JArray; if (beforeArray != null && afterArray != null && beforeArray.Count == afterArray.Count) { for (int index = 0; index < beforeArray.Count; index++) { VerifyIsPathIncluded(filter, path, beforeArray[index], afterArray[index]); } } } }
public IEnumerable <KeyValuePair <string, IJsonValue> > FindIndexedObjects(IJsonValue obj) { HashSet <string> keys = null; var indexPaths = new HashSet <string>(); foreach (var g in from indexValue in GetObjectIndexValues(obj) group indexValue by indexValue.Item1 into g select g) { indexPaths.Add(g.Key.MemberPath); HashSet <string> keysForThisIndex = null; foreach (var tuple in g) { if (keysForThisIndex == null) { keysForThisIndex = new HashSet <string>(g.Key.FindKeys(tuple.Item2)); } else { keysForThisIndex.IntersectWith(g.Key.FindKeys(tuple.Item2)); } if (keysForThisIndex.Count == 0) { return(Enumerable.Empty <KeyValuePair <string, IJsonValue> >()); } } if (keys == null) { keys = keysForThisIndex; } else { keys.IntersectWith(keysForThisIndex); } if (keys.Count == 0) { return(Enumerable.Empty <KeyValuePair <string, IJsonValue> >()); } } if (keys == null) { // table scan var filter = new JsonFilter(obj.JsonPathValues()); return(from rec in MasterTable.Scan() where filter.Matches(rec.Value) select rec); } else { var filter = new JsonFilter(obj.JsonPathValues().Where(pv => !indexPaths.Contains(pv.Path.Path))); return(from key in keys from rec in MasterTable.Find(key) where filter.Matches(rec.Value) select rec); } }
internal JsonControl(JsonFilter filter) : base(filter) { InitializeComponent(); JsonPathBox.Text = filter.JsonPath; GroupName.Text = filter.GroupName; TrimQuotation.Checked = filter.TrimQuotation; }
public void BasicFilteredJsonWriterTest() { using (StringWriter stringWriter = new StringWriter()) { using (JsonTextWriter jsonWriter = new JsonTextWriter(stringWriter) { Formatting = Formatting.None }) using (JsonWriter filteredWriter = JsonFilter.Parse("name,!name.middle").CreateFilteredJsonWriter(jsonWriter)) JToken.Parse(@"{""id"":123,""name"":{""first"":""Ed"",""middle"":""James"",""last"":""Ball""}}").WriteTo(filteredWriter); Assert.AreEqual(@"{""name"":{""first"":""Ed"",""last"":""Ball""}}", stringWriter.ToString()); } }
public void FilterTests(string filterText, string rootPath, string before, string after) { JsonFilter filter = JsonFilter.TryParse(filterText, rootPath); JToken beforeToken = JToken.Parse(before); JToken afterToken = JToken.Parse(after); JToken actualToken = filter.FilterToken(beforeToken); if (!JTokenUtility.AreEqual(actualToken, afterToken)) { Assert.Fail("expected {0} actual {1}", afterToken, actualToken); } VerifyIsPathIncluded(filter, null, beforeToken, afterToken); }
public void TryParseWithRootPath(string filterText, string rootPath, string roundTrip) { JsonFilter filter = JsonFilter.TryParse(filterText, rootPath); if (filter == null) { Assert.IsNull(roundTrip); } else { Assert.AreEqual(roundTrip, filter.ToString()); Assert.AreEqual(roundTrip, JsonFilter.TryParse(filter.ToString()).ToString()); } }
private void TestFilter <T>(T hitValue, T missValue) { var filter = new JsonFilter(JSON.FromObject(new { TestField = hitValue })); Assert.IsTrue(filter.Matches(new { TestField = hitValue, Ignored = "whatever" }), "Expected hit on exact value match"); Assert.IsFalse(filter.Matches(new { TestField = missValue, Ignored = "who cares" }), "Expected miss on exact value mismatch"); Assert.IsTrue(filter.Matches(new { TestField = new[] { hitValue, missValue }, Ignored = "whatever" }), "Expected hit on exact value in array"); Assert.IsFalse(filter.Matches(new { TestField = new[] { missValue, missValue }, Ignored = "who cares" }), "Expected miss on exact value not in array"); }
public void TryParseAndToString(string filterText, string roundTrip) { JsonFilter filter = JsonFilter.TryParse(filterText); if (filter == null) { Assert.IsNull(roundTrip); } else { Assert.AreEqual(roundTrip, JsonFilter.JoinPaths(filter.GetPropertyPaths())); Assert.AreEqual(roundTrip, filter.ToString()); Assert.AreEqual(roundTrip, JsonFilter.TryParse(filter.ToString()).ToString()); } }
public override bool VisitObject(IJsonObject value) { foreach (var kv in _value) { IJsonValue v; if (!value.TryGetValue(kv.Key, out v)) { return false; } var filter = new JsonFilter(kv.Value); if (!filter.Matches(v)) { return false; } } return true; }
public void JSON_FILTER_SHOULD_SET_KEY_WITH_COMPLETE_TOKEN_WHEN_FLATTEN_IS_DISABLED() { //Arrange const string key = "key"; var filter = new JsonFilter() { SourceKey = key, FlattenJson = false }; var eventProperties = new Dictionary <string, object> { { key, "{ \"prop\": \"value\"}" } }; //Act filter.PrepareEvent(eventProperties); //Assert ((JToken)eventProperties[key])["prop"].Value <string>().Should().Be("value"); }
public void JSON_FILTER_SHOULD_FLATTEN_JSON() { //Arrange const string key = "key"; var filter = new JsonFilter() { SourceKey = key, FlattenJson = true }; var eventProperties = new Dictionary <string, object> { { key, "{ \"prop\": \"value\"}" } }; //Act filter.PrepareEvent(eventProperties); //Assert eventProperties["prop"].Should().Be("value"); }
private async Task <int> OnExecuteAsync() { try { await JsonFilter.FilterJsonAsync(this.console.In, this.console.Out, this.JsonFieldSelectors).ConfigureAwait(false); return((int)ErrorCode.Success); } catch (OperationCanceledException) { return((int)ErrorCode.OperationCanceled); } catch (Exception ex) { await this.WriteExceptionAsync(ex).ConfigureAwait(false); return((int)ErrorCode.UnknownError); } }
public void BasicFilterTokenTest() { var personBefore = new PersonDto { Id = 123, Name = new NameDto { First = "Ed", Middle = 'J', Last = "Ball" } }; var personAfter = JsonFilter.Parse("name,!name.middle").FilterObject(personBefore); Assert.AreEqual(123, personBefore.Id); Assert.AreEqual(null, personAfter.Id); Assert.AreEqual("Ed", personBefore.Name.First); Assert.AreEqual("Ed", personAfter.Name.First); Assert.AreEqual('J', personBefore.Name.Middle); Assert.AreEqual(null, personAfter.Name.Middle); Assert.AreEqual("Ball", personBefore.Name.Last); Assert.AreEqual("Ball", personAfter.Name.Last); }
public void MatchArray() { var obj = new { F = new object[] { new { A = 1 }, 2, "Three" } }; var superObj = new { F = new object[] { new { A = 1, Extra1 = 1 }, 2, "Three" }, Extra = 1 }; var badObj = new { F = new object[] { 1, 2, 3 } }; var filter = new JsonFilter(JSON.FromObject(obj)); Assert.IsTrue(filter.Matches(superObj)); Assert.IsFalse(filter.Matches(badObj)); }
private static void WriteObject(this JsonWriter jsonWriter, DataRow dataRow, Projection projection, JsonFilter jsonFilter) { Debug.Assert(!string.IsNullOrEmpty(projection.Name)); jsonWriter.WritePropertyName(projection.Name); jsonWriter.WriteStartObject(); jsonWriter.WriteMembers(dataRow, projection, jsonFilter); jsonWriter.WriteEndObject(); }
public void SupportSemicolons() { Assert.AreEqual(JsonFilter.TryParse("abc;!def,ghi;jkl").ToString(), "abc,!def,ghi,jkl"); }
private static void WriteMembers(this JsonWriter jsonWriter, DataRow dataRow, Projection projection, JsonFilter jsonFilter) { var count = 0; var columns = projection.Columns; for (int i = 0; i < columns.Count; i++) { var column = columns[i]; if (jsonFilter != null && !jsonFilter.ShouldSerialize(column)) { continue; } if (count > 0) { jsonWriter.WriteComma(); } jsonWriter.WritePropertyName(column.RelativeName); jsonWriter.Write(dataRow, column); count++; } }
public string GetUrl(ISearchQuery query) { var list = new List <string> { //クエリ― $"q={query.Query}" }; //検索タイプ var sType = query.SearchType switch { SearchType.Tag => "tagsExact", _ => "title,description,tags" }; list.Add($"targets={sType}"); //並び替え var sort = query.SortOption.Sort switch { Sort.ViewCount => "viewCounter", Sort.MylistCount => "mylistCounter", Sort.CommentCount => "commentCounter", Sort.UploadedTime => "startTime", Sort.Length => "lengthSeconds", Sort.LastCommentTime => "lastCommentTime", _ => "viewCounter", }; if (!query.SortOption.IsAscending) { sort = "-" + sort; } else { sort = HttpUtility.UrlEncode("+") + sort; } list.Add($"_sort={sort}"); //フィールド list.Add("fields=contentId,title,viewCounter,mylistCounter,thumbnailUrl,startTime,commentCounter"); //jsonフィルター if (query.Genre != Genre.All || query.UploadedDateTimeStart is not null) { var wrapper = new JsonFilterWrapper() { Type = "and" }; //ジャンル if (query.Genre != Genre.All) { var genre = query.Genre switch { Genre.Game => "ゲーム", Genre.MusicSound => "音楽・サウンド", Genre.Anime => "アニメ", Genre.Entertainment => "エンターテイメント", Genre.Dance => "ダンス", Genre.Other => "その他", _ => "other" }; var filter = new JsonFilter() { Type = "equal", Field = "genre", Value = genre }; wrapper.Filters.Add(filter); } //投稿日時 if (query.UploadedDateTimeStart is not null) { var filter = new JsonFilter() { Type = "range", Field = "startTime", From = HttpUtility.UrlEncode(query.UploadedDateTimeStart?.ToString(Format.ISO8601)), To = HttpUtility.UrlEncode((query.UploadedDateTimeEnd ?? DateTimeOffset.Now).ToString(Format.ISO8601)), IncludeLower = true, IncludeUpper = true, }; wrapper.Filters.Add(filter); } var serialized = JsonParser.Serialize(wrapper); list.Add($"jsonFilter={serialized}"); } list.Add("_limit=50"); list.Add($"_offset={50 * (query.Page - 1)}"); return(APIConstant.SnapshotAPIV2 + "?" + string.Join("&", list)); } }
public void IsIncluded() { Assert.IsFalse(JsonFilter.Parse("id").IsPathIncluded("name.first")); Assert.IsTrue(JsonFilter.Parse("!id").IsPathIncluded("name.first")); }
public void JoinPropertyPaths() { JsonFilter.JoinPaths( JsonFilter.JoinPropertyPaths <InterestingDto>(dto => dto.Name, dto => dto.Items[0].Id), JsonFilter.JoinExcludedPropertyPaths <InterestingDto>(dto => dto.NextId, dto => dto.Items[0].IsDeleted), "name,items.id,!next,!items.del"); }
private static void Write(this JsonWriter jsonWriter, DataRow dataRow, ColumnList columnList, JsonFilter jsonFilter) { jsonWriter.WritePropertyName(columnList.Name); jsonWriter.WriteStartArray(); var count = 0; for (int i = 0; i < columnList.Count; i++) { var column = columnList[i]; if (jsonFilter != null && !jsonFilter.ShouldSerialize(column)) { continue; } if (count > 0) { jsonWriter.WriteComma(); } jsonWriter.Write(dataRow, column); count++; } jsonWriter.WriteEndArray(); }
public void ChildPropertyPath() { Assert.AreEqual(JsonFilter.GetPropertyPath((InterestingDto dto) => dto.Name.First), "name.first"); Assert.AreEqual(JsonFilter.GetPropertyPath((InterestingDto dto) => dto.Name.Middle), "name.middle"); Assert.AreEqual(JsonFilter.GetPropertyPath((InterestingDto dto) => dto.Name.Last), "name.last"); }
public void CustomNamePropertyPath() { Assert.AreEqual(JsonFilter.GetPropertyPath((InterestingDto dto) => dto.NextId), "next"); }
public void SimplePropertyPath() { Assert.AreEqual(JsonFilter.GetPropertyPath((InterestingDto dto) => dto.Items), "items"); Assert.AreEqual(JsonFilter.GetPropertyPath((InterestingDto dto) => dto.Name), "name"); }
public void AddJson(JsonFilter filter) { AddFilter(filter); }
private static void Write(this JsonWriter jsonWriter, DataRow dataRow, Projection projection, JsonFilter jsonFilter) { if (string.IsNullOrEmpty(projection.Name)) { jsonWriter.WriteMembers(dataRow, projection, jsonFilter); } else { jsonWriter.WriteObject(dataRow, projection, jsonFilter); } }
public void NullFilterTokenTest() { Assert.IsNull(JsonFilter.Parse("name,!name.middle").FilterToken(null)); Assert.IsNull(JsonFilter.Parse("name,!name.middle").FilterObject((PersonDto)null)); }
public void ArrayPropertyPath() { Assert.AreEqual(JsonFilter.GetPropertyPath((InterestingDto dto) => dto.Items[0].Id), "items.id"); Assert.AreEqual(JsonFilter.GetPropertyPath((InterestingDto dto) => dto.Items[0].IsDeleted), "items.del"); }