Пример #1
0
        private static void WriterSystemTextJsonArrayOnlyUtf8(bool formatted, ArrayFormatterWrapper output, ReadOnlySpan <int> data)
        {
            Utf8JsonWriter <ArrayFormatterWrapper> json = Utf8JsonWriter.Create(output, formatted);

            json.WriteArrayUtf8(ExtraArray, data);
            json.Flush();
        }
Пример #2
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);
        }
Пример #3
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.WriteArrayUtf8(ExtraArray, data);
            json.WriteObjectEnd();
            json.Flush();
        }
Пример #4
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();
 }