Skip to content

Commit

Permalink
[Refactoring] Fixed formatting & updated naming of factory interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
joerghartmann authored Jun 27, 2023
1 parent 0269986 commit ef09d6a
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 101 deletions.
8 changes: 3 additions & 5 deletions Client/Com/Cumulocity/Client/Converter/AlarmJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class AlarmJsonConverter<T> : BaseJsonConverter<T> where T : Alarm
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var instance = (T) Activator.CreateInstance(typeToConvert);
var additionalObjects = new Dictionary<string, object?>();
var instanceProperties = typeToConvert.GetTypeInfo().DeclaredProperties.ToList();
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
{
Expand All @@ -31,19 +30,18 @@ public class AlarmJsonConverter<T> : BaseJsonConverter<T> where T : Alarm
{
var current = objectEnumerator.Current;
var property = FindProperty(instanceProperties, current);

if (property != null)
if (property != null)
{
var value = current.Value.Deserialize(property.PropertyType, options);
property.SetValue(instance, value);
}
else
{
additionalObjects.Add(current.Name,
additionalObjects.Add(current.Name,
Alarm.Serialization.AdditionalPropertyClasses.TryGetValue(current.Name, out var type)
? current.Value.Deserialize(type, options)
: current.Value.Deserialize<object>(options));
}
}
}
}
instance.CustomFragments = additionalObjects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class AuditRecordJsonConverter<T> : BaseJsonConverter<T> where T : AuditR
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var instance = (T) Activator.CreateInstance(typeToConvert);
var additionalObjects = new Dictionary<string, object?>();
var additionalObjects = new Dictionary<string, object?>();
var instanceProperties = typeToConvert.GetTypeInfo().DeclaredProperties.ToList();
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
{
Expand All @@ -31,19 +31,18 @@ public class AuditRecordJsonConverter<T> : BaseJsonConverter<T> where T : AuditR
{
var current = objectEnumerator.Current;
var property = FindProperty(instanceProperties, current);

if (property != null)
if (property != null)
{
var value = current.Value.Deserialize(property.PropertyType, options);
property.SetValue(instance, value);
}
else
{
additionalObjects.Add(current.Name,
additionalObjects.Add(current.Name,
AuditRecord.Serialization.AdditionalPropertyClasses.TryGetValue(current.Name, out var type)
? current.Value.Deserialize(type, options)
: current.Value.Deserialize<object>(options));
}
}
}
}
instance.CustomProperties = additionalObjects;
Expand Down
96 changes: 48 additions & 48 deletions Client/Com/Cumulocity/Client/Converter/BaseJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,55 @@ namespace Client.Com.Cumulocity.Client.Converter;

public abstract class BaseJsonConverter<T> : JsonConverter<T> where T : class
{
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
writer.WriteStartObject();
var type = value.GetType();
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
writer.WriteStartObject();
var type = value.GetType();

foreach (PropertyInfo property in type.GetProperties())
{
var isIgnoredProperty = Attribute.IsDefined(property, typeof(JsonIgnoreAttribute));
if (property.CanRead && isIgnoredProperty == false)
{
var propertyValue = property.GetValue(value, null);
if (propertyValue != null)
{
if (typeof(IDictionary<string, object>).IsAssignableFrom(property.PropertyType))
{
var dictionary = propertyValue as IDictionary;
if (dictionary is not null)
{
foreach (DictionaryEntry item in dictionary)
{
writer.WritePropertyName((string)item.Key);
JsonSerializerWrapper.Serialize(writer, item.Value, options);
}
}
}
else
{
var jsonProperty = GetJsonPropertyNameAttribute(property);
var jsonPropertyName = jsonProperty?.Name ?? property.Name;
writer.WritePropertyName(jsonPropertyName);
JsonSerializerWrapper.Serialize(writer, propertyValue, options);
}
}
}
}
writer.WriteEndObject();
}
foreach (PropertyInfo property in type.GetProperties())
{
var isIgnoredProperty = Attribute.IsDefined(property, typeof(JsonIgnoreAttribute));
if (property.CanRead && isIgnoredProperty == false)
{
var propertyValue = property.GetValue(value, null);
if (propertyValue != null)
{
if (typeof(IDictionary<string, object>).IsAssignableFrom(property.PropertyType))
{
var dictionary = propertyValue as IDictionary;
if (dictionary is not null)
{
foreach (DictionaryEntry item in dictionary)
{
writer.WritePropertyName((string)item.Key);
JsonSerializerWrapper.Serialize(writer, item.Value, options);
}
}
}
else
{
var jsonProperty = GetJsonPropertyNameAttribute(property);
var jsonPropertyName = jsonProperty?.Name ?? property.Name;
writer.WritePropertyName(jsonPropertyName);
JsonSerializerWrapper.Serialize(writer, propertyValue, options);
}
}
}
}
writer.WriteEndObject();
}

protected PropertyInfo? FindProperty(List<PropertyInfo> instanceProperties, JsonProperty current)
{
return instanceProperties.Find(propertyInfo =>
{
var attribute = GetJsonPropertyNameAttribute(propertyInfo);
return current.NameEquals(attribute?.Name ?? propertyInfo.Name);
});
}
protected PropertyInfo? FindProperty(List<PropertyInfo> instanceProperties, JsonProperty current)
{
return instanceProperties.Find(propertyInfo =>
{
var attribute = GetJsonPropertyNameAttribute(propertyInfo);
return current.NameEquals(attribute?.Name ?? propertyInfo.Name);
});
}

protected JsonPropertyNameAttribute? GetJsonPropertyNameAttribute(MemberInfo propertyInfo)
{
return (JsonPropertyNameAttribute?)Attribute.GetCustomAttribute(propertyInfo, typeof(JsonPropertyNameAttribute));
}
protected JsonPropertyNameAttribute? GetJsonPropertyNameAttribute(MemberInfo propertyInfo)
{
return (JsonPropertyNameAttribute?)Attribute.GetCustomAttribute(propertyInfo, typeof(JsonPropertyNameAttribute));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CategoryOptionsJsonConverter<T> : BaseJsonConverter<T> where T : Ca
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var instance = (T) Activator.CreateInstance(typeToConvert);
var additionalObjects = new Dictionary<string, object?>();
var additionalObjects = new Dictionary<string, object?>();
var instanceProperties = typeToConvert.GetTypeInfo().DeclaredProperties.ToList();
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
{
Expand All @@ -31,19 +31,18 @@ public class CategoryOptionsJsonConverter<T> : BaseJsonConverter<T> where T : Ca
{
var current = objectEnumerator.Current;
var property = FindProperty(instanceProperties, current);

if (property != null)
if (property != null)
{
var value = current.Value.Deserialize(property.PropertyType, options);
property.SetValue(instance, value);
}
else
{
additionalObjects.Add(current.Name,
additionalObjects.Add(current.Name,
CategoryOptions.Serialization.AdditionalPropertyClasses.TryGetValue(current.Name, out var type)
? current.Value.Deserialize(type, options)
: current.Value.Deserialize<object>(options));
}
}
}
}
instance.KeyValuePairs = additionalObjects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CustomPropertiesJsonConverter<T> : BaseJsonConverter<T> where T : C
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var instance = (T) Activator.CreateInstance(typeToConvert);
var additionalObjects = new Dictionary<string, object?>();
var additionalObjects = new Dictionary<string, object?>();
var instanceProperties = typeToConvert.GetTypeInfo().DeclaredProperties.ToList();
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
{
Expand All @@ -31,19 +31,18 @@ public class CustomPropertiesJsonConverter<T> : BaseJsonConverter<T> where T : C
{
var current = objectEnumerator.Current;
var property = FindProperty(instanceProperties, current);

if (property != null)
if (property != null)
{
var value = current.Value.Deserialize(property.PropertyType, options);
property.SetValue(instance, value);
}
else
{
additionalObjects.Add(current.Name,
additionalObjects.Add(current.Name,
CustomProperties.Serialization.AdditionalPropertyClasses.TryGetValue(current.Name, out var type)
? current.Value.Deserialize(type, options)
: current.Value.Deserialize<object>(options));
}
}
}
}
instance.PCustomProperties = additionalObjects;
Expand Down
9 changes: 4 additions & 5 deletions Client/Com/Cumulocity/Client/Converter/EventJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class EventJsonConverter<T> : BaseJsonConverter<T> where T : Event
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var instance = (T) Activator.CreateInstance(typeToConvert);
var additionalObjects = new Dictionary<string, object?>();
var additionalObjects = new Dictionary<string, object?>();
var instanceProperties = typeToConvert.GetTypeInfo().DeclaredProperties.ToList();
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
{
Expand All @@ -31,19 +31,18 @@ public class EventJsonConverter<T> : BaseJsonConverter<T> where T : Event
{
var current = objectEnumerator.Current;
var property = FindProperty(instanceProperties, current);

if (property != null)
if (property != null)
{
var value = current.Value.Deserialize(property.PropertyType, options);
property.SetValue(instance, value);
}
else
{
additionalObjects.Add(current.Name,
additionalObjects.Add(current.Name,
Event.Serialization.AdditionalPropertyClasses.TryGetValue(current.Name, out var type)
? current.Value.Deserialize(type, options)
: current.Value.Deserialize<object>(options));
}
}
}
}
instance.CustomFragments = additionalObjects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ManagedObjectJsonConverter<T> : BaseJsonConverter<T> where T : Mana
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var instance = (T) Activator.CreateInstance(typeToConvert);
var additionalObjects = new Dictionary<string, object?>();
var additionalObjects = new Dictionary<string, object?>();
var instanceProperties = typeToConvert.GetTypeInfo().DeclaredProperties.ToList();
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
{
Expand All @@ -31,19 +31,18 @@ public class ManagedObjectJsonConverter<T> : BaseJsonConverter<T> where T : Mana
{
var current = objectEnumerator.Current;
var property = FindProperty(instanceProperties, current);

if (property != null)
if (property != null)
{
var value = current.Value.Deserialize(property.PropertyType, options);
property.SetValue(instance, value);
}
else
{
additionalObjects.Add(current.Name,
additionalObjects.Add(current.Name,
ManagedObject.Serialization.AdditionalPropertyClasses.TryGetValue(current.Name, out var type)
? current.Value.Deserialize(type, options)
: current.Value.Deserialize<object>(options));
}
}
}
}
instance.CustomFragments = additionalObjects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MeasurementJsonConverter<T> : BaseJsonConverter<T> where T : Measur
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var instance = (T) Activator.CreateInstance(typeToConvert);
var additionalObjects = new Dictionary<string, object?>();
var additionalObjects = new Dictionary<string, object?>();
var instanceProperties = typeToConvert.GetTypeInfo().DeclaredProperties.ToList();
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
{
Expand All @@ -31,19 +31,18 @@ public class MeasurementJsonConverter<T> : BaseJsonConverter<T> where T : Measur
{
var current = objectEnumerator.Current;
var property = FindProperty(instanceProperties, current);

if (property != null)
if (property != null)
{
var value = current.Value.Deserialize(property.PropertyType, options);
property.SetValue(instance, value);
}
else
{
additionalObjects.Add(current.Name,
additionalObjects.Add(current.Name,
Measurement.Serialization.AdditionalPropertyClasses.TryGetValue(current.Name, out var type)
? current.Value.Deserialize(type, options)
: current.Value.Deserialize<object>(options));
}
}
}
}
instance.CustomFragments = additionalObjects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class OperationJsonConverter<T> : BaseJsonConverter<T> where T : Operatio
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var instance = (T) Activator.CreateInstance(typeToConvert);
var additionalObjects = new Dictionary<string, object?>();
var additionalObjects = new Dictionary<string, object?>();
var instanceProperties = typeToConvert.GetTypeInfo().DeclaredProperties.ToList();
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
{
Expand All @@ -31,19 +31,18 @@ public class OperationJsonConverter<T> : BaseJsonConverter<T> where T : Operatio
{
var current = objectEnumerator.Current;
var property = FindProperty(instanceProperties, current);

if (property != null)
if (property != null)
{
var value = current.Value.Deserialize(property.PropertyType, options);
property.SetValue(instance, value);
}
else
{
additionalObjects.Add(current.Name,
additionalObjects.Add(current.Name,
Operation.Serialization.AdditionalPropertyClasses.TryGetValue(current.Name, out var type)
? current.Value.Deserialize(type, options)
: current.Value.Deserialize<object>(options));
}
}
}
}
instance.CustomFragments = additionalObjects;
Expand Down
Loading

0 comments on commit ef09d6a

Please sign in to comment.