Beispiel #1
0
        private void WriteDocumentsBlittable(DocumentsOperationContext context, IEnumerable <Document> documentsToWrite, List <Document> includes)
        {
            HttpContext.Response.Headers["Content-Type"] = "binary/blittable-json";


            using (var streamBuffer = new UnmanagedStreamBuffer(context, ResponseBodyStream()))
                using (var writer = new ManualBlittalbeJsonDocumentBuilder <UnmanagedStreamBuffer>(context,
                                                                                                   null, new BlittableWriter <UnmanagedStreamBuffer>(context, streamBuffer)))
                {
                    writer.StartWriteObjectDocument();

                    writer.StartWriteObject();
                    writer.WritePropertyName("Results");

                    writer.StartWriteArray();

                    foreach (var document in documentsToWrite)
                    {
                        writer.WriteEmbeddedBlittableDocument(document.Data);
                    }

                    writer.WriteArrayEnd();

                    writer.WritePropertyName("Includes");

                    writer.StartWriteArray();

                    foreach (var include in includes)
                    {
                        writer.WriteEmbeddedBlittableDocument(include.Data);
                    }

                    writer.WriteArrayEnd();

                    writer.WriteObjectEnd();

                    writer.FinalizeDocument();
                }
        }
        public unsafe void ReadDataTypesTest()
        {
            using (var context = new JsonOperationContext(1024, 1024 * 4))
            {
                BlittableJsonReaderObject embeddedReader;
                using (var builder = new ManualBlittalbeJsonDocumentBuilder <UnmanagedWriteBuffer>(context))
                {
                    builder.Reset(BlittableJsonDocumentBuilder.UsageMode.None);
                    builder.StartWriteObjectDocument();
                    builder.StartWriteObject();
                    builder.WritePropertyName("Value");
                    builder.WriteValue(1000);
                    builder.WriteObjectEnd();
                    builder.FinalizeDocument();
                    embeddedReader = builder.CreateReader();
                }

                using (var builder = new ManualBlittalbeJsonDocumentBuilder <UnmanagedWriteBuffer>(context))
                {
                    var lonEscapedCharsString             = string.Join(",", Enumerable.Repeat("\"Cool\"", 200).ToArray());
                    var longEscapedCharsAndNonAsciiString = string.Join(",", Enumerable.Repeat("\"מגניב\"", 200).ToArray());

                    builder.Reset(BlittableJsonDocumentBuilder.UsageMode.None);

                    builder.StartWriteObjectDocument();
                    builder.StartWriteObject();

                    builder.WritePropertyName("FloatMin");
                    builder.WriteValue(float.MinValue);

                    builder.WritePropertyName("FloatMax");
                    builder.WriteValue(float.MaxValue);

                    builder.WritePropertyName("UshortMin");
                    builder.WriteValue(ushort.MinValue);

                    builder.WritePropertyName("UshortMax");
                    builder.WriteValue(ushort.MaxValue);

                    builder.WritePropertyName("UintMin");
                    builder.WriteValue(uint.MinValue);

                    builder.WritePropertyName("UintMax");
                    builder.WriteValue(uint.MaxValue);

                    builder.WritePropertyName("DoubleMin");
                    builder.WriteValue(double.MinValue);

                    builder.WritePropertyName("DoubleMax");
                    builder.WriteValue(double.MaxValue);

                    builder.WritePropertyName("LongMin");
                    builder.WriteValue(long.MinValue);

                    builder.WritePropertyName("LongMax");
                    builder.WriteValue(long.MaxValue);

                    builder.WritePropertyName("StringEmpty");
                    builder.WriteValue(string.Empty);

                    builder.WritePropertyName("StringSimple");
                    builder.WriteValue("StringSimple");

                    builder.WritePropertyName("StringEscapedChars");
                    builder.WriteValue("\"Cool\"");

                    builder.WritePropertyName("StringLongEscapedChars");
                    builder.WriteValue(lonEscapedCharsString);

                    builder.WritePropertyName("StringEscapedCharsAndNonAscii");
                    builder.WriteValue(longEscapedCharsAndNonAsciiString);


                    var lsvString      = "\"fooאbar\"";
                    var lsvStringBytes = Encoding.UTF8.GetBytes(lsvString);
                    fixed(byte *b = lsvStringBytes)
                    {
                        var lsv             = new LazyStringValue(null, b, lsvStringBytes.Length, context);
                        var escapePositions = new List <int>();

                        BlittableWriter <UnmanagedWriteBuffer> .FillBufferWithEscapePositions(lsvString, escapePositions);

                        lsv.EscapePositions = escapePositions.ToArray();

                        builder.WritePropertyName("LSVString");
                        builder.WriteValue(lsv);
                    }

                    builder.WritePropertyName("Embedded");
                    builder.WriteEmbeddedBlittableDocument(embeddedReader);

                    builder.WriteObjectEnd();
                    builder.FinalizeDocument();

                    var reader = builder.CreateReader();
                    Assert.Equal(17, reader.Count);
                    Assert.Equal(float.MinValue, float.Parse(reader["FloatMin"].ToString(), CultureInfo.InvariantCulture));
                    Assert.Equal(float.MaxValue, float.Parse(reader["FloatMax"].ToString(), CultureInfo.InvariantCulture));
                    Assert.Equal(ushort.MinValue, ushort.Parse(reader["UshortMin"].ToString(), CultureInfo.InvariantCulture));
                    Assert.Equal(ushort.MaxValue, ushort.Parse(reader["UshortMax"].ToString(), CultureInfo.InvariantCulture));
                    Assert.Equal(uint.MinValue, uint.Parse(reader["UintMin"].ToString(), CultureInfo.InvariantCulture));
                    Assert.Equal(uint.MaxValue, uint.Parse(reader["UintMax"].ToString(), CultureInfo.InvariantCulture));
                    Assert.Equal(double.MinValue, double.Parse(reader["DoubleMin"].ToString(), CultureInfo.InvariantCulture));
                    Assert.Equal(double.MaxValue, double.Parse(reader["DoubleMax"].ToString(), CultureInfo.InvariantCulture));
                    Assert.Equal(long.MinValue, long.Parse(reader["LongMin"].ToString(), CultureInfo.InvariantCulture));
                    Assert.Equal(long.MaxValue, long.Parse(reader["LongMax"].ToString(), CultureInfo.InvariantCulture));
                    Assert.Equal(string.Empty, reader["StringEmpty"].ToString());
                    Assert.Equal("StringSimple", reader["StringSimple"].ToString());
                    Assert.Equal("\"Cool\"", reader["StringEscapedChars"].ToString());
                    Assert.Equal(lonEscapedCharsString, reader["StringLongEscapedChars"].ToString());
                    Assert.Equal(longEscapedCharsAndNonAsciiString, reader["StringEscapedCharsAndNonAscii"].ToString());
                    Assert.Equal(lsvString, reader["LSVString"].ToString());
                    Assert.Equal(1000, int.Parse((reader["Embedded"] as BlittableJsonReaderObject)["Value"].ToString(), CultureInfo.InvariantCulture));
                }
            }
        }