|  | 
|  | 1 | +// Licensed to the .NET Foundation under one or more agreements. | 
|  | 2 | +// The .NET Foundation licenses this file to you under the MIT license. | 
|  | 3 | + | 
|  | 4 | +using System.Collections; | 
|  | 5 | +using System.Text; | 
|  | 6 | +using System.Text.Json; | 
|  | 7 | + | 
|  | 8 | +namespace Microsoft.EntityFrameworkCore.Query.Internal; | 
|  | 9 | + | 
|  | 10 | +/// <summary> | 
|  | 11 | +///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to | 
|  | 12 | +///     the same compatibility standards as public APIs. It may be changed or removed without notice in | 
|  | 13 | +///     any release. You should only use it directly in your code with extreme caution and knowing that | 
|  | 14 | +///     doing so can result in application failures when updating to a new Entity Framework Core release. | 
|  | 15 | +/// </summary> | 
|  | 16 | +public static class RelationalJsonUtilities | 
|  | 17 | +{ | 
|  | 18 | +    /// <summary> | 
|  | 19 | +    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to | 
|  | 20 | +    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in | 
|  | 21 | +    ///     any release. You should only use it directly in your code with extreme caution and knowing that | 
|  | 22 | +    ///     doing so can result in application failures when updating to a new Entity Framework Core release. | 
|  | 23 | +    /// </summary> | 
|  | 24 | +    public static readonly MethodInfo SerializeComplexTypeToJsonMethod = | 
|  | 25 | +        typeof(RelationalJsonUtilities).GetTypeInfo().GetDeclaredMethod(nameof(SerializeComplexTypeToJson))!; | 
|  | 26 | + | 
|  | 27 | +    /// <summary> | 
|  | 28 | +    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to | 
|  | 29 | +    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in | 
|  | 30 | +    ///     any release. You should only use it directly in your code with extreme caution and knowing that | 
|  | 31 | +    ///     doing so can result in application failures when updating to a new Entity Framework Core release. | 
|  | 32 | +    /// </summary> | 
|  | 33 | +    public static string? SerializeComplexTypeToJson(IComplexType complexType, object? value, bool collection) | 
|  | 34 | +    { | 
|  | 35 | +        // Note that we treat toplevel null differently: we return a relational NULL for that case. For nested nulls, | 
|  | 36 | +        // we return JSON null string (so you get { "foo": null }) | 
|  | 37 | +        if (value is null) | 
|  | 38 | +        { | 
|  | 39 | +            return null; | 
|  | 40 | +        } | 
|  | 41 | + | 
|  | 42 | +        var stream = new MemoryStream(); | 
|  | 43 | +        var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = false }); | 
|  | 44 | + | 
|  | 45 | +        WriteJson(writer, complexType, value, collection); | 
|  | 46 | + | 
|  | 47 | +        writer.Flush(); | 
|  | 48 | + | 
|  | 49 | +        return Encoding.UTF8.GetString(stream.ToArray()); | 
|  | 50 | + | 
|  | 51 | +        void WriteJson(Utf8JsonWriter writer, IComplexType complexType, object? value, bool collection) | 
|  | 52 | +        { | 
|  | 53 | +            if (collection) | 
|  | 54 | +            { | 
|  | 55 | +                if (value is null) | 
|  | 56 | +                { | 
|  | 57 | +                    writer.WriteNullValue(); | 
|  | 58 | + | 
|  | 59 | +                    return; | 
|  | 60 | +                } | 
|  | 61 | + | 
|  | 62 | +                writer.WriteStartArray(); | 
|  | 63 | + | 
|  | 64 | +                foreach (var element in (IEnumerable)value) | 
|  | 65 | +                { | 
|  | 66 | +                    WriteJsonObject(writer, complexType, element); | 
|  | 67 | +                } | 
|  | 68 | + | 
|  | 69 | +                writer.WriteEndArray(); | 
|  | 70 | +                return; | 
|  | 71 | +            } | 
|  | 72 | + | 
|  | 73 | +            WriteJsonObject(writer, complexType, value); | 
|  | 74 | +        } | 
|  | 75 | + | 
|  | 76 | +        void WriteJsonObject(Utf8JsonWriter writer, IComplexType complexType, object? objectValue) | 
|  | 77 | +        { | 
|  | 78 | +            if (objectValue is null) | 
|  | 79 | +            { | 
|  | 80 | +                writer.WriteNullValue(); | 
|  | 81 | +                return; | 
|  | 82 | +            } | 
|  | 83 | + | 
|  | 84 | +            writer.WriteStartObject(); | 
|  | 85 | + | 
|  | 86 | +            foreach (var property in complexType.GetProperties()) | 
|  | 87 | +            { | 
|  | 88 | +                var jsonPropertyName = property.GetJsonPropertyName(); | 
|  | 89 | +                Check.DebugAssert(jsonPropertyName is not null); | 
|  | 90 | +                writer.WritePropertyName(jsonPropertyName); | 
|  | 91 | + | 
|  | 92 | +                var propertyValue = property.GetGetter().GetClrValue(objectValue); | 
|  | 93 | +                if (propertyValue is null) | 
|  | 94 | +                { | 
|  | 95 | +                    writer.WriteNullValue(); | 
|  | 96 | +                } | 
|  | 97 | +                else | 
|  | 98 | +                { | 
|  | 99 | +                    var jsonValueReaderWriter = property.GetJsonValueReaderWriter() ?? property.GetTypeMapping().JsonValueReaderWriter; | 
|  | 100 | +                    Check.DebugAssert(jsonValueReaderWriter is not null, "Missing JsonValueReaderWriter on JSON property"); | 
|  | 101 | +                    jsonValueReaderWriter.ToJson(writer, propertyValue); | 
|  | 102 | +                } | 
|  | 103 | +            } | 
|  | 104 | + | 
|  | 105 | +            foreach (var complexProperty in complexType.GetComplexProperties()) | 
|  | 106 | +            { | 
|  | 107 | +                var jsonPropertyName = complexProperty.GetJsonPropertyName(); | 
|  | 108 | +                Check.DebugAssert(jsonPropertyName is not null); | 
|  | 109 | +                writer.WritePropertyName(jsonPropertyName); | 
|  | 110 | + | 
|  | 111 | +                var propertyValue = complexProperty.GetGetter().GetClrValue(objectValue); | 
|  | 112 | + | 
|  | 113 | +                WriteJson(writer, complexProperty.ComplexType, propertyValue, complexProperty.IsCollection); | 
|  | 114 | +            } | 
|  | 115 | + | 
|  | 116 | +            writer.WriteEndObject(); | 
|  | 117 | +        } | 
|  | 118 | +    } | 
|  | 119 | +} | 
0 commit comments