// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; using EpicGames.UHT.Types; namespace EpicGames.UHT.Utils { /// /// JSON converter to output the meta data /// public class UhtMetaDataConverter : JsonConverter { /// /// Read the JSON value /// /// Source reader /// Type to convert /// Serialization options /// Read value /// public override UhtMetaData Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException(); } /// /// Write the JSON value /// /// Destination writer /// Value being written /// Serialization options public override void Write(Utf8JsonWriter writer, UhtMetaData type, JsonSerializerOptions options) { writer.WriteStartArray(); foreach (KeyValuePair kvp in type.GetSorted()) { writer.WriteStartObject(); writer.WriteString("Key", kvp.Key); writer.WriteString("Value", kvp.Value); writer.WriteEndObject(); } writer.WriteEndArray(); } } /// /// JSON converter to output the source name of a type /// /// public class UhtTypeSourceNameJsonConverter : JsonConverter where T : UhtType { /// /// Read the JSON value /// /// Source reader /// Type to convert /// Serialization options /// Read value /// public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException(); } /// /// Write the JSON value /// /// Destination writer /// Value being written /// Serialization options public override void Write(Utf8JsonWriter writer, T type, JsonSerializerOptions options) { writer.WriteStringValue(type.SourceName); } } /// /// Read/Write type source name for an optional type value /// /// public class UhtNullableTypeSourceNameJsonConverter : JsonConverter where T : UhtType { /// /// Read the JSON value /// /// Source reader /// Type to convert /// Serialization options /// Read value /// public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException(); } /// /// Write the JSON value /// /// Destination writer /// Value being written /// Serialization options public override void Write(Utf8JsonWriter writer, T? type, JsonSerializerOptions options) { if (type != null) { writer.WriteStringValue(type.SourceName); } } } /// /// Read/Write type list source name for an optional type value /// /// public class UhtTypeReadOnlyListSourceNameJsonConverter : JsonConverter> where T : UhtType { /// /// Read the JSON value /// /// Source reader /// Type to convert /// Serialization options /// Read value /// public override IReadOnlyList Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException(); } /// /// Write the JSON value /// /// Destination writer /// Value being written /// Serialization options public override void Write(Utf8JsonWriter writer, IReadOnlyList collection, JsonSerializerOptions options) { writer.WriteStartArray(); foreach (UhtType type in collection) { writer.WriteStringValue(type.SourceName); } writer.WriteEndArray(); } } /// /// Serialize a list of types /// /// public class UhtTypeListJsonConverter : JsonConverter> where T : UhtType { /// /// Read the JSON value /// /// Source reader /// Type to convert /// Serialization options /// Read value /// public override List Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException(); } /// /// Write the JSON value /// /// Destination writer /// Value being written /// Serialization options public override void Write(Utf8JsonWriter writer, List collection, JsonSerializerOptions options) { writer.WriteStartArray(); foreach (UhtType type in collection) { JsonSerializer.Serialize(writer, type, type.GetType(), options); } writer.WriteEndArray(); } } /// /// Serialize a nullable list of types /// /// public class UhtNullableTypeListJsonConverter : JsonConverter> where T : UhtType { /// /// Read the JSON value /// /// Source reader /// Type to convert /// Serialization options /// Read value /// public override List Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException(); } /// /// Write the JSON value /// /// Destination writer /// Value being written /// Serialization options public override void Write(Utf8JsonWriter writer, List? collection, JsonSerializerOptions options) { writer.WriteStartArray(); if (collection != null) { foreach (UhtType type in collection) { JsonSerializer.Serialize(writer, type, type.GetType(), options); } } writer.WriteEndArray(); } } }