Created
July 17, 2019 15:30
-
-
Save kyubuns/978b3498dc9189dee03cea13b4b68ad3 to your computer and use it in GitHub Desktop.
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
CompositeResolver.RegisterAndSetAsDefault(new IJsonFormatter[] | |
{ | |
new IntValueFormatter<Gold>(x => new Gold(x)), | |
new IDFormatter<StageID>(x => new StageID(x)), | |
}, new[] | |
{ | |
StandardResolver.Default, | |
}); | |
// ----- | |
private sealed class IntValueFormatter<T> : IJsonFormatter<T> where T : IIntValue | |
{ | |
private readonly Func<int, T> factory; | |
public IntValueFormatter(Func<int, T> factory) | |
{ | |
this.factory = factory; | |
} | |
public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver) | |
{ | |
throw new NotImplementedException(); | |
} | |
public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) | |
{ | |
return factory(reader.ReadInt32()); | |
} | |
} | |
private sealed class IDFormatter<T> : IJsonFormatter<T> where T : ITypeID | |
{ | |
private readonly Func<uint, T> factory; | |
public IDFormatter(Func<uint, T> factory) | |
{ | |
this.factory = factory; | |
} | |
public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver) | |
{ | |
throw new NotImplementedException(); | |
} | |
public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) | |
{ | |
var stringID = reader.ReadString(); | |
return factory(StringToID(stringID)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment