|  | 
|  | 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. | 
|  | 2 | +// Licensed under the MIT License. | 
|  | 3 | + | 
|  | 4 | +using System; | 
|  | 5 | +using System.ClientModel.Primitives; | 
|  | 6 | +using System.IO; | 
|  | 7 | +using System.Text.Json; | 
|  | 8 | +using Azure.Core.Serialization; | 
|  | 9 | + | 
|  | 10 | +namespace Azure.Core.TestFramework | 
|  | 11 | +{ | 
|  | 12 | +    public class MockJsonModel : IJsonModel<MockJsonModel> | 
|  | 13 | +    { | 
|  | 14 | +        internal MockJsonModel() | 
|  | 15 | +        { | 
|  | 16 | +        } | 
|  | 17 | + | 
|  | 18 | +        public int IntValue { get; set; } | 
|  | 19 | + | 
|  | 20 | +        public string StringValue { get; set; } | 
|  | 21 | + | 
|  | 22 | +        public byte[] Utf8BytesValue { get; } | 
|  | 23 | + | 
|  | 24 | +        public MockJsonModel(int intValue, string stringValue) | 
|  | 25 | +        { | 
|  | 26 | +            IntValue = intValue; | 
|  | 27 | +            StringValue = stringValue; | 
|  | 28 | + | 
|  | 29 | +            dynamic json = BinaryData.FromString("{}").ToDynamicFromJson(JsonPropertyNames.CamelCase); | 
|  | 30 | +            json.IntValue = IntValue; | 
|  | 31 | +            json.StringValue = StringValue; | 
|  | 32 | + | 
|  | 33 | +            MemoryStream stream = new(); | 
|  | 34 | +            using Utf8JsonWriter writer = new Utf8JsonWriter(stream); | 
|  | 35 | + | 
|  | 36 | +            writer.WriteStartObject(); | 
|  | 37 | +            writer.WriteNumber("IntValue", IntValue); | 
|  | 38 | +            writer.WriteString("StringValue", StringValue); | 
|  | 39 | +            writer.WriteEndObject(); | 
|  | 40 | + | 
|  | 41 | +            writer.Flush(); | 
|  | 42 | +            Utf8BytesValue = stream.ToArray(); | 
|  | 43 | +        } | 
|  | 44 | + | 
|  | 45 | +        MockJsonModel IPersistableModel<MockJsonModel>.Create(BinaryData data, ModelReaderWriterOptions options) | 
|  | 46 | +        { | 
|  | 47 | +            dynamic json = data.ToDynamicFromJson(JsonPropertyNames.CamelCase); | 
|  | 48 | +            return new MockJsonModel(json.IntValue, json.StringValue); | 
|  | 49 | +        } | 
|  | 50 | + | 
|  | 51 | +        MockJsonModel IJsonModel<MockJsonModel>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) | 
|  | 52 | +        { | 
|  | 53 | +            using JsonDocument doc = JsonDocument.ParseValue(ref reader); | 
|  | 54 | +            int intValue = doc.RootElement.GetProperty("IntValue").GetInt32(); | 
|  | 55 | +            string stringValue = doc.RootElement.GetProperty("StringValue").GetString()!; | 
|  | 56 | +            return new MockJsonModel(intValue, stringValue); | 
|  | 57 | +        } | 
|  | 58 | + | 
|  | 59 | +        string IPersistableModel<MockJsonModel>.GetFormatFromOptions(ModelReaderWriterOptions options) | 
|  | 60 | +            => "J"; | 
|  | 61 | + | 
|  | 62 | +        BinaryData IPersistableModel<MockJsonModel>.Write(ModelReaderWriterOptions options) | 
|  | 63 | +        { | 
|  | 64 | +            return BinaryData.FromBytes(Utf8BytesValue); | 
|  | 65 | +        } | 
|  | 66 | + | 
|  | 67 | +        void IJsonModel<MockJsonModel>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) | 
|  | 68 | +        { | 
|  | 69 | +            writer.WriteStartObject(); | 
|  | 70 | +            writer.WriteNumber("IntValue", IntValue); | 
|  | 71 | +            writer.WriteString("StringValue", StringValue); | 
|  | 72 | +            writer.WriteEndObject(); | 
|  | 73 | +        } | 
|  | 74 | +    } | 
|  | 75 | +} | 
0 commit comments