using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Text.Json.Nodes;
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; }
[JsonPropertyName("Rotation")]
public object Rotation { get; set; }
[JsonPropertyName("Scale")]
public object Scale { get; set; }
[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; }
[JsonPropertyName("Name")]
public string Name { get; set; }
[JsonPropertyName("Active")]
public string Active { get; set; }
[JsonPropertyName("GUID")]
public Guid GUID { get; set; }
public class EntityComponentDTO
[JsonPropertyName("Component type")]
public string Componenttype { get; set; }
[JsonPropertyName("Component data")]
public List<ComponentDatumDTO> Componentdata { get; set; }
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);
TestFromString(GetJson());
public void TestFromString(string json)
var map = ReadYaml(json);
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() =>
"GUID": "f06ed7d3-f1b3-4cfd-a623-a7ceb17dbced"
"GUID": "03788f46-e430-4882-a097-e860d2c7aed5"
"Component type": "ExtremeEngine.Transform",
"Entity": "f06ed7d3-f1b3-4cfd-a623-a7ceb17dbced",
"GUIHeaderIsOpened": "True",
"GUID": "95dea154-6f00-41b0-bdec-fe78cca589c3"
"Component type": "ExtremeEngine.Transform",
"Entity": "03788f46-e430-4882-a097-e860d2c7aed5",
"GUIHeaderIsOpened": "True",
"GUID": "d92b131a-a82f-487f-b1c1-25ff13496284"
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("Failed with unhandled exception: ");