-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1127 from andy840119/sort-some-tags-while-load-sa…
…ve-beatmap Should re-sort the order while save the time-tags to json.
- Loading branch information
Showing
9 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/RomajiTagsConverterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using Newtonsoft.Json; | ||
using NUnit.Framework; | ||
using osu.Game.Rulesets.Karaoke.IO.Serialization.Converters; | ||
using osu.Game.Rulesets.Karaoke.Objects; | ||
using osu.Game.Rulesets.Karaoke.Tests.Asserts; | ||
|
||
namespace osu.Game.Rulesets.Karaoke.Tests.IO.Serialization.Converters | ||
{ | ||
public class RomajiTagsConverterTest : BaseSingleConverterTest<RomajiTagsConverter> | ||
{ | ||
protected override JsonConverter[] CreateExtraConverts() | ||
=> new JsonConverter[] | ||
{ | ||
new RomajiTagConverter(), | ||
}; | ||
|
||
[Test] | ||
public void TestSerialize() | ||
{ | ||
var timeTags = new[] | ||
{ | ||
new RomajiTag | ||
{ | ||
StartIndex = 2, | ||
EndIndex = 3, | ||
Text = "ji" | ||
}, | ||
new RomajiTag | ||
{ | ||
StartIndex = 1, | ||
EndIndex = 2, | ||
Text = "roma" | ||
}, | ||
}; | ||
|
||
const string expected = "[\"[1,2]:roma\",\"[2,3]:ji\"]"; | ||
string actual = JsonConvert.SerializeObject(timeTags, CreateSettings()); | ||
Assert.AreEqual(expected, actual); | ||
} | ||
|
||
[Test] | ||
public void TestDeserialize() | ||
{ | ||
const string json = "[\"[2,3]:ji\",\"[1,2]:roma\"]"; | ||
|
||
var expected = new[] | ||
{ | ||
new RomajiTag | ||
{ | ||
StartIndex = 1, | ||
EndIndex = 2, | ||
Text = "roma" | ||
}, | ||
new RomajiTag | ||
{ | ||
StartIndex = 2, | ||
EndIndex = 3, | ||
Text = "ji" | ||
}, | ||
}; | ||
var actual = JsonConvert.DeserializeObject<RomajiTag[]>(json, CreateSettings()); | ||
TextTagAssert.ArePropertyEqual(expected, actual); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/RubyTagsConverterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using Newtonsoft.Json; | ||
using NUnit.Framework; | ||
using osu.Game.Rulesets.Karaoke.IO.Serialization.Converters; | ||
using osu.Game.Rulesets.Karaoke.Objects; | ||
using osu.Game.Rulesets.Karaoke.Tests.Asserts; | ||
|
||
namespace osu.Game.Rulesets.Karaoke.Tests.IO.Serialization.Converters | ||
{ | ||
public class RubyTagsConverterTest : BaseSingleConverterTest<RubyTagsConverter> | ||
{ | ||
protected override JsonConverter[] CreateExtraConverts() | ||
=> new JsonConverter[] | ||
{ | ||
new RubyTagConverter(), | ||
}; | ||
|
||
[Test] | ||
public void TestSerialize() | ||
{ | ||
var timeTags = new[] | ||
{ | ||
new RubyTag | ||
{ | ||
StartIndex = 2, | ||
EndIndex = 3, | ||
Text = "ビ" | ||
}, | ||
new RubyTag | ||
{ | ||
StartIndex = 1, | ||
EndIndex = 2, | ||
Text = "ル" | ||
}, | ||
}; | ||
|
||
const string expected = "[\"[1,2]:ル\",\"[2,3]:ビ\"]"; | ||
string actual = JsonConvert.SerializeObject(timeTags, CreateSettings()); | ||
Assert.AreEqual(expected, actual); | ||
} | ||
|
||
[Test] | ||
public void TestDeserialize() | ||
{ | ||
const string json = "[\"[2,3]:ビ\",\"[1,2]:ル\"]"; | ||
|
||
var expected = new[] | ||
{ | ||
new RubyTag | ||
{ | ||
StartIndex = 1, | ||
EndIndex = 2, | ||
Text = "ル" | ||
}, | ||
new RubyTag | ||
{ | ||
StartIndex = 2, | ||
EndIndex = 3, | ||
Text = "ビ" | ||
}, | ||
}; | ||
var actual = JsonConvert.DeserializeObject<RubyTag[]>(json, CreateSettings()); | ||
TextTagAssert.ArePropertyEqual(expected, actual); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagsConverterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using Newtonsoft.Json; | ||
using NUnit.Framework; | ||
using osu.Framework.Graphics.Sprites; | ||
using osu.Game.Rulesets.Karaoke.IO.Serialization.Converters; | ||
using osu.Game.Rulesets.Karaoke.Objects; | ||
using osu.Game.Rulesets.Karaoke.Tests.Asserts; | ||
|
||
namespace osu.Game.Rulesets.Karaoke.Tests.IO.Serialization.Converters | ||
{ | ||
[TestFixture] | ||
public class TimeTagsConverterTest : BaseSingleConverterTest<TimeTagsConverter> | ||
{ | ||
protected override JsonConverter[] CreateExtraConverts() | ||
=> new JsonConverter[] | ||
{ | ||
new TimeTagConverter(), | ||
}; | ||
|
||
[Test] | ||
public void TestSerialize() | ||
{ | ||
var timeTags = new[] | ||
{ | ||
new TimeTag(new TextIndex(0, TextIndex.IndexState.End), 1000), | ||
new TimeTag(new TextIndex(0), 0) | ||
}; | ||
|
||
const string expected = "[\"[0,start]:0\",\"[0,end]:1000\"]"; | ||
string actual = JsonConvert.SerializeObject(timeTags, CreateSettings()); | ||
Assert.AreEqual(expected, actual); | ||
} | ||
|
||
[Test] | ||
public void TestDeserialize() | ||
{ | ||
const string json = "[\"[0,end]:1000\",\"[0,start]:0\"]"; | ||
|
||
var expected = new[] | ||
{ | ||
new TimeTag(new TextIndex(0), 0), | ||
new TimeTag(new TextIndex(0, TextIndex.IndexState.End), 1000), | ||
}; | ||
var actual = JsonConvert.DeserializeObject<TimeTag[]>(json, CreateSettings()); | ||
TimeTagAssert.ArePropertyEqual(expected, actual); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/RomajiTagsConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System.Collections.Generic; | ||
using osu.Game.Rulesets.Karaoke.Objects; | ||
using osu.Game.Rulesets.Karaoke.Utils; | ||
|
||
namespace osu.Game.Rulesets.Karaoke.IO.Serialization.Converters | ||
{ | ||
public class RomajiTagsConverter : SortableJsonConvertor<RomajiTag> | ||
{ | ||
protected override IEnumerable<RomajiTag> GetSortedValue(IEnumerable<RomajiTag> objects) | ||
=> TextTagsUtils.Sort(objects); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/RubyTagsConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System.Collections.Generic; | ||
using osu.Game.Rulesets.Karaoke.Objects; | ||
using osu.Game.Rulesets.Karaoke.Utils; | ||
|
||
namespace osu.Game.Rulesets.Karaoke.IO.Serialization.Converters | ||
{ | ||
public class RubyTagsConverter : SortableJsonConvertor<RubyTag> | ||
{ | ||
protected override IEnumerable<RubyTag> GetSortedValue(IEnumerable<RubyTag> objects) | ||
=> TextTagsUtils.Sort(objects); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/SortableJsonConvertor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagsConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System.Collections.Generic; | ||
using osu.Game.Rulesets.Karaoke.Objects; | ||
using osu.Game.Rulesets.Karaoke.Utils; | ||
|
||
namespace osu.Game.Rulesets.Karaoke.IO.Serialization.Converters | ||
{ | ||
public class TimeTagsConverter : SortableJsonConvertor<TimeTag> | ||
{ | ||
protected override IEnumerable<TimeTag> GetSortedValue(IEnumerable<TimeTag> objects) | ||
=> TimeTagsUtils.Sort(objects); | ||
} | ||
} |