internal static byte[] ConvertParametersToBytes(object[] parameters, bool addTypeIdPrefix = true) { var paramtersBytes = new List <byte[]>(); foreach (var parameter in parameters) { if (parameter != null) { if (addTypeIdPrefix) { paramtersBytes.Add(GetTypeId(parameter.GetType())); } if (parameter is int) { paramtersBytes.Add(SerDe.ToBytes((int)parameter)); } else if (parameter is long) { paramtersBytes.Add(SerDe.ToBytes((long)parameter)); } else if (parameter is string) { paramtersBytes.Add(ToPayloadBytes(parameter.ToString())); } else if (parameter is bool) { paramtersBytes.Add(SerDe.ToBytes((bool)parameter)); } else if (parameter is double) { paramtersBytes.Add(SerDe.ToBytes((double)parameter)); } else if (parameter is byte[]) { paramtersBytes.Add(SerDe.ToBytes(((byte[])parameter).Length)); paramtersBytes.Add((byte[])parameter); } else if (parameter is int[]) { paramtersBytes.Add(GetTypeId(typeof(int))); paramtersBytes.Add(SerDe.ToBytes(((int[])parameter).Length)); paramtersBytes.AddRange(((int[])parameter).Select(x => SerDe.ToBytes(x))); } else if (parameter is long[]) { paramtersBytes.Add(GetTypeId(typeof(long))); paramtersBytes.Add(SerDe.ToBytes(((long[])parameter).Length)); paramtersBytes.AddRange(((long[])parameter).Select(x => SerDe.ToBytes(x))); } else if (parameter is double[]) { paramtersBytes.Add(GetTypeId(typeof(double))); paramtersBytes.Add(SerDe.ToBytes(((double[])parameter).Length)); paramtersBytes.AddRange(((double[])parameter).Select(x => SerDe.ToBytes(x))); } else if (parameter is IEnumerable <byte[]> ) { paramtersBytes.Add(GetTypeId(typeof(byte[]))); paramtersBytes.Add(SerDe.ToBytes(((IEnumerable <byte[]>)parameter).Count())); //TODO - Count() will traverse the collection - change interface? foreach (var byteArray in (IEnumerable <byte[]>)parameter) { paramtersBytes.Add(SerDe.ToBytes(byteArray.Length)); paramtersBytes.Add(byteArray); } } else if (parameter is IEnumerable <string> ) { paramtersBytes.Add(GetTypeId(typeof(string))); paramtersBytes.Add(SerDe.ToBytes(((IEnumerable <string>)parameter).Count())); //TODO - Count() will traverse the collection - change interface? paramtersBytes.AddRange(from stringVal in (IEnumerable <string>) parameter select ToPayloadBytes(stringVal)); } else if (parameter is IEnumerable <JvmObjectReference> ) { paramtersBytes.Add(GetTypeId(typeof(JvmObjectReference))); paramtersBytes.Add(SerDe.ToBytes(((IEnumerable <JvmObjectReference>)parameter).Count())); //TODO - Count() will traverse the collection - change interface? paramtersBytes.AddRange(from jObj in (IEnumerable <JvmObjectReference>) parameter select ToPayloadBytes(jObj.Id)); } else if (IsDictionary(parameter.GetType())) { // Generic Dictionary 'parameter' passed into this function is a object which lost its Generic Type T (namely Dictionary<T, T>). // Cannot neither cast to Dictionary<T, T> nor Dictionary<dynamic, dynamic>, so we need to first cast to Non-Generic interface IDictionary and then // rebuild a Dictionary<dynamic, dynamic>. var nonGenericDict = (IDictionary)parameter; var dict = new Dictionary <dynamic, dynamic>(); foreach (var k in nonGenericDict.Keys) { dict[k] = nonGenericDict[k]; } Type keyType = parameter.GetType().GetGenericArguments()[0]; // Below serialization is coressponding to deserialization method ReadMap() of SerDe.scala paramtersBytes.Add(SerDe.ToBytes(dict.Count())); // dictionary's length paramtersBytes.Add(GetTypeId(keyType)); // keys' data type paramtersBytes.Add(SerDe.ToBytes(dict.Count())); // keys' length, same as dictionary's length paramtersBytes.AddRange(from kv in dict select ConvertParametersToBytes(new object[] { kv.Key }, false)); // keys, do not need type prefix paramtersBytes.Add(SerDe.ToBytes(dict.Count())); // values' length, same as dictionary's length paramtersBytes.AddRange(from kv in dict select ConvertParametersToBytes(new object[] { kv.Value })); // values, need type prefix } else if (parameter is JvmObjectReference) { paramtersBytes.Add(ToPayloadBytes((parameter as JvmObjectReference).Id)); } else { throw new NotSupportedException(string.Format("Type {0} is not supported", parameter.GetType())); } } else { paramtersBytes.Add(new [] { Convert.ToByte('n') }); } } return(paramtersBytes.SelectMany(byteArray => byteArray).ToArray()); }
public void Write(string value) { byte[] buffer = SerDe.ToBytes(value); Write(buffer.Length); Write(buffer); }