From b15a55afd1b1741a0c723385ea7ae88d05d47ebd Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Tue, 13 Jul 2021 11:00:19 -0400 Subject: [PATCH] Newtonsoft.Json -> System.Text.Json for Sprites.cs --- Shared/Sprites.cs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/Shared/Sprites.cs b/Shared/Sprites.cs index 04b0f29..f59605c 100644 --- a/Shared/Sprites.cs +++ b/Shared/Sprites.cs @@ -1,7 +1,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; -using Newtonsoft.Json.Linq; using System.Collections.Generic; +using System.Text.Json; namespace SemiColinGames { public static class Sprites { @@ -61,29 +61,32 @@ namespace SemiColinGames { Texture = texture; animations = new Dictionary(); - JObject json = JObject.Parse(metadataJson); + JsonElement jsonRoot = JsonDocument.Parse(metadataJson).RootElement; frames = new List(); - foreach (JToken child in json.SelectToken("frames").Children()) { + foreach (JsonElement child in jsonRoot.GetProperty("frames").EnumerateArray()) { + JsonElement frame = child.GetProperty("frame"); Rectangle source = new Rectangle( - child.SelectToken("frame.x").Value(), - child.SelectToken("frame.y").Value(), - child.SelectToken("frame.w").Value(), - child.SelectToken("frame.h").Value()); - double duration = child.SelectToken("duration").Value() / 1000; + frame.GetProperty("x").GetInt32(), + frame.GetProperty("y").GetInt32(), + frame.GetProperty("w").GetInt32(), + frame.GetProperty("h").GetInt32()); + + double duration = child.GetProperty("duration").GetDouble() / 1000; frames.Add(new Frame(source, duration)); } + // We assume that all frames are the same size (which right now is assured by the // Aseprite-based spritesheet export process). Width = frames[0].Source.Width; Height = frames[0].Source.Height; - JToken frameTags = json.SelectToken("meta.frameTags"); - foreach (JToken child in frameTags.Children()) { - string name = child.SelectToken("name").Value(); - int start = child.SelectToken("from").Value(); - int end = child.SelectToken("to").Value(); - string directionString = child.SelectToken("direction").Value(); + JsonElement frameTags = jsonRoot.GetProperty("meta").GetProperty("frameTags"); + foreach (JsonElement child in frameTags.EnumerateArray()) { + string name = child.GetProperty("name").GetString(); + int start = child.GetProperty("from").GetInt32(); + int end = child.GetProperty("to").GetInt32(); + string directionString = child.GetProperty("direction").GetString(); AnimationDirection direction = directionString == "pingpong" ? AnimationDirection.PingPong : AnimationDirection.Forward; double duration = 0;