private Dictionary<string, string> ReadHeaders(ref Utf8JsonReader reader) { var headers = new Dictionary<string, string>(StringComparer.Ordinal); if (reader.TokenType != JsonTokenType.StartObject) { throw new InvalidDataException($"Expected '{HeadersPropertyName}' to be of type {JsonTokenType.StartObject}."); } while (reader.Read()) { switch (reader.TokenType) { case JsonTokenType.PropertyName: var propertyName = reader.GetString(); reader.CheckRead(); if (reader.TokenType != JsonTokenType.String) { throw new InvalidDataException($"Expected header '{propertyName}' to be of type {JsonTokenType.String}."); } headers[propertyName] = reader.GetString(); break; case JsonTokenType.Comment: break; case JsonTokenType.EndObject: return headers; } } throw new InvalidDataException("Unexpected end when reading message headers"); }
private object?[] BindTypes(ref Utf8JsonReader reader, IReadOnlyList <Type> paramTypes) { object?[]? arguments = null; var paramIndex = 0; var paramCount = paramTypes.Count; var depth = reader.CurrentDepth; reader.CheckRead(); while (reader.TokenType != JsonTokenType.EndArray && reader.CurrentDepth > depth) { if (paramIndex < paramCount) { arguments ??= new object?[paramCount]; try { arguments[paramIndex] = BindType(ref reader, paramTypes[paramIndex]); } catch (Exception ex) { throw new InvalidDataException("Error binding arguments. Make sure that the types of the provided values match the types of the hub method being invoked.", ex); } } else { // Skip extra arguments and throw error after reading them all reader.Skip(); } reader.CheckRead(); paramIndex++; } if (paramIndex != paramCount) { throw new InvalidDataException($"Invocation provides {paramIndex} argument(s) but target expects {paramCount}."); } return(arguments ?? Array.Empty <object>()); }
private static AvailableTransport ParseAvailableTransport(ref Utf8JsonReader reader) { var availableTransport = new AvailableTransport(); while (reader.CheckRead()) { switch (reader.TokenType) { case JsonTokenType.PropertyName: var memberName = reader.ValueSpan; if (memberName.SequenceEqual(TransportPropertyNameBytes)) { availableTransport.Transport = reader.ReadAsString(TransportPropertyNameBytes); } else if (memberName.SequenceEqual(TransferFormatsPropertyNameBytes)) { reader.CheckRead(); reader.EnsureArrayStart(); var completed = false; availableTransport.TransferFormats = new List <string>(); while (!completed && reader.CheckRead()) { switch (reader.TokenType) { case JsonTokenType.String: availableTransport.TransferFormats.Add(reader.GetString()); break; case JsonTokenType.EndArray: completed = true; break; default: throw new InvalidDataException($"Unexpected token '{reader.TokenType}' when reading transfer formats JSON."); } } } else { reader.Skip(); } break; case JsonTokenType.EndObject: if (availableTransport.Transport == null) { throw new InvalidDataException($"Missing required property '{TransportPropertyName}'."); } if (availableTransport.TransferFormats == null) { throw new InvalidDataException($"Missing required property '{TransferFormatsPropertyName}'."); } return(availableTransport); default: throw new InvalidDataException($"Unexpected token '{reader.TokenType}' when reading available transport JSON."); } } throw new InvalidDataException("Unexpected end when reading JSON."); }