private static MessagePackObjectDictionary UnpackDictionaryCore(Unpacker unpacker) { if (IsNil(unpacker)) { return(null); } uint count = ( uint )unpacker.LastReadData; if (count > Int32.MaxValue) { throw new MessageNotSupportedException("The map which count is greater than Int32.MaxValue is not supported."); } var result = new MessagePackObjectDictionary(unchecked (( int )count)); for (int i = 0; i < count; i++) { var key = UnpackObjectCore(unpacker); var value = UnpackObjectCore(unpacker); try { result.Add(key, value); } catch (ArgumentException ex) { throw new InvalidMessagePackStreamException("The dicationry key is duplicated in the stream.", ex); } } return(result); }
private static MessagePackObjectDictionary UnpackDictionaryCore(Unpacker unpacker) { if (IsNil(unpacker)) { return(null); } uint num = (uint)unpacker.Data.Value; if (num > 0x7fffffff) { throw new MessageNotSupportedException("The map which count is greater than Int32.MaxValue is not supported."); } MessagePackObjectDictionary dictionary = new MessagePackObjectDictionary((int)num); for (int i = 0; i < num; i++) { MessagePackObject key = UnpackObjectCore(unpacker); MessagePackObject obj3 = UnpackObjectCore(unpacker); try { dictionary.Add(key, obj3); } catch (ArgumentException exception) { throw new InvalidMessagePackStreamException("The dicationry key is duplicated in the stream.", exception); } } return(dictionary); }
/// <summary> /// Unpacks current subtree and returns subtree root as array or map. /// </summary> /// <returns> /// An unpacked array or map when current position is array or map header. /// <c>null</c> when current position is not array nor map header. /// </returns> public MessagePackObject?UnpackSubtree() { if (this.IsArrayHeader) { var array = new MessagePackObject[checked (( int )this.Data.Value.AsUInt32())]; using (var subTreeReader = this.ReadSubtree()) { for (int i = 0; i < array.Length; i++) { var item = subTreeReader.ReadItem(); Contract.Assert(item.HasValue); array[i] = item.Value; } } this.Data = new MessagePackObject(array, true); } else if (this.IsMapHeader) { var capacity = checked (( int )this.Data.Value.AsUInt32()); var map = new MessagePackObjectDictionary(capacity); using (var subTreeReader = this.ReadSubtree()) { for (int i = 0; i < capacity; i++) { var key = subTreeReader.ReadItem(); var value = subTreeReader.ReadItem(); Contract.Assert(key.HasValue); Contract.Assert(value.HasValue); map.Add(key.Value, value.Value); } } this.Data = new MessagePackObject(map, true); } else { return(null); } return(this.Data); }
internal bool UnpackSubtreeDataCore(out MessagePackObject result) { // ReSharper disable RedundantIfElseBlock if (this.IsArrayHeader) { var array = new MessagePackObject[checked (( int )this.LastReadData.AsUInt32())]; using (var subTreeReader = this.ReadSubtree()) { for (int i = 0; i < array.Length; i++) { array[i] = subTreeReader.ReadItemData(); } } result = new MessagePackObject(array, true); return(true); } else if (this.IsMapHeader) { var capacity = checked (( int )this.LastReadData.AsUInt32()); var map = new MessagePackObjectDictionary(capacity); using (var subTreeReader = this.ReadSubtree()) { for (int i = 0; i < capacity; i++) { var key = subTreeReader.ReadItemData(); var value = subTreeReader.ReadItemData(); map.Add(key, value); } } result = new MessagePackObject(map, true); return(true); } else { result = default(MessagePackObject); return(false); } // ReSharper restore RedundantIfElseBlock }
public MessagePackObject?UnpackSubtree() { Unpacker unpacker; int num; if (this.IsArrayHeader) { MessagePackObject[] objArray = new MessagePackObject[this.Data.Value.AsUInt32()]; using (unpacker = this.ReadSubtree()) { for (num = 0; num < objArray.Length; num++) { MessagePackObject?nullable = unpacker.ReadItem(); Contract.Assert(nullable.HasValue); objArray[num] = nullable.Value; } } this.Data = new MessagePackObject(objArray, true); } else if (this.IsMapHeader) { int initialCapacity = (int)this.Data.Value.AsUInt32(); MessagePackObjectDictionary dictionary = new MessagePackObjectDictionary(initialCapacity); using (unpacker = this.ReadSubtree()) { for (num = 0; num < initialCapacity; num++) { MessagePackObject?nullable2 = unpacker.ReadItem(); MessagePackObject?nullable3 = unpacker.ReadItem(); Contract.Assert(nullable2.HasValue); Contract.Assert(nullable3.HasValue); dictionary.Add(nullable2.Value, nullable3.Value); } } this.Data = new MessagePackObject(dictionary, true); } else { return(null); } return(this.Data); }
MsgPack.MessagePackObject convertToMsgPackObject(object v) { Type t = v.GetType(); if (t.Equals(typeof(Hashtable))) { Hashtable ht = v as Hashtable; IDictionaryEnumerator e = ht.GetEnumerator(); MsgPack.MessagePackObjectDictionary d = new MsgPack.MessagePackObjectDictionary(); while (e.MoveNext()) { d.Add(new MsgPack.MessagePackObject(e.Key as string), convertToMsgPackObject(e.Value)); } return(new MsgPack.MessagePackObject(d)); } else if (t.Equals(typeof(ArrayList))) { ArrayList al = v as ArrayList; IEnumerator e = al.GetEnumerator(); System.Collections.Generic.IList <MsgPack.MessagePackObject> l = new System.Collections.Generic.List <MsgPack.MessagePackObject> (); while (e.MoveNext()) { l.Add(convertToMsgPackObject(e.Current)); } return(new MsgPack.MessagePackObject(l)); } else if (t.Equals(typeof(bool))) { return(new MsgPack.MessagePackObject((bool)v)); } else if (t.Equals(typeof(string))) { return(new MsgPack.MessagePackObject((string)v)); } else { Debug.AssertFormat(false, "Unknwon type: " + t.Name); return(new MsgPack.MessagePackObject()); } }
internal async Task <AsyncReadResult <MessagePackObject> > UnpackSubtreeDataAsyncCore(CancellationToken cancellationToken) { if (this.IsArrayHeader) { var array = new MessagePackObject[checked (( int )this.LastReadData.AsUInt32())]; using (var subTreeReader = this.ReadSubtree()) { for (int i = 0; i < array.Length; i++) { array[i] = await subTreeReader.ReadItemDataAsync(cancellationToken).ConfigureAwait(false); } } return(AsyncReadResult.Success(new MessagePackObject(array, true))); } else if (this.IsMapHeader) { var capacity = checked (( int )this.LastReadData.AsUInt32()); var map = new MessagePackObjectDictionary(capacity); using (var subTreeReader = this.ReadSubtree()) { for (int i = 0; i < capacity; i++) { var key = await subTreeReader.ReadItemDataAsync(cancellationToken).ConfigureAwait(false); var value = await subTreeReader.ReadItemDataAsync(cancellationToken).ConfigureAwait(false); map.Add(key, value); } } return(AsyncReadResult.Success(new MessagePackObject(map, true))); } else { return(AsyncReadResult.Fail <MessagePackObject>()); } }