private static void WriterSystemTextJsonBasicUtf8(bool formatted, ArrayFormatter output, int[] data) { var json = new JsonWriterUtf8(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(); // Add a large array of values json.WriteArrayStart("ExtraArray"); for (var i = 0; i < ExtraArraySize; i++) { json.WriteValue(data[i]); } json.WriteArrayEnd(); json.WriteObjectEnd(); }
static void WriteResponseForPostJson(HttpRequest request, ReadOnlySequence <byte> body, TcpConnectionFormatter response) { // read request json int requestedCount; // TODO: this should not convert to span var dom = JsonObject.Parse(body.First.Span); 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 JsonWriterUtf8(response, prettyPrint: false); jsonWriter.WriteObjectStart(); jsonWriter.WriteArrayStart("values"); for (int i = 0; i < requestedCount; i++) { jsonWriter.WriteValue("hello!"); } jsonWriter.WriteArrayEnd(); jsonWriter.WriteObjectEnd(); }
static void Write(ref JsonWriterUtf8 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.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(); }
public void WriteBasicJsonUtf8(bool prettyPrint) { int[] data = GetData(ExtraArraySize, 42, -10000, 10000); string expectedStr = GetExpectedString(prettyPrint, isUtf8: true, data); var output = new ArrayFormatter(1024, SymbolTable.InvariantUtf8); var jsonUtf8 = new JsonWriterUtf8(output, prettyPrint); jsonUtf8.WriteObjectStart(); jsonUtf8.WriteAttribute("age", 42); jsonUtf8.WriteAttribute("first", "John"); 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.WriteArrayStart("ExtraArray"); for (var i = 0; i < ExtraArraySize; i++) { jsonUtf8.WriteValue(data[i]); } jsonUtf8.WriteArrayEnd(); jsonUtf8.WriteObjectEnd(); ArraySegment <byte> formatted = output.Formatted; string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expectedStr, actualStr); }
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 JsonWriterUtf8(response, prettyPrint: false); jsonWriter.WriteObjectStart(); jsonWriter.WriteArrayStart("values"); for (int i = 0; i < 5; i++) { jsonWriter.WriteValue("hello!"); } jsonWriter.WriteArrayEnd(); jsonWriter.WriteObjectEnd(); }