CSharp · Console
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Text.Json.Nodes;
using NUnit.Framework; // Throws NUnit.Framework.AssertionException
//#nullable enable
// https://stackoverflow.com/questions/78402874/system-text-json-utf8jsonreader-is-reading-the-array-values-twice
public class MapDTO
{
// https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/immutability
// Important: System.Text.Json requires that the constructor parameter names are the same as the corresponding property names, ignoring differences in case.
[JsonConstructor]
public MapDTO(string mapname) => this.Mapname = mapname;
public MapDTO() : this("") { }
[JsonPropertyName("Map name")]
public string Mapname { get; }
[JsonPropertyName("Entities")]
public List<EntityDTO> Entities { get; set; }
[JsonPropertyName("Entity components")]
public List<EntityComponentDTO> Entitycomponents { get; set; }
}
public class ComponentDatumDTO
{
[JsonPropertyName("Position")]
public object Position { get; set; } // TODO: update with the correct type
[JsonPropertyName("Rotation")]
public object Rotation { get; set; } // TODO: update with the correct type
[JsonPropertyName("Scale")]
public object Scale { get; set; } // TODO: update with the correct type
[JsonPropertyName("Entity")]
public string Entity { get; set; }
[JsonPropertyName("GUIHeaderIsOpened")]
public string GUIHeaderIsOpened { get; set; }
[JsonPropertyName("Enabled")]
public string Enabled { get; set; }
[JsonPropertyName("GUID")]
public Guid GUID { get; set; } // Changed from string to Guid
}
public class EntityDTO
{
[JsonPropertyName("Name")]
public string Name { get; set; }
[JsonPropertyName("Active")]
public string Active { get; set; }
[JsonPropertyName("GUID")]
public Guid GUID { get; set; } // Changed from string to Guid
}
public class EntityComponentDTO
{
[JsonPropertyName("Component type")]
public string Componenttype { get; set; }
[JsonPropertyName("Component data")]
public List<ComponentDatumDTO> Componentdata { get; set; }
}
#nullable enable
class TestClass
{
static JsonSerializerOptions DefaultOptions { get; } = new() { AllowTrailingCommas = true, ReadCommentHandling = JsonCommentHandling.Skip };
public MapDTO? ReadYamlFromFile(string filePath)
{
using (var stream = File.OpenRead(filePath))
return JsonSerializer.Deserialize<MapDTO>(stream, DefaultOptions);
}
public MapDTO? ReadYaml(string json) =>
JsonSerializer.Deserialize<MapDTO>(json, DefaultOptions);
public void Test()
{
TestFromString(GetJson());
}
public void TestFromString(string json)
{
var map = ReadYaml(json);
//map.Dump();
Console.WriteLine("Deserialized and re-serialized {0}:", map);
var newJson = JsonSerializer.Serialize(map, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(newJson);
Assert.That(JsonNode.DeepEquals(JsonNode.Parse(json), JsonNode.Parse(newJson)));
}
static string GetJson() =>
"""
{
"Map name": "NewMap",
"Entities": [
{
"Name": "Entity",
"Active": "True",
"GUID": "f06ed7d3-f1b3-4cfd-a623-a7ceb17dbced"
},
{
"Name": "Entity #1",
"Active": "True",
"GUID": "03788f46-e430-4882-a097-e860d2c7aed5"
}
],
"Entity components": [
{
"Component type": "ExtremeEngine.Transform",
"Component data": [
{
"Position": null,
"Rotation": null,
"Scale": null,
"Entity": "f06ed7d3-f1b3-4cfd-a623-a7ceb17dbced",
"GUIHeaderIsOpened": "True",
"Enabled": "True",
"GUID": "95dea154-6f00-41b0-bdec-fe78cca589c3"
}
]
},
{
"Component type": "ExtremeEngine.Transform",
"Component data": [
{
"Position": null,
"Rotation": null,
"Scale": null,
"Entity": "03788f46-e430-4882-a097-e860d2c7aed5",
"GUIHeaderIsOpened": "True",
"Enabled": "True",
"GUID": "d92b131a-a82f-487f-b1c1-25ff13496284"
}
]
}
]
}
""";
}
public class Program
{
public static void Main()
{
Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("System.Text.Json version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine();
try
{
new TestClass().Test();
}
catch (Exception ex)
{
Console.WriteLine("Failed with unhandled exception: ");
Console.WriteLine(ex);
throw;
}
}
}