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 Map(string mapname) => this.Mapname = mapname;
public Map() : this("") { }
[JsonPropertyName("Map name")]
public string Mapname { get; }
[JsonPropertyName("Entities")]
public List<Entity> Entities { get; set; }
[JsonPropertyName("Entity components")]
public List<EntityComponent> Entitycomponents { get; set; }
public class ComponentDatum
[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 string GUID { get; set; }
[JsonPropertyName("Name")]
public string Name { get; set; }
[JsonPropertyName("Active")]
public string Active { get; set; }
[JsonPropertyName("GUID")]
public string GUID { get; set; }
public class EntityComponent
[JsonPropertyName("Component type")]
public string Componenttype { get; set; }
[JsonPropertyName("Component data")]
public List<ComponentDatum> Componentdata { get; set; }
static JsonSerializerOptions DefaultOptions { get; } = new() { AllowTrailingCommas = true, ReadCommentHandling = JsonCommentHandling.Skip };
public Map? ReadYamlFromFile(string filePath)
using (var stream = File.OpenRead(filePath))
return JsonSerializer.Deserialize<Map>(stream, DefaultOptions);
public Map? ReadYaml(string json)
JsonReaderOptions options = new() { AllowTrailingCommas = true, CommentHandling = JsonCommentHandling.Skip };
Utf8JsonReader reader = new(System.Text.Encoding.UTF8.GetBytes(json), options);
switch (reader.TokenType)
case JsonTokenType.PropertyName:
if (reader.ValueTextEquals("Map name"))
map = new(reader.GetString());
if (reader.ValueTextEquals("Entities"))
if (reader.TokenType == JsonTokenType.StartArray)
if (reader.TokenType == JsonTokenType.EndArray)
if (reader.TokenType == JsonTokenType.StartObject)
if (reader.TokenType == JsonTokenType.EndObject)
if (reader.TokenType == JsonTokenType.PropertyName)
if (reader.TokenType == JsonTokenType.String)
var str = reader.GetString();
TestFromString(GetJson());
public void TestFromString(string json)
var map = ReadYaml(json);
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: ");
public static class Debug
public static void Log(string s) => Console.WriteLine(s);