Skip to content

Commit

Permalink
fix properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Ragath authored and Ragath committed Jan 15, 2023
1 parent 75daee1 commit 97fb296
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 13 deletions.
2 changes: 2 additions & 0 deletions TiledLib.Tests/ParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ public void TestJsonParsing(string file)
var result = JsonSerializer.Deserialize<Map>(mapStream, new JsonSerializerOptions(JsonSerializerDefaults.Web));

Assert.IsNotNull(result);
Assert.IsNotNull(result.Layers);
Assert.IsTrue(result.Layers.Any());
}
}
17 changes: 17 additions & 0 deletions TiledLib/JsonStringConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace TiledLib;

public class JsonStringConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.TokenType switch
{
JsonTokenType.Number => reader.GetDecimal().ToString(),
JsonTokenType.String => reader.GetString(),
JsonTokenType.True or JsonTokenType.False => reader.GetBoolean().ToString(),
_ => reader.GetString(),
};
}

public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) => throw new NotImplementedException();
}
2 changes: 1 addition & 1 deletion TiledLib/Layer/BaseLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ public abstract class BaseLayer

[JsonPropertyName("properties")]
[JsonConverter(typeof(PropertiesConverter))]
public Dictionary<string, string> Properties { get; } = new Dictionary<string, string>();
public Dictionary<string, string> Properties { get; init; } = new Dictionary<string, string>();
}
2 changes: 1 addition & 1 deletion TiledLib/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class Map : IXmlSerializable

[JsonPropertyName("properties")]
[JsonConverter(typeof(PropertiesConverter))]
public Dictionary<string, string> Properties { get; } = new Dictionary<string, string>();
public Dictionary<string, string> Properties { get; init; } = new Dictionary<string, string>();

/// <summary>
/// Parses map from JSON stream
Expand Down
2 changes: 1 addition & 1 deletion TiledLib/Objects/BaseObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public abstract class BaseObject

[JsonPropertyName("properties")]
[JsonConverter(typeof(PropertiesConverter))]
public Dictionary<string, string> Properties { get; } = new Dictionary<string, string>();
public Dictionary<string, string> Properties { get; init; } = new Dictionary<string, string>();

public BaseObject(Dictionary<string, string> properties) { Properties = properties; }
}
2 changes: 1 addition & 1 deletion TiledLib/Objects/ObjectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var a when a.TryGetProperty("gid", out var _) => a.Deserialize<TileObject>(optio
var a when a.TryGetProperty("polygon", out var _) => a.Deserialize<PolygonObject>(options),
var a when a.TryGetProperty("polyline", out var _) => a.Deserialize<PolyLineObject>(options),
var a when a.TryGetProperty("ellipse", out var _) => a.Deserialize<EllipseObject>(options),
_ => jo.Deserialize<RectangleObject>()
_ => jo.Deserialize<RectangleObject>(options)
};
}

Expand Down
17 changes: 11 additions & 6 deletions TiledLib/PropertiesConverter.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@


namespace TiledLib;
namespace TiledLib;

/// <summary>
/// Supports legacy property syntax
/// </summary>
public class PropertiesConverter : JsonConverter<Dictionary<string, string>>
{
protected class Property
{
public string Name { get; set; }
public string Type { get; set; }
[JsonConverter(typeof(JsonStringConverter))]
public string Value { get; set; }
}

public override Dictionary<string, string> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.StartArray)
return JsonSerializer.Deserialize<Property[]>(ref reader).ToDictionary(key => key.Name, value => value.Value);
{
var result = JsonSerializer.Deserialize<Property[]>(ref reader, options).ToDictionary(key => key.Name, value => value.Value.ToString());
return result;
}
else
return JsonSerializer.Deserialize<Dictionary<string, string>>(ref reader);
return JsonSerializer.Deserialize<Dictionary<string, string>>(ref reader, options);
}

public override void Write(Utf8JsonWriter writer, Dictionary<string, string> value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
}
6 changes: 3 additions & 3 deletions TiledLib/Tileset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public class Tileset : ITileset, IXmlSerializable

[JsonPropertyName("properties")]
[JsonConverter(typeof(PropertiesConverter))]
public Dictionary<string, string> Properties { get; } = new Dictionary<string, string>();
public Dictionary<string, string> Properties { get; init; } = new Dictionary<string, string>();
[JsonPropertyName("tileproperties")]
public Dictionary<int, Dictionary<string, string>> TileProperties { get; } = new Dictionary<int, Dictionary<string, string>>();
public Dictionary<int, Dictionary<string, string>> TileProperties { get; init; } = new Dictionary<int, Dictionary<string, string>>();



[JsonIgnore] //TODO: Add json support
public Dictionary<int, Frame[]> TileAnimations { get; } = new Dictionary<int, Frame[]>();
public Dictionary<int, Frame[]> TileAnimations { get; init; } = new Dictionary<int, Frame[]>();

public Tile this[int gid]
{
Expand Down

0 comments on commit 97fb296

Please sign in to comment.