예제 #1
0
        private static void WriterSystemTextJsonHelloWorldUtf8(bool formatted, ArrayFormatterWrapper output)
        {
            Utf8JsonWriter <ArrayFormatterWrapper> json = Utf8JsonWriter.Create(output, formatted);

            json.WriteObjectStart();
            json.WriteAttribute("message", "Hello, World!");
            json.WriteObjectEnd();
            json.Flush();
        }
예제 #2
0
        private static void WriterSystemTextJsonBasicUtf8(bool formatted, ArrayFormatterWrapper output, ReadOnlySpan <int> data)
        {
            Utf8JsonWriter <ArrayFormatterWrapper> json = Utf8JsonWriter.Create(output, formatted);

            json.WriteObjectStart();
            json.WriteAttribute("age", 42);
            json.WriteAttribute("first", "John");
            json.WriteAttribute("last", "Smith");
            json.WriteArrayStart("phoneNumbers");
            json.WriteValue("425-000-1212");
            json.WriteValue("425-000-1213");
            json.WriteArrayEnd();
            json.WriteObjectStart("address");
            json.WriteAttribute("street", "1 Microsoft Way");
            json.WriteAttribute("city", "Redmond");
            json.WriteAttribute("zip", 98052);
            json.WriteObjectEnd();

            json.WriteArrayStart("ExtraArray");
            for (var i = 0; i < data.Length; i++)
            {
                json.WriteValue(data[i]);
            }
            json.WriteArrayEnd();

            json.WriteObjectEnd();
            json.Flush();
        }
예제 #3
0
        public void WriteBasicJsonUtf8(bool prettyPrint)
        {
            int[]  data        = GetData(ExtraArraySize, 42, -10000, 10000);
            byte[] ExtraArray  = Encoding.UTF8.GetBytes("ExtraArray");
            string expectedStr = GetExpectedString(prettyPrint, isUtf8: true, data);

            var output   = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8);
            var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(output, prettyPrint);

            jsonUtf8.WriteObjectStart();
            jsonUtf8.WriteAttribute("age", 42);
            jsonUtf8.WriteAttribute("first", null);
            jsonUtf8.WriteAttribute("last", "Smith");
            jsonUtf8.WriteArrayStart("phoneNumbers");
            jsonUtf8.WriteValue("425-000-1212");
            jsonUtf8.WriteValue("425-000-1213");
            jsonUtf8.WriteArrayEnd();
            jsonUtf8.WriteObjectStart("address");
            jsonUtf8.WriteAttribute("street", "1 Microsoft Way");
            jsonUtf8.WriteAttribute("city", "Redmond");
            jsonUtf8.WriteAttribute("zip", 98052);
            jsonUtf8.WriteObjectEnd();

            // Add a large array of values
            jsonUtf8.WriteArrayUtf8(ExtraArray, data);

            jsonUtf8.WriteObjectEnd();
            jsonUtf8.Flush();

            ArraySegment <byte> formatted = output.Formatted;
            string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count);

            Assert.Equal(expectedStr, actualStr);
        }
예제 #4
0
 private static void WriteDepth(ref Utf8JsonWriter <ArrayFormatterWrapper> jsonUtf8, int depth)
 {
     jsonUtf8.WriteObjectStart();
     for (int i = 0; i < depth; i++)
     {
         jsonUtf8.WriteObjectStart("message" + i);
     }
     jsonUtf8.WriteAttribute("message" + depth, "Hello, World!");
     for (int i = 0; i < depth; i++)
     {
         jsonUtf8.WriteObjectEnd();
     }
     jsonUtf8.WriteObjectEnd();
     jsonUtf8.Flush();
 }
예제 #5
0
        public void WriteHelloWorldJsonUtf8(bool prettyPrint)
        {
            string expectedStr = GetHelloWorldExpectedString(prettyPrint, isUtf8: true);

            var output   = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8);
            var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(output, prettyPrint);

            jsonUtf8.WriteObjectStart();
            jsonUtf8.WriteAttribute("message", "Hello, World!");
            jsonUtf8.WriteObjectEnd();
            jsonUtf8.Flush();

            ArraySegment <byte> formatted = output.Formatted;
            string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count);

            Assert.Equal(expectedStr, actualStr);
        }
예제 #6
0
        public void TechEmpowerJsonNoIO(int numberOfRequests, int concurrentConnections)
        {
            RawInMemoryHttpServer.Run(numberOfRequests, concurrentConnections, s_genericRequest, (request, response) =>
            {
                var formatter = new BufferWriterFormatter <PipeWriter>(response, SymbolTable.InvariantUtf8);
                formatter.Append("HTTP/1.1 200 OK");
                formatter.Append("\r\nContent-Length: 25");
                formatter.Append("\r\nContent-Type: application/json");
                formatter.Format("\r\nDate: {0:R}", DateTime.UtcNow);
                formatter.Append("Server: System.IO.Pipelines");
                formatter.Append("\r\n\r\n");

                // write body
                var writer = new Utf8JsonWriter <BufferWriterFormatter <PipeWriter> >(formatter);
                writer.WriteObjectStart();
                writer.WriteAttribute("message", "Hello, World!");
                writer.WriteObjectEnd();
                writer.Flush();
            });
        }
예제 #7
0
 static void Write(ref Utf8JsonWriter <ArrayFormatterWrapper> json)
 {
     int[]  values      = { 425121, -425122, 425123 };
     byte[] valueString = Encoding.UTF8.GetBytes("values");
     json.WriteObjectStart();
     json.WriteAttribute("age", 30);
     json.WriteAttribute("first", "John");
     json.WriteAttribute("last", "Smith");
     json.WriteArrayStart("phoneNumbers");
     json.WriteValue("425-000-1212");
     json.WriteValue("425-000-1213");
     json.WriteNull();
     json.WriteArrayEnd();
     json.WriteObjectStart("address");
     json.WriteAttribute("street", "1 Microsoft Way");
     json.WriteAttribute("city", "Redmond");
     json.WriteAttribute("zip", 98052);
     json.WriteObjectEnd();
     json.WriteArrayUtf8(valueString, values);
     json.WriteObjectEnd();
     json.Flush();
 }
예제 #8
0
 static void Write(ref Utf8JsonWriter <ArrayFormatterWrapper> json)
 {
     json.WriteObjectStart();
     json.WriteAttribute("age", 30);
     json.WriteAttribute("first", "John");
     json.WriteAttribute("last", "Smith");
     json.WriteArrayStart("phoneNumbers");
     json.WriteValue("425-000-1212");
     json.WriteValue("425-000-1213");
     json.WriteNull();
     json.WriteArrayEnd();
     json.WriteObjectStart("address");
     json.WriteAttribute("street", "1 Microsoft Way");
     json.WriteAttribute("city", "Redmond");
     json.WriteAttribute("zip", 98052);
     json.WriteObjectEnd();
     json.WriteArrayStart("values");
     json.WriteValue(425121);
     json.WriteValue(-425122);
     json.WriteValue(425123);
     json.WriteArrayEnd();
     json.WriteObjectEnd();
     json.Flush();
 }