void TestB(CompiledPacker packer) { TestB_Class obj0 = TestB_Class.Create(); TestB_Class obj1 = packer.Unpack <TestB_Class> (packer.Pack <TestB_Class> (obj0)); obj0.Check(obj1); }
void TestA(CompiledPacker packer) { TestA_Class obj0 = new TestA_Class(); TestA_Class obj1 = packer.Unpack <TestA_Class> (packer.Pack <TestA_Class> (obj0)); obj0.Check(obj1); }
private void TestPacker <T>(T obj) { var packer = new CompiledPacker(true); var res = packer.Pack(obj); Console.WriteLine(HexDump.Dump(res)); Assert.Greater(res.Length, 0); }
public void Int_Works() { var packer = new CompiledPacker(); packer.Pack(new IntClass { Test = 32 }); }
public void String_Works() { var packer = new CompiledPacker(); packer.Pack(new StringClass { Test = "Test" }); }
public void TwoPackers_SameType_DoNotConflict() { var packerA = new CompiledPacker(false); var packerB = new CompiledPacker(false); var item = new TestTwoPackers {Test = "TestString"}; packerA.Pack(item); packerB.Pack(item); }
public void Dictionary_Works() { var packer = new CompiledPacker(true); var res = packer.Pack(new Dictionary <string, string> { { "a", "b" }, { "c", "d" } }); Assert.Greater(res.Length, 0); }
public void DateTime_DoesntWork() { var packer = new CompiledPacker(); Assert.Throws <AccessViolationException>( () => packer.Pack(new DateTimeClass { Test = DateTime.Now })); }
/// <summary> /// 序列成bytes /// </summary> /// <param name="o">对像</param> /// <returns>bytes</returns> public byte[] Serialize <T>(T o) { return(packer.Pack(o)); }
public override void WriteToStream(Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content) { if (type == null) throw new ArgumentNullException("type is null"); if (writeStream == null) throw new ArgumentNullException("writeStream is null"); if (typeof(IEnumerable).IsAssignableFrom(type)) value = (value as IEnumerable<object>).ToList(); var packer = new CompiledPacker(packPrivateField: false); byte[] buffer = packer.Pack(value); writeStream.Write(buffer, 0, buffer.Length); }
//static ObjectPacker packer = new MsgPack.ObjectPacker(); public static byte[] MsgPackToBytes <T>(T dto) { return(packer.Pack(dto)); }
void TestB(CompiledPacker packer) { TestB_Class obj0 = TestB_Class.Create (); TestB_Class obj1 = packer.Unpack<TestB_Class> (packer.Pack<TestB_Class> (obj0)); obj0.Check (obj1); }
void TestA(CompiledPacker packer) { TestA_Class obj0 = new TestA_Class (); TestA_Class obj1 = packer.Unpack<TestA_Class> (packer.Pack<TestA_Class> (obj0)); obj0.Check (obj1); }
//Yay, fun method! public Dictionary<object, object> Execute(string method, object[] args) { if (string.IsNullOrEmpty(_host)) throw new Exception("Host null or empty"); if (method != "auth.login" && string.IsNullOrEmpty(_token)) throw new Exception("Not authenticated."); BoxingPacker boxingPacker = new BoxingPacker(); CompiledPacker compiledPacker = new CompiledPacker(true); ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {return true;}; //dis be bad, no ssl check HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_host); request.ContentType = "binary/message-pack"; request.Method = "POST"; Stream requestStream = request.GetRequestStream(); MsgPackWriter msgpackWriter = new MsgPackWriter(requestStream); msgpackWriter.WriteArrayHeader(args.Length + 1 + (string.IsNullOrEmpty(_token) ? 0 : 1)); msgpackWriter.Write(method); if (!string.IsNullOrEmpty(_token)) msgpackWriter.Write(_token); foreach (object arg in args) { if (arg is string) msgpackWriter.Write(arg as string); else if (arg is Dictionary<object, object>) msgpackWriter.Write(compiledPacker.Pack<Dictionary<object, object>>(arg as Dictionary<object, object>)); } requestStream.Close(); Stream responseStream = request.GetResponse().GetResponseStream(); //everything is a bunch of bytes, needs to be typed Dictionary<object, object> resp = boxingPacker.Unpack(responseStream) as Dictionary<object, object>; //This is me trying to type the response for the user.... Dictionary<object, object> returnDictionary = new Dictionary<object, object>(); System.Text.Encoding enc = System.Text.Encoding.UTF8; foreach (KeyValuePair<object, object> pair in resp) { string keyType = pair.Key.GetType().ToString(); string valueType = pair.Value.GetType().ToString(); if (pair.Value.GetType() == typeof(bool)) returnDictionary.Add(enc.GetString(pair.Key as byte[]), ((bool)pair.Value).ToString()); else if (pair.Value.GetType() == typeof(byte[])) returnDictionary.Add(enc.GetString(pair.Key as byte[]), enc.GetString(pair.Value as byte[])); else if (pair.Value.GetType() == typeof(object[])) returnDictionary.Add(enc.GetString(pair.Key as byte[]), pair.Value); else if (pair.Value.GetType() == typeof(UInt32)) returnDictionary.Add(enc.GetString(pair.Key as byte[]), ((UInt32)pair.Value).ToString()); else if (pair.Value.GetType() == typeof(Int32)) returnDictionary.Add(enc.GetString(pair.Key as byte[]), ((Int32)pair.Value).ToString()); else throw new Exception("key type: " + keyType + ", value type: " + valueType); } return returnDictionary; }