Skip to content

Commit

Permalink
implement base sortable class.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Feb 21, 2022
1 parent 0ea897b commit 1d8a4e1
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace osu.Game.Rulesets.Karaoke.IO.Serialization.Converters
{
public abstract class SortableJsonConvertor<TObject> : JsonConverter<IEnumerable<TObject>>
{
public sealed override IEnumerable<TObject> ReadJson(JsonReader reader, Type objectType, IEnumerable<TObject> existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var obj = JArray.Load(reader);
var timeTags = obj.Select(x => serializer.Deserialize<TObject>(x.CreateReader()));
return GetSortedValue(timeTags);
}

public override void WriteJson(JsonWriter writer, IEnumerable<TObject> value, JsonSerializer serializer)
{
// see: https://stackoverflow.com/questions/3330989/order-of-serialized-fields-using-json-net
var sortedTimeTags = GetSortedValue(value);

writer.WriteStartArray();

foreach (var timeTag in sortedTimeTags)
{
serializer.Serialize(writer, timeTag);
}

writer.WriteEndArray();
}

protected abstract IEnumerable<TObject> GetSortedValue(IEnumerable<TObject> objects);
}
}

0 comments on commit 1d8a4e1

Please sign in to comment.