/// <summary>
        /// Clones <see cref="Utf8JsonWriter"/> with new <see cref="JsonWriterOptions"/>.
        /// </summary>
        /// <param name="writer">Source writer.</param>
        /// <param name="newOptions">Options to use in new writer.</param>
        /// <returns>New copy of <see cref="Utf8JsonWriter"/> with new options.</returns>
        public static Utf8JsonWriter Clone(this Utf8JsonWriter writer, JsonWriterOptions newOptions)
        {
            AssertReflectionStateIsValid();

            Utf8JsonWriter writerCopy;

            // Get internal output to use in new writer
            IBufferWriter <byte>?output = (IBufferWriter <byte>?)_reflectionCache.OutputField.GetValue(writer);

            if (output != null)
            {
                // Create copy
                writerCopy = new Utf8JsonWriter(output, newOptions);
            }
            else
            {
                // Get internal stream to use in new writer
                Stream?stream = (Stream?)_reflectionCache.StreamField.GetValue(writer);

                // Create copy
                writerCopy = new Utf8JsonWriter(stream, newOptions);
            }

            // Copy internal state
            writer.CopyStateTo(writerCopy);

            return(writerCopy);
        }
예제 #2
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();
        }