public T Deserialize <T>(byte[] payload) where T : ISpecificRecord, new() { using (var ms = new MemoryStream(payload)) { var decoder = new BinaryDecoder(ms); var newObj = new T(); var reader = new SpecificDefaultReader(Schema, Schema); reader.Read(newObj, Schema, Schema, decoder); return(newObj); } }
/// <summary> /// Same as the deserialize method above but we allow called to provider an existing /// Avro DTO object that can be reused across calls to avoid new'ing an object /// for each call (for high performance situations) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="bytes"></param> /// <param name="regenObj"></param> /// <returns></returns> public static T DeserializeReuseObject <T>(byte[] bytes, T regenObj) where T : ISpecificRecord { using (var ms = new MemoryStream(bytes)) { var dec = new BinaryDecoder(ms); var reader = new SpecificDefaultReader(regenObj.Schema, regenObj.Schema); reader.Read(regenObj, dec); return(regenObj); } }
public override T Deserialize(dynamic bytes) { using (var ms = new MemoryStream(bytes)) { var dec = new BinaryDecoder(ms); var reader = new SpecificDefaultReader(InheritedEntityAvro._SCHEMA, InheritedEntityAvro._SCHEMA); reader.Read(ReuseDeserObj, dec); DoCopyIfNotCheating(ReuseDeserObj); return(RegenAppObj); } }
/// <inheritdoc /> public T Deserialize(BaseMessage message) { using (var ms = new MemoryStream(((BytesMessage)message).Body)) { var dec = new BinaryDecoder(ms); var regenObj = new T(); var reader = new SpecificDefaultReader(regenObj.Schema, regenObj.Schema); reader.Read(regenObj, dec); return(regenObj); } }
public InheritedEntityAvro DeserializeAvro(byte[] bytes) { using (var ms = new MemoryStream(bytes)) { var dec = new BinaryDecoder(ms); var reader = new SpecificDefaultReader(InheritedEntityAvro._SCHEMA, InheritedEntityAvro._SCHEMA); var regenAvroMsg = new InheritedEntityAvro(); reader.Read(regenAvroMsg, dec); return(regenAvroMsg); } }
public InheritedEntityAvro DeserializeAvro(byte[] bytes) { using (var ms = new MemoryStream(bytes)) { var dec = new BinaryDecoder(ms); var reader = new SpecificDefaultReader(InheritedEntityAvro._SCHEMA, InheritedEntityAvro._SCHEMA); var regenAvroMsg = new InheritedEntityAvro(); reader.Read(regenAvroMsg, dec); return regenAvroMsg; } }
public async Task <int> PullAvroMessagesAsync(string projectId, string subscriptionId, bool acknowledge) { SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId); int messageCount = 0; SubscriberClient subscriber = await SubscriberClient.CreateAsync(subscriptionName, settings : new SubscriberClient.Settings() { AckExtensionWindow = TimeSpan.FromSeconds(4), AckDeadline = TimeSpan.FromSeconds(10), FlowControlSettings = new FlowControlSettings(maxOutstandingElementCount: 100, maxOutstandingByteCount: 10240) }); // SubscriberClient runs your message handle function on multiple // threads to maximize throughput. Task startTask = subscriber.StartAsync((PubsubMessage message, CancellationToken cancel) => { string encoding = message.Attributes["googclient_schemaencoding"]; AvroUtilities.State state = new AvroUtilities.State(); switch (encoding) { case "BINARY": using (var ms = new MemoryStream(message.Data.ToByteArray())) { var decoder = new BinaryDecoder(ms); var reader = new SpecificDefaultReader(state.Schema, state.Schema); reader.Read <AvroUtilities.State>(state, decoder); } break; case "JSON": state = JsonConvert.DeserializeObject <AvroUtilities.State>(message.Data.ToStringUtf8()); break; default: Console.WriteLine($"Encoding not provided in message."); break; } Console.WriteLine($"Message {message.MessageId}: {state}"); Interlocked.Increment(ref messageCount); return(Task.FromResult(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack)); }); // Run for 5 seconds. await Task.Delay(5000); await subscriber.StopAsync(CancellationToken.None); // Lets make sure that the start task finished successfully after the call to stop. await startTask; return(messageCount); }
public object Deserialize(Type t, byte[] payload) { using (var ms = ReadMemoryStreamFactory(payload)) { var dec = new BinaryDecoder(ms); var message = MessageFactory(t); var readerSchema = ReadSchemaLookup(t); AssertSchemaNotNull(t, readerSchema, false); var writerSchema = WriteSchemaLookup(t); AssertSchemaNotNull(t, writerSchema, true); Log.DebugFormat(CultureInfo.InvariantCulture, "Type {0} writer schema: {1}, reader schema: {2}", t, writerSchema, readerSchema); var reader = new SpecificDefaultReader(writerSchema, readerSchema); reader.Read(message, dec); return(message); } }
public static void TestSpecific(string str, object[] result) { Protocol protocol = Protocol.Parse(str); var codegen = new CodeGen(); codegen.AddProtocol(protocol); var compileUnit = codegen.GenerateCode(); // add a constructor to the main class using the passed assignment statements CodeTypeDeclaration ctd = compileUnit.Namespaces[0].Types[(int)result[0]]; CodeConstructor constructor = new CodeConstructor(); constructor.Attributes = MemberAttributes.Public; CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]); constructor.Statements.Add(snippet); ctd.Members.Add(constructor); // add a function to the main class to populate the data // This has been moved from constructor, as it was causing some tests to pass that shouldn't when referencing a blank object on READ. CodeMemberMethod method = new CodeMemberMethod(); method.Attributes = MemberAttributes.Public; method.Name = "Populate"; CodeSnippetExpression snippet2 = new CodeSnippetExpression((string)result[3]); method.Statements.Add(snippet2); ctd.Members.Add(method); // compile var comparam = new CompilerParameters(new string[] { "mscorlib.dll" }); comparam.ReferencedAssemblies.Add("System.dll"); comparam.ReferencedAssemblies.Add("Avro.dll"); comparam.GenerateInMemory = true; var ccp = new Microsoft.CSharp.CSharpCodeProvider(); var units = new CodeCompileUnit[] { compileUnit }; var compres = ccp.CompileAssemblyFromDom(comparam, units); if (compres == null || compres.Errors.Count > 0) { for (int i = 0; i < compres.Errors.Count; i++) { Console.WriteLine(compres.Errors[i]); } } if (null != compres) { Assert.IsTrue(compres.Errors.Count == 0); } // create record ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord; // Call populate to put some data in it. Type recType = rec.GetType();; MethodInfo methodInfo = recType.GetMethod("Populate"); methodInfo.Invoke(rec, null); var x1 = compres.CompiledAssembly.FullName; Assert.IsFalse(rec == null); // serialize var stream = new MemoryStream(); var binEncoder = new BinaryEncoder(stream); var writer = new SpecificDefaultWriter(rec.Schema); writer.Write(rec.Schema, rec, binEncoder); // deserialize stream.Position = 0; var decoder = new BinaryDecoder(stream); var reader = new SpecificDefaultReader(rec.Schema, rec.Schema); var rec2 = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder); Assert.IsFalse(rec2 == null); }
public static void TestSpecific(string str, object[] result) { Protocol protocol = Protocol.Parse(str); var codegen = new CodeGen(); codegen.AddProtocol(protocol); var compileUnit = codegen.GenerateCode(); // add a constructor to the main class using the passed assignment statements CodeTypeDeclaration ctd = compileUnit.Namespaces[0].Types[(int)result[0]]; CodeConstructor constructor = new CodeConstructor(); constructor.Attributes = MemberAttributes.Public; CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]); constructor.Statements.Add(snippet); ctd.Members.Add(constructor); // add a function to the main class to populate the data // This has been moved from constructor, as it was causing some tests to pass that shouldn't when referencing a blank object on READ. CodeMemberMethod method = new CodeMemberMethod(); method.Attributes = MemberAttributes.Public; method.Name = "Populate"; CodeSnippetExpression snippet2 = new CodeSnippetExpression((string)result[3]); method.Statements.Add(snippet2); ctd.Members.Add(method); // compile var comparam = new CompilerParameters(new string[] { "mscorlib.dll" }); comparam.ReferencedAssemblies.Add("System.dll"); comparam.ReferencedAssemblies.Add("Avro.dll"); comparam.GenerateInMemory = true; var ccp = new Microsoft.CSharp.CSharpCodeProvider(); var units = new CodeCompileUnit[] { compileUnit }; var compres = ccp.CompileAssemblyFromDom(comparam, units); if (compres == null || compres.Errors.Count > 0) { for (int i = 0; i < compres.Errors.Count; i++) Console.WriteLine(compres.Errors[i]); } if (null != compres) Assert.IsTrue(compres.Errors.Count == 0); // create record ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord; // Call populate to put some data in it. Type recType = rec.GetType(); ; MethodInfo methodInfo = recType.GetMethod("Populate"); methodInfo.Invoke(rec, null); var x1 = compres.CompiledAssembly.FullName; Assert.IsFalse(rec == null); // serialize var stream = new MemoryStream(); var binEncoder = new BinaryEncoder(stream); var writer = new SpecificDefaultWriter(rec.Schema); writer.Write(rec.Schema, rec, binEncoder); // deserialize stream.Position = 0; var decoder = new BinaryDecoder(stream); var reader = new SpecificDefaultReader(rec.Schema, rec.Schema); var rec2 = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder); Assert.IsFalse(rec2 == null); }
public static void TestSpecific(string str, object[] result) { Protocol protocol = Protocol.Parse(str); var codegen = new CodeGen(); codegen.AddProtocol(protocol); var compileUnit = codegen.GenerateCode(); // add a constructor to the main class using the passed assignment statements CodeTypeDeclaration ctd = compileUnit.Namespaces[0].Types[(int)result[0]]; CodeConstructor constructor = new CodeConstructor(); constructor.Attributes = MemberAttributes.Public; CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]); constructor.Statements.Add(snippet); ctd.Members.Add(constructor); // compile var comparam = new CompilerParameters(new string[] { "mscorlib.dll" }); comparam.ReferencedAssemblies.Add("System.dll"); comparam.ReferencedAssemblies.Add("System.Core.dll"); comparam.ReferencedAssemblies.Add(Type.GetType("Mono.Runtime") != null ? "Mono.CSharp.dll" : "Microsoft.CSharp.dll"); comparam.ReferencedAssemblies.Add("Avro.dll"); comparam.GenerateInMemory = true; var ccp = new Microsoft.CSharp.CSharpCodeProvider(); var units = new CodeCompileUnit[] { compileUnit }; var compres = ccp.CompileAssemblyFromDom(comparam, units); if (compres == null || compres.Errors.Count > 0) { for (int i = 0; i < compres.Errors.Count; i++) { Console.WriteLine(compres.Errors[i]); } } if (null != compres) { Assert.IsTrue(compres.Errors.Count == 0); } // create record ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord; Assert.IsFalse(rec == null); // serialize var stream = new MemoryStream(); var binEncoder = new BinaryEncoder(stream); var writer = new SpecificDefaultWriter(rec.Schema); writer.Write(rec.Schema, rec, binEncoder); // deserialize stream.Position = 0; var decoder = new BinaryDecoder(stream); var reader = new SpecificDefaultReader(rec.Schema, rec.Schema); var rec2 = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder); Assert.IsFalse(rec2 == null); }
public static void TestSpecific(string str, object[] result) { Protocol protocol = Protocol.Parse(str); var codegen = new CodeGen(); codegen.AddProtocol(protocol); var compileUnit = codegen.GenerateCode(); // add a constructor to the main class using the passed assignment statements CodeTypeDeclaration ctd = compileUnit.Namespaces[0].Types[(int)result[0]]; CodeConstructor constructor = new CodeConstructor(); constructor.Attributes = MemberAttributes.Public; CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]); constructor.Statements.Add(snippet); ctd.Members.Add(constructor); // compile var comparam = new CompilerParameters(new string[] { "mscorlib.dll" }); comparam.ReferencedAssemblies.Add("System.dll"); comparam.ReferencedAssemblies.Add("System.Core.dll"); comparam.ReferencedAssemblies.Add(Type.GetType("Mono.Runtime") != null ? "Mono.CSharp.dll" : "Microsoft.CSharp.dll"); comparam.ReferencedAssemblies.Add("Avro.dll"); comparam.GenerateInMemory = true; var ccp = new Microsoft.CSharp.CSharpCodeProvider(); var units = new CodeCompileUnit[] { compileUnit }; var compres = ccp.CompileAssemblyFromDom(comparam, units); if (compres == null || compres.Errors.Count > 0) { for (int i = 0; i < compres.Errors.Count; i++) Console.WriteLine(compres.Errors[i]); } if (null != compres) Assert.IsTrue(compres.Errors.Count == 0); // create record ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord; Assert.IsFalse(rec == null); // serialize var stream = new MemoryStream(); var binEncoder = new BinaryEncoder(stream); var writer = new SpecificDefaultWriter(rec.Schema); writer.Write(rec.Schema, rec, binEncoder); // deserialize stream.Position = 0; var decoder = new BinaryDecoder(stream); var reader = new SpecificDefaultReader(rec.Schema, rec.Schema); var rec2 = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder); Assert.IsFalse(rec2 == null); }