-
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 #1493 from andy840119/implement-lyric-translate-js…
…on-convertor Implement lyric translate json convertor.
- Loading branch information
Showing
5 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TranslatesConvertorTest.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,53 @@ | ||
// 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.Globalization; | ||
using Newtonsoft.Json; | ||
using NUnit.Framework; | ||
using osu.Game.Rulesets.Karaoke.IO.Serialization.Converters; | ||
|
||
namespace osu.Game.Rulesets.Karaoke.Tests.IO.Serialization.Converters | ||
{ | ||
public class TranslatesConvertorTest : BaseSingleConverterTest<TranslatesConvertor> | ||
{ | ||
protected override JsonConverter[] CreateExtraConverts() | ||
=> new JsonConverter[] | ||
{ | ||
new CultureInfoConverter(), | ||
}; | ||
|
||
[Test] | ||
public void TestSerialize() | ||
{ | ||
var translates = new Dictionary<CultureInfo, string> | ||
{ | ||
{ new CultureInfo("en-US"), "karaoke" }, | ||
{ new CultureInfo("Ja-jp"), "カラオケ" } | ||
}; | ||
|
||
const string expected = "[{\"key\":1033,\"value\":\"karaoke\"},{\"key\":1041,\"value\":\"カラオケ\"}]"; | ||
string actual = JsonConvert.SerializeObject(translates, CreateSettings()); | ||
Assert.AreEqual(expected, actual); | ||
} | ||
|
||
[Test] | ||
public void TestDeserialize() | ||
{ | ||
const string json = "[{\"key\":1033,\"value\":\"karaoke\"},{\"key\":1041,\"value\":\"カラオケ\"}]"; | ||
|
||
var expected = new Dictionary<CultureInfo, string> | ||
{ | ||
{ new CultureInfo("en-US"), "karaoke" }, | ||
{ new CultureInfo("Ja-jp"), "カラオケ" } | ||
}; | ||
|
||
var actual = JsonConvert.DeserializeObject<Dictionary<CultureInfo, string>>(json, CreateSettings()) ?? throw new InvalidCastException(); | ||
CollectionAssert.AreEquivalent(expected, actual); | ||
|
||
var actualWithInterface = JsonConvert.DeserializeObject<IDictionary<CultureInfo, string>>(json, CreateSettings()) ?? throw new InvalidCastException(); | ||
CollectionAssert.AreEquivalent(expected, actualWithInterface); | ||
} | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/DictionaryConverter.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,45 @@ | ||
// 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 DictionaryConverter<TKey, TValue> : JsonConverter<IDictionary<TKey, TValue>> | ||
{ | ||
public sealed override IDictionary<TKey, TValue> ReadJson(JsonReader reader, Type objectType, IDictionary<TKey, TValue>? existingValue, bool hasExistingValue, JsonSerializer serializer) | ||
{ | ||
var obj = JArray.Load(reader); | ||
return obj.OfType<JObject>().ToDictionary( | ||
x => deserializeKey((JProperty)x.First!), | ||
x => deserializeValue((JProperty)x.Last!) | ||
); | ||
|
||
TKey deserializeKey(JProperty token) | ||
=> serializer.Deserialize<TKey>(token.Value.CreateReader())!; | ||
|
||
TValue deserializeValue(JProperty token) | ||
=> serializer.Deserialize<TValue>(token.Value.CreateReader())!; | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, IDictionary<TKey, TValue>? value, JsonSerializer serializer) | ||
{ | ||
if (value == null) | ||
throw new ArgumentNullException(nameof(value)); | ||
|
||
writer.WriteStartArray(); | ||
|
||
foreach (var keyValuePair in value) | ||
{ | ||
var jObject = JObject.FromObject(keyValuePair, serializer); | ||
jObject.WriteTo(writer); | ||
} | ||
|
||
writer.WriteEndArray(); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TranslatesConvertor.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,11 @@ | ||
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System.Globalization; | ||
|
||
namespace osu.Game.Rulesets.Karaoke.IO.Serialization.Converters | ||
{ | ||
public class TranslatesConvertor : DictionaryConverter<CultureInfo, string> | ||
{ | ||
} | ||
} |
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