Skip to content

Instantly share code, notes, and snippets.

@kyubuns
Created July 17, 2019 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyubuns/978b3498dc9189dee03cea13b4b68ad3 to your computer and use it in GitHub Desktop.
Save kyubuns/978b3498dc9189dee03cea13b4b68ad3 to your computer and use it in GitHub Desktop.
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