Fixes JSON WorldConverter (#263)
- [X] Adds Map index support for JSON MapConverter - [X] Files JSON WorldConverter - [X] Removes LINQ for Map.Parse Bumps release version
This commit is contained in:
parent
13e2693b59
commit
93cf40ec81
7 changed files with 73 additions and 54 deletions
|
|
@ -21,10 +21,15 @@ namespace Server.Json
|
|||
{
|
||||
public class MapConverter : JsonConverter<Map>
|
||||
{
|
||||
public override Map Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
=> Map.Parse(reader.GetString());
|
||||
public override Map Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
|
||||
reader.TokenType switch
|
||||
{
|
||||
JsonTokenType.String => Map.Parse(reader.GetString()),
|
||||
JsonTokenType.Number => Map.Maps[reader.GetInt32()],
|
||||
_ => throw new JsonException($"Value must be a number or string")
|
||||
};
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Map value, JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value.Name);
|
||||
public override void Write(Utf8JsonWriter writer, Map value, JsonSerializerOptions options) =>
|
||||
writer.WriteStringValue(value.Name);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,9 @@ namespace Server.Json
|
|||
{
|
||||
public class WorldLocationConverter : JsonConverter<WorldLocation>
|
||||
{
|
||||
private static Point3DConverter _point3DConverter;
|
||||
private static MapConverter _mapConverter;
|
||||
|
||||
private WorldLocation DeserializeArray(ref Utf8JsonReader reader)
|
||||
{
|
||||
Span<int> data = stackalloc int[3];
|
||||
|
|
@ -45,6 +48,7 @@ namespace Server.Json
|
|||
else if (count == 3)
|
||||
{
|
||||
map = Map.Maps[reader.GetInt32()];
|
||||
hasMap = true;
|
||||
}
|
||||
|
||||
count++;
|
||||
|
|
@ -52,12 +56,20 @@ namespace Server.Json
|
|||
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
map = Map.Parse(reader.GetString());
|
||||
var key = reader.GetString();
|
||||
|
||||
if (count != 3 || hasMap)
|
||||
{
|
||||
throw new JsonException($"Value {key} is not valid for this element.");
|
||||
}
|
||||
|
||||
map = Map.Parse(key);
|
||||
hasMap = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasMap || count < 3 || count > 4)
|
||||
if (!hasMap || count != 3)
|
||||
{
|
||||
throw new JsonException("WorldLocation must be an array of x, y, z, and map");
|
||||
}
|
||||
|
|
@ -131,7 +143,10 @@ namespace Server.Json
|
|||
}
|
||||
|
||||
hasLoc = true;
|
||||
var loc = new Point3DConverter().Read(ref reader, typeof(Point3D), options);
|
||||
|
||||
_point3DConverter ??= new Point3DConverter();
|
||||
|
||||
var loc = _point3DConverter.Read(ref reader, typeof(Point3D), options);
|
||||
data[0] = loc.X;
|
||||
data[1] = loc.Y;
|
||||
data[2] = loc.Z;
|
||||
|
|
@ -139,15 +154,13 @@ namespace Server.Json
|
|||
continue;
|
||||
}
|
||||
|
||||
map = reader.TokenType switch
|
||||
{
|
||||
JsonTokenType.String => Map.Parse(reader.GetString()),
|
||||
JsonTokenType.Number => Map.Maps[reader.GetInt32()],
|
||||
_ => throw new JsonException($"Value for {key} must be a number or string")
|
||||
};
|
||||
_mapConverter ??= new MapConverter();
|
||||
map = _mapConverter.Read(ref reader, typeof(Map), options);
|
||||
|
||||
hasMap = true;
|
||||
}
|
||||
|
||||
if (!hasMap || count < 2)
|
||||
if (!hasMap || count != 3)
|
||||
{
|
||||
throw new JsonException("WorldLocation must have an x, y, z, and map properties");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,10 +30,10 @@ namespace Server.Json
|
|||
// In the future this should be optimized by cloning DefaultOptions
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
ReadCommentHandling = JsonCommentHandling.Skip,
|
||||
WriteIndented = true,
|
||||
AllowTrailingCommas = true,
|
||||
IgnoreNullValues = true
|
||||
IgnoreNullValues = true,
|
||||
ReadCommentHandling = JsonCommentHandling.Skip
|
||||
};
|
||||
|
||||
options.Converters.Add(new MapConverterFactory());
|
||||
|
|
@ -43,6 +43,7 @@ namespace Server.Json
|
|||
options.Converters.Add(new IPEndPointConverterFactory());
|
||||
options.Converters.Add(new NullableStructSerializerFactory());
|
||||
options.Converters.Add(new TypeConverterFactory());
|
||||
options.Converters.Add(new WorldLocationConverterFactory());
|
||||
|
||||
for (var i = 0; i < converters.Length; i++)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue