Ejemplo n.º 1
0
        private static void VerifyBinaryEncoding(byte[] byteArray, string encodedByteArr)
        {
            // Writing binary literal
            var builder = new StringBuilder();
            var writer = new JsonWriter(new StringWriter(builder), false /*indent*/, ODataFormat.Json, isIeee754Compatible: true);
            writer.WritePrimitiveValue(byteArray);

            // Json literals is surrounded with quotes, so we need to add quotes to the encoded string. 
            Assert.Equal("\"" + encodedByteArr + "\"", builder.ToString());

            string defaultFormattedByteArray = DefaultLiteralFormatter.Format(byteArray);
            string keyAsSegmentFormattedByteArray = KeyAsSegmentsLiteralFormatter.Format(byteArray);

            // Non key segment is surrounded with binary prefix and escaped. 
            Assert.Equal("binary\'" + Uri.EscapeDataString(encodedByteArr) + "\'", defaultFormattedByteArray);

            // Key Segments are the same as the escaped encoded string.
            Assert.Equal(Uri.EscapeDataString(encodedByteArr), keyAsSegmentFormattedByteArray);

            // Parsing binary literal
            var jsonReader = new JsonReader(new StringReader(builder.ToString()), ODataFormat.Json, isIeee754Compatible:true);
            jsonReader.Read();
            Assert.Equal(encodedByteArr, jsonReader.Value);

            object defaultParsedByteArray;
            DefaultLiteralParser.TryParseLiteral(byteArray.GetType(),Uri.UnescapeDataString(defaultFormattedByteArray), out defaultParsedByteArray).Should().BeTrue();
            Assert.Equal((byte[])defaultParsedByteArray, byteArray);

            object keyAsSegmentParsedByteArray;
            KeyAsSegmentsLiteralParser.TryParseLiteral(byteArray.GetType(), Uri.UnescapeDataString(keyAsSegmentFormattedByteArray), out keyAsSegmentParsedByteArray).Should().BeTrue();
            Assert.Equal((byte[])keyAsSegmentParsedByteArray, byteArray);
        }
Ejemplo n.º 2
0
        private JsonReader CreateJsonLightReader(string jsonValue)
        {
            JsonReader reader = new JsonReader(new StringReader(String.Format("{{ \"data\" : {0} }}", jsonValue)), ODataFormat.Json, isIeee754Compatible: false);
            reader.Read();
            reader.ReadStartObject();
            reader.ReadPropertyName();
            reader.NodeType.Should().Be(JsonNodeType.PrimitiveValue);

            return reader;
        }