public object Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) { var type = MessagePackBinary.GetMessagePackType(bytes, offset); switch (type) { case MessagePackType.Integer: var code = bytes[offset]; if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt) { return(MessagePackBinary.ReadSByte(bytes, offset, out readSize)); } else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt) { return(MessagePackBinary.ReadByte(bytes, offset, out readSize)); } else if (code == MessagePackCode.Int8) { return(MessagePackBinary.ReadSByte(bytes, offset, out readSize)); } else if (code == MessagePackCode.Int16) { return(MessagePackBinary.ReadInt16(bytes, offset, out readSize)); } else if (code == MessagePackCode.Int32) { return(MessagePackBinary.ReadInt32(bytes, offset, out readSize)); } else if (code == MessagePackCode.Int64) { return(MessagePackBinary.ReadInt64(bytes, offset, out readSize)); } else if (code == MessagePackCode.UInt8) { return(MessagePackBinary.ReadByte(bytes, offset, out readSize)); } else if (code == MessagePackCode.UInt16) { return(MessagePackBinary.ReadUInt16(bytes, offset, out readSize)); } else if (code == MessagePackCode.UInt32) { return(MessagePackBinary.ReadUInt32(bytes, offset, out readSize)); } else if (code == MessagePackCode.UInt64) { return(MessagePackBinary.ReadUInt64(bytes, offset, out readSize)); } throw new InvalidOperationException("Invalid primitive bytes."); case MessagePackType.Boolean: return(MessagePackBinary.ReadBoolean(bytes, offset, out readSize)); case MessagePackType.Float: if (MessagePackCode.Float32 == bytes[offset]) { return(MessagePackBinary.ReadSingle(bytes, offset, out readSize)); } else { return(MessagePackBinary.ReadDouble(bytes, offset, out readSize)); } case MessagePackType.String: return(MessagePackBinary.ReadString(bytes, offset, out readSize)); case MessagePackType.Binary: return(MessagePackBinary.ReadBytes(bytes, offset, out readSize)); case MessagePackType.Extension: var ext = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize); if (ext.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime) { return(MessagePackBinary.ReadDateTime(bytes, offset, out readSize)); } throw new InvalidOperationException("Invalid primitive bytes."); case MessagePackType.Array: { var length = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); var startOffset = offset; offset += readSize; var objectFormatter = formatterResolver.GetFormatter <object>(); var array = new object[length]; using (MessagePackSecurity.DepthStep()) { for (int i = 0; i < length; i++) { array[i] = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize); offset += readSize; } } readSize = offset - startOffset; return(array); } case MessagePackType.Map: { var length = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize); var startOffset = offset; offset += readSize; var objectFormatter = formatterResolver.GetFormatter <object>(); var hash = new Dictionary <object, object>(length, MessagePackSecurity.Active.GetEqualityComparer <object>()); using (MessagePackSecurity.DepthStep()) { for (int i = 0; i < length; i++) { var key = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize); offset += readSize; var value = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize); offset += readSize; hash.Add(key, value); } } readSize = offset - startOffset; return(hash); } case MessagePackType.Nil: readSize = 1; return(null); default: throw new InvalidOperationException("Invalid primitive bytes."); } }
public System.Numerics.BigInteger Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) { return(new System.Numerics.BigInteger(MessagePackBinary.ReadBytes(bytes, offset, out readSize))); }
static int ToJsonCore(byte[] bytes, int offset, StringBuilder builder) { var readSize = 0; var type = MessagePackBinary.GetMessagePackType(bytes, offset); switch (type) { case MessagePackType.Integer: var code = bytes[offset]; if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt) { builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize)); } else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt) { builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize)); } else if (code == MessagePackCode.Int8) { builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize)); } else if (code == MessagePackCode.Int16) { builder.Append(MessagePackBinary.ReadInt16(bytes, offset, out readSize)); } else if (code == MessagePackCode.Int32) { builder.Append(MessagePackBinary.ReadInt32(bytes, offset, out readSize)); } else if (code == MessagePackCode.Int64) { builder.Append(MessagePackBinary.ReadInt64(bytes, offset, out readSize)); } else if (code == MessagePackCode.UInt8) { builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize)); } else if (code == MessagePackCode.UInt16) { builder.Append(MessagePackBinary.ReadUInt16(bytes, offset, out readSize)); } else if (code == MessagePackCode.UInt32) { builder.Append(MessagePackBinary.ReadUInt32(bytes, offset, out readSize)); } else if (code == MessagePackCode.UInt64) { builder.Append(MessagePackBinary.ReadUInt64(bytes, offset, out readSize)); } break; case MessagePackType.Boolean: builder.Append(MessagePackBinary.ReadBoolean(bytes, offset, out readSize) ? "true" : "false"); break; case MessagePackType.Float: var floatCode = bytes[offset]; if (floatCode == MessagePackCode.Float32) { builder.Append(MessagePackBinary.ReadSingle(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture)); } else { builder.Append(MessagePackBinary.ReadDouble(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture)); } break; case MessagePackType.String: WriteJsonString(MessagePackBinary.ReadString(bytes, offset, out readSize), builder); break; case MessagePackType.Binary: builder.Append("\"" + Convert.ToBase64String(MessagePackBinary.ReadBytes(bytes, offset, out readSize)) + "\""); break; case MessagePackType.Array: { var length = MessagePackBinary.ReadArrayHeaderRaw(bytes, offset, out readSize); var totalReadSize = readSize; offset += readSize; builder.Append("["); for (int i = 0; i < length; i++) { readSize = ToJsonCore(bytes, offset, builder); offset += readSize; totalReadSize += readSize; if (i != length - 1) { builder.Append(","); } } builder.Append("]"); return(totalReadSize); } case MessagePackType.Map: { var length = MessagePackBinary.ReadMapHeaderRaw(bytes, offset, out readSize); var totalReadSize = readSize; offset += readSize; builder.Append("{"); for (int i = 0; i < length; i++) { // write key { var keyType = MessagePackBinary.GetMessagePackType(bytes, offset); if (keyType == MessagePackType.String || keyType == MessagePackType.Binary) { readSize = ToJsonCore(bytes, offset, builder); } else { builder.Append("\""); readSize = ToJsonCore(bytes, offset, builder); builder.Append("\""); } offset += readSize; totalReadSize += readSize; } builder.Append(":"); // write body { readSize = ToJsonCore(bytes, offset, builder); offset += readSize; totalReadSize += readSize; } if (i != length - 1) { builder.Append(","); } } builder.Append("}"); return(totalReadSize); } case MessagePackType.Extension: var extHeader = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize); if (extHeader.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime) { var dt = MessagePackBinary.ReadDateTime(bytes, offset, out readSize); builder.Append("\""); builder.Append(dt.ToString("o", CultureInfo.InvariantCulture)); builder.Append("\""); } else { var ext = MessagePackBinary.ReadExtensionFormat(bytes, offset, out readSize); builder.Append("["); builder.Append(ext.TypeCode); builder.Append(","); builder.Append("\""); builder.Append(Convert.ToBase64String(ext.Data)); builder.Append("\""); builder.Append("]"); } break; case MessagePackType.Unknown: case MessagePackType.Nil: default: readSize = 1; builder.Append("null"); break; } return(readSize); }
public byte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) { return(MessagePackBinary.ReadBytes(bytes, offset, out readSize)); }
public static object ReadObject(byte[] input, ref int offset) { var type = MessagePackBinary.GetMessagePackType(input, offset); int size; switch (type) { case MessagePackType.Integer: var intValue = MessagePackBinary.ReadInt64(input, offset, out size); offset += size; return(intValue); case MessagePackType.Nil: MessagePackBinary.ReadNil(input, offset, out size); offset += size; return(null); case MessagePackType.Boolean: var boolValue = MessagePackBinary.ReadBoolean(input, offset, out size); offset += size; return(boolValue); case MessagePackType.Float: var doubleValue = MessagePackBinary.ReadDouble(input, offset, out size); offset += size; return(doubleValue); case MessagePackType.String: var textValue = MessagePackBinary.ReadString(input, offset, out size); offset += size; return(textValue); case MessagePackType.Binary: var binaryValue = MessagePackBinary.ReadBytes(input, offset, out size); offset += size; return(binaryValue); case MessagePackType.Array: var argumentCount = ReadArrayLength(input, ref offset, "arguments"); var array = new object[argumentCount]; for (int i = 0; i < argumentCount; i++) { array[i] = ReadObject(input, ref offset); } return(array); case MessagePackType.Map: var propertyCount = MessagePackBinary.ReadMapHeader(input, offset, out size); offset += size; var map = new Dictionary <string, object>(); for (int i = 0; i < propertyCount; i++) { textValue = MessagePackBinary.ReadString(input, offset, out size); offset += size; var value = ReadObject(input, ref offset); map[textValue] = value; } return(map); case MessagePackType.Extension: case MessagePackType.Unknown: default: return(null); } }
public void Read() { void Check1 <T, T2>(T data, T2 result, Func <Stream, T2> streamRead) { const int CheckOffset = 10; byte[] bytes = null; var len = MessagePack.Resolvers.StandardResolver.Instance.GetFormatter <T>().Serialize(ref bytes, CheckOffset, data, MessagePack.Resolvers.StandardResolver.Instance); MessagePackBinary.FastResize(ref bytes, CheckOffset + len); var ms = new MemoryStream(bytes); ms.Position = CheckOffset; streamRead(ms).Is(result); } void Check2 <T>(T data, Func <Stream, T> streamRead) { const int CheckOffset = 10; byte[] bytes = null; var len = MessagePack.Resolvers.StandardResolver.Instance.GetFormatter <T>().Serialize(ref bytes, CheckOffset, data, MessagePack.Resolvers.StandardResolver.Instance); MessagePackBinary.FastResize(ref bytes, CheckOffset + len); var ms = new MemoryStream(bytes); ms.Position = CheckOffset; streamRead(ms).Is(data); } Check1(new[] { 1, 10, 100, 1000, 10000, short.MaxValue, int.MaxValue }, 7, x => MessagePackBinary.ReadArrayHeader(x)); Check1(new[] { 1, 10, 100, 1000, 10000, short.MaxValue, int.MaxValue }, (uint)7, x => MessagePackBinary.ReadArrayHeaderRaw(x)); Check1(Nil.Default, true, x => MessagePackBinary.IsNil((x))); Check2(true, x => MessagePackBinary.ReadBoolean(x)); Check2((byte)100, x => MessagePackBinary.ReadByte(x)); Check2(new byte[] { 1, 10, 100, 245 }, x => MessagePackBinary.ReadBytes(x)); Check2('あ', x => MessagePackBinary.ReadChar(x)); Check2(DateTime.UtcNow, x => MessagePackBinary.ReadDateTime(x)); Check2(132, x => MessagePackBinary.ReadInt16(x)); Check2(423, x => MessagePackBinary.ReadInt32(x)); Check2(64332, x => MessagePackBinary.ReadInt64(x)); Check2(Nil.Default, x => MessagePackBinary.ReadNil(x)); Check2(11, x => MessagePackBinary.ReadSByte(x)); Check2(10.31231f, x => MessagePackBinary.ReadSingle(x)); Check2("foobar", x => MessagePackBinary.ReadString(x)); Check2(124, x => MessagePackBinary.ReadUInt16(x)); Check2((uint)432, x => MessagePackBinary.ReadUInt32(x)); Check2((ulong)432, x => MessagePackBinary.ReadUInt64(x)); Check1(new Dictionary <int, int>() { { 1, 2 } }, 1, x => MessagePackBinary.ReadMapHeader(x)); Check1(new Dictionary <int, int>() { { 1, 2 } }, (uint)1, x => MessagePackBinary.ReadMapHeaderRaw(x)); { var block = new object[] { 1, new[] { 1, 10, 100 }, 100 }; var bytes = MessagePackSerializer.Serialize(block); var stream = new MemoryStream(bytes); MessagePackBinary.ReadNext(stream); // array(first) MessagePackBinary.ReadNext(stream); // int MessagePackBinary.ReadNextBlock(stream); // skip array MessagePackBinary.ReadInt32(stream).Is(100); } { var block = new object[] { 1, new Dictionary <int, int> { { 1, 10 }, { 111, 200 } }, 100 }; var bytes = MessagePackSerializer.Serialize(block); var stream = new MemoryStream(bytes); MessagePackBinary.ReadNext(stream); MessagePackBinary.ReadNext(stream); MessagePackBinary.ReadNextBlock(stream); MessagePackBinary.ReadInt32(stream).Is(100); } }
static int ToJsonCore(byte[] bytes, int offset, StringBuilder builder) { var readSize = 0; var type = MessagePackBinary.GetMessagePackType(bytes, offset); switch (type) { case MessagePackType.Integer: var code = bytes[offset]; if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt) { builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize)); } else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt) { builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize)); } else if (code == MessagePackCode.Int8) { builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize)); } else if (code == MessagePackCode.Int16) { builder.Append(MessagePackBinary.ReadInt16(bytes, offset, out readSize)); } else if (code == MessagePackCode.Int32) { builder.Append(MessagePackBinary.ReadInt32(bytes, offset, out readSize)); } else if (code == MessagePackCode.Int64) { builder.Append(MessagePackBinary.ReadInt64(bytes, offset, out readSize)); } else if (code == MessagePackCode.UInt8) { builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize)); } else if (code == MessagePackCode.UInt16) { builder.Append(MessagePackBinary.ReadUInt16(bytes, offset, out readSize)); } else if (code == MessagePackCode.UInt32) { builder.Append(MessagePackBinary.ReadUInt32(bytes, offset, out readSize)); } else if (code == MessagePackCode.UInt64) { builder.Append(MessagePackBinary.ReadUInt64(bytes, offset, out readSize)); } break; case MessagePackType.Boolean: builder.Append(MessagePackBinary.ReadBoolean(bytes, offset, out readSize) ? "true" : "false"); break; case MessagePackType.Float: var floatCode = bytes[offset]; if (floatCode == MessagePackCode.Float32) { builder.Append(MessagePackBinary.ReadSingle(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture)); } else { builder.Append(MessagePackBinary.ReadDouble(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture)); } break; case MessagePackType.String: WriteJsonString(MessagePackBinary.ReadString(bytes, offset, out readSize), builder); break; case MessagePackType.Binary: builder.Append("\"" + Convert.ToBase64String(MessagePackBinary.ReadBytes(bytes, offset, out readSize)) + "\""); break; case MessagePackType.Array: { var length = MessagePackBinary.ReadArrayHeaderRaw(bytes, offset, out readSize); var totalReadSize = readSize; offset += readSize; builder.Append("["); for (int i = 0; i < length; i++) { readSize = ToJsonCore(bytes, offset, builder); offset += readSize; totalReadSize += readSize; if (i != length - 1) { builder.Append(","); } } builder.Append("]"); return(totalReadSize); } case MessagePackType.Map: { var length = MessagePackBinary.ReadMapHeaderRaw(bytes, offset, out readSize); var totalReadSize = readSize; offset += readSize; builder.Append("{"); for (int i = 0; i < length; i++) { // write key { var keyType = MessagePackBinary.GetMessagePackType(bytes, offset); if (keyType == MessagePackType.String || keyType == MessagePackType.Binary) { readSize = ToJsonCore(bytes, offset, builder); } else { builder.Append("\""); readSize = ToJsonCore(bytes, offset, builder); builder.Append("\""); } offset += readSize; totalReadSize += readSize; } builder.Append(":"); // write body { readSize = ToJsonCore(bytes, offset, builder); offset += readSize; totalReadSize += readSize; } if (i != length - 1) { builder.Append(","); } } builder.Append("}"); return(totalReadSize); } case MessagePackType.Extension: var extHeader = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize); if (extHeader.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime) { var dt = MessagePackBinary.ReadDateTime(bytes, offset, out readSize); builder.Append("\""); builder.Append(dt.ToString("o", CultureInfo.InvariantCulture)); builder.Append("\""); } #if NETSTANDARD || NETFRAMEWORK else if (extHeader.TypeCode == TypelessFormatter.ExtensionTypeCode) { int startOffset = offset; // prepare type name token offset += 6; var typeNameToken = new StringBuilder(); var typeNameReadSize = ToJsonCore(bytes, offset, typeNameToken); offset += typeNameReadSize; int startBuilderLength = builder.Length; if (extHeader.Length > typeNameReadSize) { // object map or array var typeInside = MessagePackBinary.GetMessagePackType(bytes, offset); if (typeInside != MessagePackType.Array && typeInside != MessagePackType.Map) { builder.Append("{"); } offset += ToJsonCore(bytes, offset, builder); // insert type name token to start of object map or array if (typeInside != MessagePackType.Array) { typeNameToken.Insert(0, "\"$type\":"); } if (typeInside != MessagePackType.Array && typeInside != MessagePackType.Map) { builder.Append("}"); } if (builder.Length - startBuilderLength > 2) { typeNameToken.Append(","); } builder.Insert(startBuilderLength + 1, typeNameToken.ToString()); } else { builder.Append("{\"$type\":\"" + typeNameToken.ToString() + "}"); } readSize = offset - startOffset; } #endif else { var ext = MessagePackBinary.ReadExtensionFormat(bytes, offset, out readSize); builder.Append("["); builder.Append(ext.TypeCode); builder.Append(","); builder.Append("\""); builder.Append(Convert.ToBase64String(ext.Data)); builder.Append("\""); builder.Append("]"); } break; case MessagePackType.Unknown: case MessagePackType.Nil: default: readSize = 1; builder.Append("null"); break; } return(readSize); }
public Product Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) { int startOffset = offset; int size = 0; Product p = new Product(); _ = MessagePackBinary.ReadMapHeader(bytes, offset, out size); offset += size; string fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Id = MessagePackBinary.ReadInt64(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Name = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Description = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Seller = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; formatterResolver.GetFormatter <Category>().Deserialize(bytes, offset, formatterResolver, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Rating = formatterResolver.GetFormatter <Rate>().Deserialize(bytes, offset, formatterResolver, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; if (bytes[offset] != 0xc0) { string price = MessagePackBinary.ReadString(bytes, offset, out size); decimal priceResult; if (Decimal.TryParse(price, out priceResult)) { p.Price = priceResult; } ; } else { MessagePackBinary.ReadNil(bytes, offset, out size); } offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Currency = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Created = MessagePackBinary.ReadDateTime(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; if (bytes[offset] != 0xc0) { p.Updated = MessagePackBinary.ReadDateTime(bytes, offset, out size); } else { MessagePackBinary.ReadNil(bytes, offset, out size); } offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Labels = formatterResolver.GetFormatter <ICollection <string> >().Deserialize(bytes, offset, formatterResolver, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; Uri uriStr = formatterResolver.GetFormatter <Uri>().Deserialize(bytes, offset, formatterResolver, out size); p.Location = uriStr; offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Image = MessagePackBinary.ReadBytes(bytes, offset, out size); offset += size; fieldName = MessagePackBinary.ReadString(bytes, offset, out size); offset += size; p.Details = formatterResolver.GetFormatter <Dictionary <string, string> >().Deserialize(bytes, offset, formatterResolver, out size); offset += size; readSize = offset - startOffset; return(p); }
public byte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) { var type = MessagePackBinary.GetMessagePackType(bytes, offset); if (type == MessagePackType.Nil) { readSize = 1; return(null); } if (type == MessagePackType.Binary) { return(MessagePackBinary.ReadBytes(bytes, offset, out readSize)); } if (type == MessagePackType.String) { var code = bytes[offset]; unchecked { if (MessagePackCode.MinFixStr <= code && code <= MessagePackCode.MaxFixStr) { var length = bytes[offset] & 0x1F; readSize = length + 1; var result = new byte[length]; Buffer.BlockCopy(bytes, offset + 1, result, 0, result.Length); return(result); } if (code == MessagePackCode.Str8) { var length = (int)bytes[offset + 1]; readSize = length + 2; var result = new byte[length]; Buffer.BlockCopy(bytes, offset + 2, result, 0, result.Length); return(result); } if (code == MessagePackCode.Str16) { var length = (bytes[offset + 1] << 8) + bytes[offset + 2]; readSize = length + 3; var result = new byte[length]; Buffer.BlockCopy(bytes, offset + 3, result, 0, result.Length); return(result); } if (code == MessagePackCode.Str32) { var length = (int)((uint)(bytes[offset + 1] << 24) | (uint)(bytes[offset + 2] << 16) | (uint)(bytes[offset + 3] << 8) | bytes[offset + 4]); readSize = length + 5; var result = new byte[length]; Buffer.BlockCopy(bytes, offset + 5, result, 0, result.Length); return(result); } } } throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); }