Created
June 26, 2023 17:00
-
-
Save thatcosmonaut/ae67450ac9dd2f9eb14f55e4b88a4a15 to your computer and use it in GitHub Desktop.
System.Text.Json Codegen Serialization Example
This file contains hidden or 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
using System.IO; | |
using System.Text.Json; | |
using System.Text.Json.Serialization; | |
namespace SamuraiGunn2.Data | |
{ | |
[JsonSerializable(typeof(CramTextureAtlasData))] | |
internal partial class CramTextureAtlasDataContext : JsonSerializerContext | |
{ | |
} | |
public static class CramAtlasReader | |
{ | |
static JsonSerializerOptions options = new JsonSerializerOptions | |
{ | |
PropertyNameCaseInsensitive = true | |
}; | |
static CramTextureAtlasDataContext context = new CramTextureAtlasDataContext(options); | |
public static TexturePage ReadTextureAtlas(string path) | |
{ | |
var data = (CramTextureAtlasData) JsonSerializer.Deserialize(File.ReadAllText(path), typeof(CramTextureAtlasData), context); | |
return new TexturePage(new CramTextureAtlasFile(new FileInfo(path), data)); | |
} | |
} | |
} |
This file contains hidden or 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
using System.Collections.Generic; | |
using System.IO; | |
namespace SamuraiGunn2.Data | |
{ | |
public struct CramTextureAtlasFile | |
{ | |
public FileInfo File { get; } | |
public CramTextureAtlasData Data { get; } | |
public CramTextureAtlasFile(FileInfo file, CramTextureAtlasData data) | |
{ | |
File = file; | |
Data = data; | |
} | |
} | |
public struct CramTextureAtlasData | |
{ | |
// these are the fields generated by Cram | |
public string Name { get; set; } | |
public int Width { get; set; } | |
public int Height { get; set; } | |
public CramTextureAtlasImageData[] Images { get; set; } | |
// some extra data, just for us | |
public Dictionary<string, CramTextureAtlasAnimationData> Animations { get; set; } | |
} | |
public struct CramTextureAtlasImageData | |
{ | |
public string Name { get; set; } | |
public int X { get; set; } | |
public int Y { get; set; } | |
public int W { get; set; } | |
public int H { get; set; } | |
public int TrimOffsetX { get; set; } | |
public int TrimOffsetY { get; set; } | |
public int UntrimmedWidth { get; set; } | |
public int UntrimmedHeight { get; set; } | |
} | |
public struct CramTextureAtlasAnimationData | |
{ | |
public string[] Frames { get; set; } | |
public int FrameRate { get; set; } | |
public int XOrigin { get; set; } | |
public int YOrigin { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment