public MessagePackObject PopObj() { MessagePackObject mpo; if (!unpacker.ReadObject(out mpo)) { return(0); } return(mpo); }
protected override Capability UnpackFromCore(Unpacker unpacker) { MessagePackObject obj = ""; if (unpacker.ReadObject(out obj)) { return(new Capability(obj.ToString())); } return(new Capability()); }
/// <summary> /// Unpack json document. /// </summary> /// <remarks> /// Json support 110% some value not suppport in json format may not generate error and output perfactly /// <para> "name" : "value" output as INode with Name value</para> /// <para> </para> /// </remarks> /// <param name="unpacker"></param> /// <returns><see cref="Node"/> of json</returns> /// <exception cref="FormatException">document invalid format <seealso cref="Unpacker.LineCount"/> to find where error locate</exception> public static INode JsonDocument(this Unpacker unpacker) { if (unpacker.Peek() != '{') { throw new FormatException("json document must start with '{'"); } else { INode json = new Node() { SubNode = unpacker.ReadObject() }; return(json); } }
protected void UnpackTo(Unpacker unpacker, IDictionary <string, object> dict, long mapLength) { for (long i = 0; i < mapLength; i++) { string key; MessagePackObject value; if (!unpacker.ReadString(out key)) { throw new InvalidMessagePackStreamException("string expected for a map key"); } if (!unpacker.ReadObject(out value)) { throw new InvalidMessagePackStreamException("unexpected EOF"); } if (unpacker.LastReadData.IsNil) { dict.Add(key, null); } else if (unpacker.IsMapHeader) { long innerMapLength = value.AsInt64(); var innerDict = new Dictionary <string, object>(); UnpackTo(unpacker, innerDict, innerMapLength); dict.Add(key, innerDict); } else if (unpacker.IsArrayHeader) { long innerArrayLength = value.AsInt64(); var innerArray = new List <object>(); UnpackTo(unpacker, innerArray, innerArrayLength); dict.Add(key, innerArray); } else { dict.Add(key, value.ToObject()); } } }
protected internal override DictionaryEntry UnpackFromCore(Unpacker unpacker) { if (unpacker.IsArrayHeader) { MessagePackObject key; MessagePackObject value; if (!unpacker.ReadObject(out key)) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } if (!unpacker.ReadObject(out value)) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } return(new DictionaryEntry(key, value)); } else { // Previous DictionaryEntry serializer accidentally pack it as map... MessagePackObject key = default(MessagePackObject); MessagePackObject value = default(MessagePackObject); bool isKeyFound = false; bool isValueFound = false; string propertyName; while ((!isKeyFound || !isValueFound) && unpacker.ReadString(out propertyName)) { switch (propertyName) { case "Key": { if (!unpacker.ReadObject(out key)) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } isKeyFound = true; break; } case "Value": { if (!unpacker.ReadObject(out value)) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } isValueFound = true; break; } } } if (!isKeyFound) { throw SerializationExceptions.NewMissingProperty("Key"); } if (!isValueFound) { throw SerializationExceptions.NewMissingProperty("Value"); } return(new DictionaryEntry(key, value)); } }