tools/Elastic.CommonSchema.Generator/Views/EcsDocumentJsonConverter.Generated.cshtml (118 lines of code) (raw):

@* ReSharper disable once RedundantUsingDirective *@ @using System.Collections.Generic @inherits Elastic.CommonSchema.Generator.Views.CodeTemplatePage<Elastic.CommonSchema.Generator.Projection.CommonSchemaTypesProjection> // Licensed to Elasticsearch B.V under one or more agreements. // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information /* IMPORTANT NOTE ============== This file has been generated. If you wish to submit a PR please modify the original csharp file and submit the PR with that change. Thanks! */ using System; using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; namespace Elastic.CommonSchema.Serialization { public partial class @(Model.Base.Name)JsonConverter@(Raw("<TBase>")) : EcsJsonConverterBase@(Raw("<TBase>")) where TBase : @Model.Base.Name, new() { private static bool ReadProperties( ref Utf8JsonReader reader, TBase ecsEvent, ref DateTimeOffset? timestamp, ref string loglevel, ref string ecsVersion, JsonSerializerOptions options ) { var propertyName = reader.GetString(); reader.Read(); return propertyName switch { "log.level" => ReadString(ref reader, ref loglevel), "ecs.version" => ReadString(ref reader, ref ecsVersion), "metadata" => ReadProp@(Raw("<MetadataDictionary>"))(ref reader, "metadata", ecsEvent, (b, v) => b.Metadata = v, options), @foreach (var property in Model.Base.BaseFieldSet.ValueProperties) { var name = property.JsonProperty; if (name == "@timestamp") {<text> "@(name)" => ReadDateTime(ref reader, ref @(name), options), </text>} else {<text> "@(name)" => ReadProp<@(Raw(property.ClrType))>(ref reader, "@name", ecsEvent, (b, v) => b.@(property.Name) = v, options), </text>} } @foreach (var property in Model.Base.BaseFieldSet.InlineObjectProperties) { var name = property.JsonProperty; <text> "@(name)" => ReadProp<@(Raw(property.InlineObject.Name))>(ref reader, "@name", ecsEvent, (b, v) => b.@(property.Name) = v, options), </text> } @foreach (var entity in Model.EntityClasses) { var entityName = entity.BaseFieldSet.FieldSet.Name; <text> "@entityName" => ReadProp<@(entity.Name)>(ref reader, "@entityName", EcsJsonContext.Default.@(entity.Name), ecsEvent, (b, v) => b.@(entity.Name) = v), </text>} _ => typeof(@(Model.Base.Name)) == ecsEvent.GetType() ? false : ecsEvent.TryRead(propertyName, out var t) ? ecsEvent.ReceiveProperty(propertyName, ReadPropDeserialize(ref reader, t, options)) : false }; } /// <inheritdoc cref="JsonConverter{T}.Write"/> public override void Write(Utf8JsonWriter writer, TBase value, JsonSerializerOptions options) { if (value == null) { writer.WriteNullValue(); return; } writer.WriteStartObject(); WriteTimestamp(writer, value, options); WriteLogLevel(writer, value); WriteMessage(writer, value); WriteEcsVersion(writer, value); WriteLogEntity(writer, value.Log, options); WriteEcsEntity(writer, value.Ecs, options); // Base fields @{ var manualFields = new List<string>() { "@timestamp", "message", "log", "ecs" }; } @foreach (var field in Model.Base.BaseFieldSet.ValueProperties) { if (manualFields.Contains(field.JsonProperty)) { continue; } <text> WriteProp(writer, "@field.JsonProperty", value.@field.Name, options); </text> } @foreach (var property in Model.Base.BaseFieldSet.InlineObjectProperties) { var name = property.JsonProperty; <text> WriteProp(writer, "@(name)", value.@(property.InlineObject.Name), options); </text> } // Complex types @foreach (var entity in Model.EntityClasses) { var entityName = entity.BaseFieldSet.FieldSet.Name; if (manualFields.Contains(entityName)) { continue; } <text> WriteProp(writer, "@(entityName)", value.@(entity.Name), EcsJsonContext.Default.@(entity.Name), options); </text> } WriteProp(writer, "metadata", value.Metadata, options); if (typeof(@Model.Base.Name) != value.GetType()) value.WriteAdditionalProperties((k, v) => WriteProp(writer, k, v, options)); writer.WriteEndObject(); } } }