ReadAsBytes() public method

Reads the next JSON token from the stream as a Byte[].
public ReadAsBytes ( ) : byte[]
return byte[]
Beispiel #1
0
        /// <summary>
        /// Reads the next JSON token from the stream as a <see cref="T:Byte[]"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:Byte[]"/> or a null reference if the next JSON token is null.
        /// </returns>
        public override byte[] ReadAsBytes()
        {
            byte[] data = _reader.ReadAsBytes();

            ValidateCurrentToken();
            return(data);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if( reader.TokenType == JsonToken.Null )
            {
                return null;
            }

            if( reader.TokenType != JsonToken.StartObject )
            {
                var msg = string.Join(" ",
                    $"The JSON representation of binary data (byte[]) when parsing the server response is not a {Converter.PseudoTypeKey}:{Converter.Binary} object.",
                    $"This happens if your JSON document contains binary data (byte[]) in some other format (like a base64 string only) rather than a native RethinkDB pseudo type {Converter.PseudoTypeKey}:{Converter.Binary} object.",
                    $"If you are overriding the default Ser/Deserialization process, you need to make sure byte[] is a native {Converter.PseudoTypeKey}:{Converter.Binary} objects before using the built-in {nameof(ReqlBinaryConverter)}.",
                    "See https://rethinkdb.com/docs/data-types/ for more information about how binary data is represented in RethinkDB.");
                throw new JsonSerializationException(msg);
            }

            reader.ReadAndAssertProperty(Converter.PseudoTypeKey);
            var reql_type = reader.ReadAsString();
            if( reql_type != Converter.Binary )
            {
                throw new JsonSerializationException($"Expected {Converter.PseudoTypeKey} should be {Converter.Binary} but got {reql_type}.");
            }

            reader.ReadAndAssertProperty("data");

            var data = reader.ReadAsBytes();

            //realign and get out of the pseudo type
            //one more post read to align out of { reql_type:BINARY, data:""} 
            reader.ReadAndAssert();

            return data;
        }
 public IConfigureCloudQueueMessageEnvelope Read(JsonReader reader)
 {
     if (reader == null) throw new ArgumentNullException("reader");
     IConfigureCloudQueueMessageEnvelope envelope = new CloudQueueMessageEnvelope();
     reader.Read(); //StartObject
     reader.Read(); //PropertyName:From
     envelope = envelope.SetFrom(reader.ReadAsString()); //String:From value
     reader.Read(); //PropertyName:To
     envelope = envelope.SetTo(reader.ReadAsString()); //String:To value
     reader.Read(); //PropertyName:MessageId
     envelope = envelope.SetMessageId(new Guid(reader.ReadAsString())); //String:MessageId value
     reader.Read();
     if (((string)reader.Value) == "RelatesToMessageId") //PropertyName:Possibly RelatesToMessageId
     {
         envelope = envelope.SetRelatesToMessageId(new Guid(reader.ReadAsString()));
         reader.Read(); //PropertyName:CorrelationId
     }
     envelope = envelope.SetCorrelationId(new Guid(reader.ReadAsString())); //String:CorrelationId value
     reader.Read(); //PropertyName:ContentType
     envelope = envelope.SetContentType(reader.ReadAsString()); //String:ContentType value
     reader.Read(); //PropertyName:Content
     envelope = envelope.SetContent(reader.ReadAsBytes()); //String:Content value
     reader.Read(); //PropertyName:Time
     envelope = envelope.SetTime(reader.ReadAsDateTimeOffset().GetValueOrDefault()); //String:Time value
     reader.Read(); //EndObject
     return envelope;
 }
        private static void ReadByDataType(JsonReader reader, DataType dataType)
        {
            switch (dataType)
            {
                case DataType.String:
                    reader.ReadAsString();
                    break;

                case DataType.Int16:
                case DataType.Int32:
                    reader.ReadAsInt32();
                    break;

                case DataType.ByteArray:
                    reader.ReadAsBytes();
                    break;

                case DataType.Decimal:
                    reader.ReadAsDecimal();
                    break;

                case DataType.DateTime:
                    reader.ReadAsDateTime();
                    break;

                // These types are dealt with, but there's no explicit reader method for them
                // so we just have to trust that it does the right thing.
                case DataType.Byte:
                case DataType.Int64:
                case DataType.Boolean:
                case DataType.Float:
                case DataType.Double:
                    reader.Read();
                    break;
            }
        }
Beispiel #5
0
 public override byte[] ReadAsBytes()
 {
     byte[] result = _reader.ReadAsBytes();
     ValidateCurrentToken();
     return(result);
 }
 private bool ReadForType(JsonReader reader, JsonContract contract, bool hasConverter)
 {
   if (hasConverter)
     return reader.Read();
   switch (contract != null ? (int) contract.InternalReadType : 0)
   {
     case 0:
       while (reader.Read())
       {
         if (reader.TokenType != JsonToken.Comment)
           return true;
       }
       return false;
     case 1:
       reader.ReadAsInt32();
       break;
     case 2:
       reader.ReadAsBytes();
       break;
     case 3:
       reader.ReadAsString();
       break;
     case 4:
       reader.ReadAsDecimal();
       break;
     case 5:
       reader.ReadAsDateTime();
       break;
     default:
       throw new ArgumentOutOfRangeException();
   }
   return reader.TokenType != JsonToken.None;
 }