Beispiel #1
0
        /// <inheritdoc />
        public override void Write(Utf8JsonWriter writer, TPropertyContainer container, JsonSerializerOptions options)
        {
            writer.WriteStartObject();

            if (container != null && container.Properties.Count > 0)
            {
                string GetJsonPropertyName(string name) => options.PropertyNamingPolicy?.ConvertName(name) ?? name;

                if (Options.WriteSchemaCompact)
                {
                    Type containerType = container.GetType();

                    bool writeSchemaCompact = true;

                    if (Options.WriteSchemaOnceForKnownTypes && container is IKnownPropertySet)
                    {
                        JsonWriterContext?jsonWriterContext = writer.AsMetadataProvider().GetMetadata <JsonWriterContext>();
                        if (jsonWriterContext != null)
                        {
                            writeSchemaCompact = !jsonWriterContext.IsWritten(containerType);
                        }
                    }

                    if (writeSchemaCompact)
                    {
                        writer.WritePropertyName("$metadata.schema.compact");
                        string[] compactSchema = MetadataSchema.GenerateCompactSchema(container, GetJsonPropertyName, Options.Separator, Options.TypeMapper);
                        JsonSerializer.Serialize(writer, compactSchema, options);

                        if (Options.WriteSchemaOnceForKnownTypes && container is IKnownPropertySet)
                        {
                            writer.AsMetadataProvider().ConfigureMetadata <JsonWriterContext>(context => context.SetIsWritten(containerType));
                        }
                    }
                }

                foreach (IPropertyValue propertyValue in container.Properties)
                {
                    string jsonPropertyName = GetJsonPropertyName(propertyValue.PropertyUntyped.Name);
                    Type   propertyType     = propertyValue.PropertyUntyped.Type;

                    // PropertyName
                    writer.WritePropertyName(jsonPropertyName);

                    if (Options.WriteArraysInOneRow && propertyType.IsArray && writer.Options.Indented)
                    {
                        // Creates NotIndented writer
                        Utf8JsonWriter writerCopy = writer.CloneNotIndented();

                        // PropertyValue
                        JsonSerializer.Serialize(writerCopy, propertyValue.ValueUntyped, propertyType, options);

                        // Needs to copy internal state back to writer
                        writerCopy.CopyStateTo(writer);
                    }
                    else
                    {
                        // PropertyValue
                        JsonSerializer.Serialize(writer, propertyValue.ValueUntyped, propertyType, options);
                    }
                }
            }

            writer.WriteEndObject();
        }