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();
        }
Esempio n. 2
0
        static void WriteResponseForPostJson(HttpRequest request, ReadOnlySequence <byte> body, TcpConnectionFormatter response)
        {
            // read request json
            int requestedCount;

            // TODO: this should not convert to span
            var        parser = new JsonParser(body.First.Span);
            JsonObject dom    = parser.Parse();

            try
            {
                requestedCount = (int)dom["Count"];
            }
            finally
            {
                dom.Dispose();
            }

            WriteCommonHeaders(ref response, Http.Version.Http11, 200, "OK");
            response.Append("Content-Type : application/json; charset=UTF-8\r\n");
            response.AppendEoh();

            // write response JSON
            var jsonWriter = new Utf8JsonWriter <TcpConnectionFormatter>(response, prettyPrint: false);

            jsonWriter.WriteObjectStart();
            jsonWriter.WriteArrayStart("values");
            for (int i = 0; i < requestedCount; i++)
            {
                jsonWriter.WriteValue("hello!");
            }
            jsonWriter.WriteArrayEnd();
            jsonWriter.WriteObjectEnd();
            jsonWriter.Flush();
        }
Esempio n. 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);
        }
        private static void WriterSystemTextJsonArrayOnlyUtf8(bool formatted, ArrayFormatterWrapper output, ReadOnlySpan <int> data)
        {
            Utf8JsonWriter <ArrayFormatterWrapper> json = Utf8JsonWriter.Create(output, formatted);

            json.WriteArrayStart("ExtraArray");
            for (var i = 0; i < data.Length; i++)
            {
                json.WriteValue(data[i]);
            }
            json.WriteArrayEnd();
            json.Flush();
        }
 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();
 }
Esempio n. 6
0
        static void WriteResponseForGetJson(HttpRequest request, ReadOnlySequence <byte> body, TcpConnectionFormatter response)
        {
            WriteCommonHeaders(ref response, Http.Version.Http11, 200, "OK");

            response.Append("Content-Type : application/json; charset=UTF-8\r\n");
            response.AppendEoh();

            // write response JSON
            var jsonWriter = new Utf8JsonWriter <TcpConnectionFormatter>(response, prettyPrint: false);

            jsonWriter.WriteObjectStart();
            jsonWriter.WriteArrayStart("values");
            for (int i = 0; i < 5; i++)
            {
                jsonWriter.WriteValue("hello!");
            }
            jsonWriter.WriteArrayEnd();
            jsonWriter.WriteObjectEnd();
            jsonWriter.Flush();
        }
Esempio n. 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();
 }