Skip to content

Closes #294: Preserving custom object property names sent to SetProperty and AddObject #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions src/Exceptionless/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@

namespace Exceptionless.Extensions {
public static class StringExtensions {
internal static string ToLowerUnderscoredWords(this string value) {
var builder = new StringBuilder(value.Length + 10);
for (int index = 0; index < value.Length; index++) {
char c = value[index];
if (Char.IsUpper(c)) {
if (index > 0 && value[index - 1] != '_')
builder.Append('_');

builder.Append(Char.ToLower(c));
} else {
builder.Append(c);
}
}

return builder.ToString();
}

public static bool AnyWildcardMatches(this string value, IEnumerable<string> patternsToMatch, bool ignoreCase = true) {
if (patternsToMatch == null || value == null)
return false;
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/ClientConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Exceptionless.Models {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class ClientConfiguration {
public ClientConfiguration() {
Settings = new SettingsDictionary();
Expand Down
3 changes: 3 additions & 0 deletions src/Exceptionless/Models/Client/Data/EnvironmentInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class EnvironmentInfo : IData {
public EnvironmentInfo() {
Data = new DataDictionary();
Expand Down Expand Up @@ -66,11 +67,13 @@ public EnvironmentInfo() {
/// <summary>
/// The OS name that the error occurred on.
/// </summary>
[Json.JsonProperty("o_s_name")]
public string OSName { get; set; }

/// <summary>
/// The OS version that the error occurred on.
/// </summary>
[Json.JsonProperty("o_s_version")]
public string OSVersion { get; set; }

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Data/Error.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class Error : InnerError {
public Error() {
Modules = new ModuleCollection();
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Data/InnerError.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class InnerError : IData {
public InnerError() {
Data = new DataDictionary();
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Data/ManualStackingInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Exceptionless.Extensions;

namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class ManualStackingInfo {
public ManualStackingInfo() {
SignatureData = new Dictionary<string, string>();
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Data/Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;

namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class Method : IData {
public Method() {
Data = new DataDictionary();
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Data/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;

namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class Module : IData {
public Module() {
Data = new DataDictionary();
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Data/Parameter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class Parameter : IData {
public Parameter() {
Data = new DataDictionary();
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Data/RequestInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;

namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class RequestInfo : IData {
public RequestInfo() {
Data = new DataDictionary();
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Data/SimpleError.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class SimpleError : SimpleInnerError {
public SimpleError() {
Modules = new ModuleCollection();
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Data/SimpleInnerError.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class SimpleInnerError : IData {
public SimpleInnerError() {
Data = new DataDictionary();
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Data/StackFrame.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class StackFrame : Method {
public string FileName { get; set; }
public int LineNumber { get; set; }
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Data/UserDescription.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;

namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class UserDescription : IData {
public UserDescription() {
Data = new DataDictionary();
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Data/UserInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;

namespace Exceptionless.Models.Data {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class UserInfo : IData {
public UserInfo() {
Data = new DataDictionary();
Expand Down
1 change: 1 addition & 0 deletions src/Exceptionless/Models/Client/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;

namespace Exceptionless.Models {
[Json.JsonObject(NamingStrategyType = typeof(Json.Serialization.SnakeCaseNamingStrategy))]
public class Event : IData {
public Event() {
Tags = new TagSet();
Expand Down
8 changes: 4 additions & 4 deletions src/Exceptionless/Serializer/DataDictionaryConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
var dictionary = new DataDictionary();
foreach (string key in result.Keys) {
object value = result[key];
if (value is JObject)
dictionary[key] = value.ToString();
else if (value is JArray)
dictionary[key] = value.ToString();
if (value is JObject jObject)
dictionary[key] = jObject.ToString(serializer.Formatting);
else if (value is JArray jArray)
dictionary[key] = jArray.ToString(serializer.Formatting);
else
dictionary[key] = value;
}
Expand Down
27 changes: 3 additions & 24 deletions src/Exceptionless/Serializer/DefaultJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public DefaultJsonSerializer() {
_serializerSettings = new JsonSerializerSettings {
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
PreserveReferencesHandling = PreserveReferencesHandling.None,
FloatParseHandling = FloatParseHandling.Decimal,
ContractResolver = new ExceptionlessContractResolver()
};

Expand Down Expand Up @@ -48,13 +49,11 @@ public virtual string Serialize(object model, string[] exclusions = null, int ma

using (var sw = new StringWriter()) {
using (var jw = new JsonTextWriterWithExclusions(sw, exclusions)) {
jw.Formatting = Formatting.None;
Func<JsonProperty, object, bool> include = (property, value) => ShouldSerialize(jw, property, value, maxDepth, exclusions);
var resolver = new ExceptionlessContractResolver(include);
serializer.ContractResolver = resolver;
serializer.ContractResolver = new ExceptionlessContractResolver(include);
if (continueOnSerializationError)
serializer.Error += (sender, args) => { args.ErrorContext.Handled = true; };

serializer.Serialize(jw, model);
}

Expand All @@ -78,26 +77,6 @@ private bool ShouldSerialize(JsonTextWriterWithDepth jw, JsonProperty property,
bool isPastMaxDepth = !(isPrimitiveType ? jw.CurrentDepth <= maxDepth : jw.CurrentDepth < maxDepth);
if (isPastMaxDepth)
return false;

if (isPrimitiveType)
return true;

object value = property.ValueProvider.GetValue(obj);
if (value == null)
return true;

if (typeof(ICollection).GetTypeInfo().IsAssignableFrom(property.PropertyType.GetTypeInfo())) {
var collection = value as ICollection;
if (collection != null)
return collection.Count > 0;
}

var collectionType = value.GetType().GetInterfaces().FirstOrDefault(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection<>));
if (collectionType != null) {
var countProperty = collectionType.GetProperty("Count");
if (countProperty != null)
return (int)countProperty.GetValue(value, null) > 0;
}
} catch (Exception) {}

return true;
Expand Down
11 changes: 0 additions & 11 deletions src/Exceptionless/Serializer/ExceptionlessContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Reflection;
using Exceptionless.Json;
using Exceptionless.Json.Serialization;
using Exceptionless.Extensions;

namespace Exceptionless.Serializer {
internal class ExceptionlessContractResolver : DefaultContractResolver {
Expand All @@ -21,15 +20,5 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ
property.ShouldSerialize = obj => _includeProperty(property, obj) && (shouldSerialize == null || shouldSerialize(obj));
return property;
}

protected override JsonDictionaryContract CreateDictionaryContract(Type objectType) {
var contract = base.CreateDictionaryContract(objectType);
contract.DictionaryKeyResolver = propertyName => propertyName;
return contract;
}

protected override string ResolvePropertyName(string propertyName) {
return propertyName.ToLowerUnderscoredWords();
}
}
}
4 changes: 1 addition & 3 deletions src/Exceptionless/Serializer/JsonTextWriterWithExclusions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ public JsonTextWriterWithExclusions(TextWriter textWriter, string[] excludedProp

public override bool ShouldWriteProperty(string name) {
var exclusions = _excludedPropertyNames;
return exclusions is null ||
exclusions.Length == 0 ||
!name.AnyWildcardMatches(exclusions, ignoreCase: true);
return exclusions == null || exclusions.Length == 0 || !name.AnyWildcardMatches(exclusions, ignoreCase: true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void CanHandleObject() {
var ev = new Event();
ev.SetProperty(nameof(order), order, excludedPropertyNames: new [] { nameof(order.CardLast4) });
Assert.Single(ev.Data);
Assert.Equal("{\"id\":\"1234\",\"data\":{}}", ev.Data.GetString(nameof(order)));
Assert.Equal("{\"Id\":\"1234\",\"Data\":{}}", ev.Data.GetString(nameof(order)));
}

[InlineData("Credit*", true)]
Expand Down
Loading