Ejemplo n.º 1
0
        public static string SertializeJson(Scene scene)
        {
            JsonSerializer serializer = new JsonSerializer(CreateSettings(scene));

            using (MemoryStream stream = new MemoryStream())
                using (System.Text.Json.Utf8JsonWriter writer = new System.Text.Json.Utf8JsonWriter(stream, new System.Text.Json.JsonWriterOptions()
                {
                    Indented = true
                }))
                {
                    serializer.Serialize(writer, scene);
                    writer.Flush();
                    stream.Flush();
                    return(Encoding.Default.GetString(stream.ToArray()));
                }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var m1 = new Winner3Way(
                new Outcome.Priced(1.85m),
                new Outcome.PricedWithProb(3.3m, 0.33f),
                new Outcome.Resulted(OutcomeResult.Canceled));

            Console.WriteLine($"Orig: {m1}");

            using var outputStream = new MemoryStream();
            using var output       = new Google.Protobuf.CodedOutputStream(outputStream);

            m1.FromDomain().WriteTo(output);
            output.Flush();

            var bytes = outputStream.ToArray();

            Console.WriteLine($"Protobuf Bytes Length={bytes.Length}, {BitConverter.ToString(bytes)}");

            using var inputStream = new MemoryStream(bytes);
            using var input       = new Google.Protobuf.CodedInputStream(inputStream);
            var protoCopy = new Domain.Protobuf.Winner3Way();

            protoCopy.MergeFrom(input);

            var copy = protoCopy.ToDomain();

            Console.WriteLine($"Copy: {copy}");

            using var jsonStream = new MemoryStream();
            using var jsonWriter = new System.Text.Json.Utf8JsonWriter(jsonStream);
            m1.WriteJson(jsonWriter);
            jsonWriter.Flush();
            var jsonString = Encoding.UTF8.GetString(jsonStream.ToArray());

            Console.WriteLine($"Json {jsonString}");

            var jsonDoc  = System.Text.Json.JsonDocument.Parse(jsonString);
            var jsonCopy = Json.ReadWinner3Way(jsonDoc.RootElement);

            Console.WriteLine($"Json Copy: {jsonCopy}");

            ComplexDomainDemo.Run();
        }
        private string FormatJson(string json)
        {
            if (json == null)
            {
                return(null);
            }
            var doc = System.Text.Json.JsonDocument.Parse(Symbol.ToJson());

            using (var ms = new System.IO.MemoryStream())
            {
                using (var jw = new System.Text.Json.Utf8JsonWriter(ms, new System.Text.Json.JsonWriterOptions()
                {
                    Indented = true
                }))
                {
                    doc.WriteTo(jw);
                    jw.Flush();
                    return(Encoding.UTF8.GetString(ms.ToArray()));
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// The ping command returns an acknowledgement, so that clients can test the connection status and latency.
        /// </summary>
        public async Task Ping(CancellationToken cancellationToken = default)
        {
            jsonBuffer.Clear();
            jsonWriter.Reset();
            jsonWriter.WriteStartObject();
            var requestId = WriteHeader(jsonWriter, "ping");

            WriteFooter(jsonWriter);
            jsonWriter.WriteEndObject();
            jsonWriter.Flush();
            var _ = await SendReceiveAsync(requestId, jsonBuffer.WrittenMemory, cancellationToken);
        }