Skip to content
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
2 changes: 1 addition & 1 deletion Ical.Net/DataTypes/RequestStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override void CopyFrom(ICopyable obj)
ExtraData = rs.ExtraData;
}

public override string ToString()
public override string? ToString()
{
var serializer = new RequestStatusSerializer();
return serializer.SerializeToString(this);
Expand Down
2 changes: 1 addition & 1 deletion Ical.Net/DataTypes/StatusCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
statusCode.Parts.CopyTo(Parts, 0);
}

public override string ToString() => new StatusCodeSerializer().SerializeToString(this);
public override string? ToString() => new StatusCodeSerializer().SerializeToString(this);

Check warning on line 71 in Ical.Net/DataTypes/StatusCode.cs

View check run for this annotation

Codecov / codecov/patch

Ical.Net/DataTypes/StatusCode.cs#L71

Added line #L71 was not covered by tests

protected bool Equals(StatusCode other) => Parts.SequenceEqual(other.Parts);

Expand Down
3 changes: 2 additions & 1 deletion Ical.Net/Serialization/CalendarComponentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license.
//

#nullable enable
using Ical.Net.CalendarComponents;

namespace Ical.Net.Serialization;
Expand Down Expand Up @@ -48,4 +49,4 @@ public virtual ICalendarComponent Build(string objectName)
c.Name = name;
return c;
}
}
}
9 changes: 5 additions & 4 deletions Ical.Net/Serialization/CalendarSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license.
//

#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -11,7 +12,7 @@

public class CalendarSerializer : ComponentSerializer
{
private readonly Calendar _calendar;
private readonly Calendar? _calendar;

public CalendarSerializer()
: this(new SerializationContext()) { }
Expand All @@ -23,16 +24,16 @@

public CalendarSerializer(SerializationContext ctx) : base(ctx) { }

public virtual string SerializeToString() => SerializeToString(_calendar);
public virtual string? SerializeToString() => SerializeToString(_calendar);

protected override IComparer<ICalendarProperty> PropertySorter => new CalendarPropertySorter();


public override object Deserialize(TextReader tr) => null;
public override object? Deserialize(TextReader tr) => null;

Check warning on line 32 in Ical.Net/Serialization/CalendarSerializer.cs

View check run for this annotation

Codecov / codecov/patch

Ical.Net/Serialization/CalendarSerializer.cs#L32

Added line #L32 was not covered by tests

private class CalendarPropertySorter : IComparer<ICalendarProperty>
{
public int Compare(ICalendarProperty x, ICalendarProperty y)
public int Compare(ICalendarProperty? x, ICalendarProperty? y)
{
if (x == y)
{
Expand Down
19 changes: 11 additions & 8 deletions Ical.Net/Serialization/ComponentSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license.
//

#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -23,9 +24,9 @@

public override Type TargetType => typeof(CalendarComponent);

public override string SerializeToString(object obj)
public override string? SerializeToString(object? obj)
{
if (!(obj is ICalendarComponent c))
if (obj is not ICalendarComponent c || SerializationContext == null)
{
return null;
}
Expand All @@ -44,27 +45,29 @@
foreach (var p in properties)
{
// Get a serializer for each property.
var serializer = sf.Build(p.GetType(), SerializationContext) as IStringSerializer;
sb.Append(serializer.SerializeToString(p));
var serializer = sf?.Build(p.GetType(), SerializationContext) as IStringSerializer;
var val = serializer?.SerializeToString(p);
if (val != null) sb.Append(val);
}

// Serialize child objects
foreach (var child in c.Children)
{
// Get a serializer for each child object.
var serializer = sf.Build(child.GetType(), SerializationContext) as IStringSerializer;
sb.Append(serializer.SerializeToString(child));
var serializer = sf?.Build(child.GetType(), SerializationContext) as IStringSerializer;
var val = serializer?.SerializeToString(child);
if (val != null) sb.Append(val);
}

sb.FoldLines($"END:{upperName}");
return sb.ToString();
}

public override object Deserialize(TextReader tr) => null;
public override object? Deserialize(TextReader tr) => null;

Check warning on line 66 in Ical.Net/Serialization/ComponentSerializer.cs

View check run for this annotation

Codecov / codecov/patch

Ical.Net/Serialization/ComponentSerializer.cs#L66

Added line #L66 was not covered by tests

public class PropertyAlphabetizer : IComparer<ICalendarProperty>
{
public int Compare(ICalendarProperty x, ICalendarProperty y)
public int Compare(ICalendarProperty? x, ICalendarProperty? y)
{
if (x == y)
{
Expand Down
17 changes: 9 additions & 8 deletions Ical.Net/Serialization/DataMapSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license.
//

#nullable enable
using System;
using System.IO;
using Ical.Net.Serialization.DataTypes;
Expand All @@ -15,19 +16,19 @@

public DataMapSerializer(SerializationContext ctx) : base(ctx) { }

protected IStringSerializer GetMappedSerializer()
protected IStringSerializer? GetMappedSerializer()
{
var sf = GetService<ISerializerFactory>();
var mapper = GetService<DataTypeMapper>();
if (sf == null || mapper == null)
if (sf == null || mapper == null || SerializationContext == null)
{
return null;
}

var obj = SerializationContext.Peek();

// Get the data type for this object
var type = mapper.GetPropertyMapping(obj);
var type = obj != null ? mapper.GetPropertyMapping(obj) : null;

Check warning on line 31 in Ical.Net/Serialization/DataMapSerializer.cs

View check run for this annotation

Codecov / codecov/patch

Ical.Net/Serialization/DataMapSerializer.cs#L31

Added line #L31 was not covered by tests

return type == null
? new StringSerializer(SerializationContext)
Expand All @@ -38,18 +39,18 @@
{
get
{
ISerializer serializer = GetMappedSerializer();
return serializer?.TargetType;
ISerializer serializer = GetMappedSerializer() ?? throw new InvalidOperationException("Cannot detect the mapped serializer");
return serializer.TargetType;

Check warning on line 43 in Ical.Net/Serialization/DataMapSerializer.cs

View check run for this annotation

Codecov / codecov/patch

Ical.Net/Serialization/DataMapSerializer.cs#L43

Added line #L43 was not covered by tests
}
}

public override string SerializeToString(object obj)
public override string? SerializeToString(object? obj)
{
var serializer = GetMappedSerializer();
return serializer?.SerializeToString(obj);
}

public override object Deserialize(TextReader tr)
public override object? Deserialize(TextReader tr)
{
var serializer = GetMappedSerializer();
if (serializer == null)
Expand All @@ -67,4 +68,4 @@
// as try/catch is much slower than other means.
return returnValue ?? value;
}
}
}
33 changes: 15 additions & 18 deletions Ical.Net/Serialization/DataTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license.
//

#nullable enable
using System;
using System.Collections.Generic;
using Ical.Net.CalendarComponents;
Expand All @@ -16,8 +17,8 @@
{
private class PropertyMapping
{
public Type ObjectType { get; set; }
public TypeResolverDelegate Resolver { get; set; }
public Type? ObjectType { get; set; }
public TypeResolverDelegate? Resolver { get; set; }
public bool AllowsMultipleValuesPerProperty { get; set; }
}

Expand Down Expand Up @@ -66,25 +67,21 @@

protected Type ResolveStatusProperty(object context)
{
if (!(context is ICalendarObject obj))
if (context is not ICalendarObject obj)
{
return null;
return typeof(object); // Return a default type to match the delegate signature

Check warning on line 72 in Ical.Net/Serialization/DataTypeMapper.cs

View check run for this annotation

Codecov / codecov/patch

Ical.Net/Serialization/DataTypeMapper.cs#L72

Added line #L72 was not covered by tests
}

switch (obj.Parent)
return obj.Parent switch
{
case CalendarEvent _:
return typeof(EventStatus);
case Todo _:
return typeof(TodoStatus);
case Journal _:
return typeof(JournalStatus);
}

return null;
CalendarEvent _ => typeof(EventStatus),
Todo _ => typeof(TodoStatus),
Journal _ => typeof(JournalStatus),
_ => typeof(object) // Return a default type to match the delegate signature
};
}

public void AddPropertyMapping(string name, Type objectType, bool allowsMultipleValues)
public void AddPropertyMapping(string? name, Type? objectType, bool allowsMultipleValues)
{
if (name == null || objectType == null)
{
Expand All @@ -100,7 +97,7 @@
_propertyMap[name] = m;
}

public void AddPropertyMapping(string name, TypeResolverDelegate resolver, bool allowsMultipleValues)
public void AddPropertyMapping(string? name, TypeResolverDelegate? resolver, bool allowsMultipleValues)
{
if (name == null || resolver == null)
{
Expand All @@ -116,7 +113,7 @@
_propertyMap[name] = m;
}

public void RemovePropertyMapping(string name)
public void RemovePropertyMapping(string? name)
{
if (name != null && _propertyMap.ContainsKey(name))
{
Expand All @@ -132,7 +129,7 @@
&& m.AllowsMultipleValuesPerProperty;
}

public virtual Type GetPropertyMapping(object obj)
public virtual Type? GetPropertyMapping(object obj)
{
var p = obj as ICalendarProperty;
if (p?.Name == null)
Expand Down
105 changes: 32 additions & 73 deletions Ical.Net/Serialization/DataTypeSerializerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,53 @@
// Licensed under the MIT license.
//

#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Ical.Net.DataTypes;
using Ical.Net.Serialization.DataTypes;

namespace Ical.Net.Serialization;

public class DataTypeSerializerFactory : ISerializerFactory
{
private static readonly Dictionary<Type, Func<SerializationContext, ISerializer>> _serializerMap =
new()
{
{ typeof(Attachment), ctx => new AttachmentSerializer(ctx) },
{ typeof(Attendee), ctx => new AttendeeSerializer(ctx) },
{ typeof(CalDateTime), ctx => new DateTimeSerializer(ctx) },
{ typeof(FreeBusyEntry), ctx => new FreeBusyEntrySerializer(ctx) },
{ typeof(GeographicLocation), ctx => new GeographicLocationSerializer(ctx) },
{ typeof(Organizer), ctx => new OrganizerSerializer(ctx) },
{ typeof(Period), ctx => new PeriodSerializer(ctx) },
{ typeof(PeriodList), ctx => new PeriodListSerializer(ctx) },
{ typeof(RecurrencePattern), ctx => new RecurrencePatternSerializer(ctx) },
{ typeof(RequestStatus), ctx => new RequestStatusSerializer(ctx) },
{ typeof(StatusCode), ctx => new StatusCodeSerializer(ctx) },
{ typeof(Trigger), ctx => new TriggerSerializer(ctx) },
{ typeof(UtcOffset), ctx => new UtcOffsetSerializer(ctx) },
{ typeof(WeekDay), ctx => new WeekDaySerializer(ctx) }
};

/// <summary>
/// Returns a serializer that can be used to serialize and object
/// of type <paramref name="objectType"/>.
/// <note>
/// TODO: Add support for caching.
/// </note>
/// </summary>
/// <param name="objectType">The type of object to be serialized.</param>
/// <param name="ctx">The serialization context.</param>
public virtual ISerializer Build(Type objectType, SerializationContext ctx)
public virtual ISerializer? Build(Type? objectType, SerializationContext ctx)
{
if (objectType != null)
{
ISerializer s;
if (objectType == null) return null;

if (typeof(Attachment).IsAssignableFrom(objectType))
{
s = new AttachmentSerializer(ctx);
}
else if (typeof(Attendee).IsAssignableFrom(objectType))
{
s = new AttendeeSerializer(ctx);
}
else if (typeof(CalDateTime).IsAssignableFrom(objectType))
{
s = new DateTimeSerializer(ctx);
}
else if (typeof(FreeBusyEntry).IsAssignableFrom(objectType))
{
s = new FreeBusyEntrySerializer(ctx);
}
else if (typeof(GeographicLocation).IsAssignableFrom(objectType))
{
s = new GeographicLocationSerializer(ctx);
}
else if (typeof(Organizer).IsAssignableFrom(objectType))
{
s = new OrganizerSerializer(ctx);
}
else if (typeof(Period).IsAssignableFrom(objectType))
{
s = new PeriodSerializer(ctx);
}
else if (typeof(PeriodList).IsAssignableFrom(objectType))
{
s = new PeriodListSerializer(ctx);
}
else if (typeof(RecurrencePattern).IsAssignableFrom(objectType))
{
s = new RecurrencePatternSerializer(ctx);
}
else if (typeof(RequestStatus).IsAssignableFrom(objectType))
{
s = new RequestStatusSerializer(ctx);
}
else if (typeof(StatusCode).IsAssignableFrom(objectType))
{
s = new StatusCodeSerializer(ctx);
}
else if (typeof(Trigger).IsAssignableFrom(objectType))
{
s = new TriggerSerializer(ctx);
}
else if (typeof(UtcOffset).IsAssignableFrom(objectType))
{
s = new UtcOffsetSerializer(ctx);
}
else if (typeof(WeekDay).IsAssignableFrom(objectType))
{
s = new WeekDaySerializer(ctx);
}
// Default to a string serializer, which simply calls
// ToString() on the value to serialize it.
else
{
s = new StringSerializer(ctx);
}
// Check if the type exists in the map
var serializer = _serializerMap
.Where(entry => entry.Key.IsAssignableFrom(objectType))
.Select(entry => entry.Value(ctx))
.FirstOrDefault();

return s;
}
return null;
// Return the found serializer or default to a string serializer
return serializer ?? new StringSerializer(ctx);
}
}
}
Loading
Loading