Skip to content

Commit

Permalink
fixed first chapter having a length of 0 in json dumps and integrated…
Browse files Browse the repository at this point in the history
… chapter.UseOffset check
  • Loading branch information
sandreas committed Jan 16, 2023
1 parent fc98652 commit a3ddbad
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion tone/Metadata/Converters/ChaptersConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
writer.WritePropertyName("start");
writer.WriteValue(chapter.StartTime);

if (chapter.EndTime >= chapter.StartTime)
var chapterLength = CalculateChapterLength(chapter);
if (chapterLength != null)
{
var length = chapter.EndTime - chapter.StartTime;
writer.WritePropertyName("length");
Expand All @@ -47,6 +48,28 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
writer.WriteEndArray();
}

private uint? CalculateChapterLength(ChapterInfo chapter)
{
uint start;
uint end;
if (chapter.UseOffset)
{
start = chapter.StartOffset;
end = chapter.EndOffset;
}
else
{
start = chapter.StartTime;
end = chapter.EndTime;
}
if (end > 0 && end>=start)
{
return end - start;
}

return null;
}

// https://www.jerriepelser.com/blog/custom-converters-in-json-net-case-study-1/
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue,
JsonSerializer serializer)
Expand Down

0 comments on commit a3ddbad

Please sign in to comment.