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(); }
private static void WriterSystemTextJsonHelloWorldUtf8(bool formatted, ArrayFormatter output) { var json = new JsonWriterUtf8(output, formatted); json.WriteObjectStart(); json.WriteAttribute("message", "Hello, World!"); json.WriteObjectEnd(); }
public static unsafe void WriteRaw4(JsonWriterUtf8 writer, byte[] src) { writer.EnsureCapacity(writer.offset, src.Length); fixed(byte *pSrc = &src[0]) fixed(byte *pDst = &writer.buffer[writer.offset]) { *(int *)(pDst + 0) = *(int *)(pSrc + 0); } writer.offset += src.Length; }
void IJsonFormatterSpecWriter <decimal, JsonWriterUtf8> .SerializeSpec(JsonWriterUtf8 writer, decimal value, IJsonFormatterResolver formatterResolver) { if (serializeAsString) { writer.WriteString(value.ToString(CultureInfo.InvariantCulture)); } else { // write as number format. writer.WriteRaw(StringEncoding.UTF8.GetBytes(value.ToString(CultureInfo.InvariantCulture))); } }
public void WriteHelloWorldJsonUtf8(bool prettyPrint) { string expectedStr = GetHelloWorldExpectedString(prettyPrint, isUtf8: true); var output = new ArrayFormatter(1024, SymbolTable.InvariantUtf8); var jsonUtf8 = new JsonWriterUtf8(output, prettyPrint); jsonUtf8.WriteObjectStart(); jsonUtf8.WriteAttribute("message", "Hello, World!"); jsonUtf8.WriteObjectEnd(); ArraySegment <byte> formatted = output.Formatted; string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expectedStr, actualStr); }
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 JsonWriterUtf8(formatter); writer.WriteObjectStart(); writer.WriteAttribute("message", "Hello, World!"); writer.WriteObjectEnd(); }); }
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(); }
public void WriteJsonUtf8() { var formatter = new ArrayFormatter(1024, SymbolTable.InvariantUtf8); var json = new JsonWriterUtf8(formatter, prettyPrint: false); Write(ref json); var formatted = formatter.Formatted; var str = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expected, str.Replace(" ", "")); formatter.Clear(); json = new JsonWriterUtf8(formatter, prettyPrint: true); Write(ref json); formatted = formatter.Formatted; str = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expected, str.Replace("\r\n", "").Replace("\n", "").Replace(" ", "")); }
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); }
public void SerializeSpec(JsonWriterUtf8 writer, DateTime value, IJsonFormatterResolver formatterResolver) { throw new NotImplementedException(); }