Skip to content

Commit

Permalink
Skip pattern maching for empty sets (#258)
Browse files Browse the repository at this point in the history
* Skip pattern maching for empty sets

* Skip pattern maching for empty sets

* Fix logic

Co-authored-by: Blake Niemyjski <[email protected]>
  • Loading branch information
benaadams and niemyjski authored Dec 30, 2021
1 parent 6bbe515 commit e8efdf9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Exceptionless/Extensions/DataDictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static void AddObject(this IData data, ExtendedDataInfo info, Exceptionle
int index = 1;
while (data.Data.ContainsKey(name))
name = dataType.Name + index++;
} else if (name.AnyWildcardMatches(exclusions, true)) {
} else if (exclusions.Length > 0 && name.AnyWildcardMatches(exclusions, true)) {
return;
}

Expand Down
9 changes: 4 additions & 5 deletions src/Exceptionless/Serializer/DefaultJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ public virtual string Serialize(object model, string[] exclusions = null, int ma
if (maxDepth < 1)
maxDepth = Int32.MaxValue;

var excludedPropertyNames = new HashSet<string>(exclusions ?? new string[0], StringComparer.OrdinalIgnoreCase);
using (var sw = new StringWriter()) {
using (var jw = new JsonTextWriterWithExclusions(sw, excludedPropertyNames)) {
using (var jw = new JsonTextWriterWithExclusions(sw, exclusions)) {
jw.Formatting = Formatting.None;
Func<JsonProperty, object, bool> include = (property, value) => ShouldSerialize(jw, property, value, maxDepth, excludedPropertyNames);
Func<JsonProperty, object, bool> include = (property, value) => ShouldSerialize(jw, property, value, maxDepth, exclusions);
var resolver = new ExceptionlessContractResolver(include);
serializer.ContractResolver = resolver;
if (continueOnSerializationError)
Expand All @@ -71,9 +70,9 @@ public virtual object Deserialize(string json, Type type) {
return JsonConvert.DeserializeObject(json, type, _serializerSettings);
}

private bool ShouldSerialize(JsonTextWriterWithDepth jw, JsonProperty property, object obj, int maxDepth, ISet<string> excludedPropertyNames) {
private bool ShouldSerialize(JsonTextWriterWithDepth jw, JsonProperty property, object obj, int maxDepth, string[] excludedPropertyNames) {
try {
if (excludedPropertyNames != null && (property.UnderlyingName.AnyWildcardMatches(excludedPropertyNames, true) || property.PropertyName.AnyWildcardMatches(excludedPropertyNames, true)))
if (excludedPropertyNames != null && excludedPropertyNames.Length > 0 && (property.UnderlyingName.AnyWildcardMatches(excludedPropertyNames, ignoreCase: true) || property.PropertyName.AnyWildcardMatches(excludedPropertyNames, ignoreCase: true)))
return false;

bool isPrimitiveType = DefaultContractResolver.IsJsonPrimitiveType(property.PropertyType);
Expand Down
11 changes: 7 additions & 4 deletions src/Exceptionless/Serializer/JsonTextWriterWithExclusions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@

namespace Exceptionless.Serializer
{
internal class JsonTextWriterWithExclusions : JsonTextWriterWithDepth {
private readonly ISet<string> _excludedPropertyNames;
internal sealed class JsonTextWriterWithExclusions : JsonTextWriterWithDepth {
private readonly string[] _excludedPropertyNames;

public JsonTextWriterWithExclusions(TextWriter textWriter, ISet<string> excludedPropertyNames) : base(textWriter) {
public JsonTextWriterWithExclusions(TextWriter textWriter, string[] excludedPropertyNames) : base(textWriter) {
_excludedPropertyNames = excludedPropertyNames;
}

public override bool ShouldWriteProperty(string name) {
return !name.AnyWildcardMatches(_excludedPropertyNames, true);
var exclusions = _excludedPropertyNames;
return exclusions is null ||
exclusions.Length == 0 ||
!name.AnyWildcardMatches(exclusions, ignoreCase: true);
}
}
}

0 comments on commit e8efdf9

Please sign in to comment.