Exemple #1
0
 /// <summary>
 /// Serializes the specified object instance.
 /// </summary>
 /// <param name="instances">The objects to serialize.</param>
 /// <returns>The serialized JSON string.</returns>
 public static string Serialize(params IAvroRecord[] instances)
 {
     using (var writer = new StringWriter())
     {
         using (var encoder = new JsonAvroEncoder(writer))
         {
             if (instances.Length == 1)
             {
                 instances[0].Encode(encoder);
             }
             else
             {
                 encoder.EncodeArrayStart(instances.Length, instances.Length);
                 var separator = false;
                 foreach (var instance in instances)
                 {
                     if (separator)
                     {
                         encoder.EncodeArrayItemSeparator();
                     }
                     instance.Encode(encoder);
                     separator = true;
                 }
                 encoder.EncodeArrayEnd();
             }
         }
         return(writer.ToString());
     }
 }
Exemple #2
0
        private void HandleServerCapabilitiesRequest(HttpListenerResponse response, IDictionary <string, string> headers)
        {
            if (headers.ContainsKey(EtpHeaders.GetVersions) && string.Equals(headers[EtpHeaders.GetVersions], "true", StringComparison.OrdinalIgnoreCase))
            {
                response.ContentType = "application/json";
                using (var writer = new StringWriter())
                {
                    using (var encoder = new JsonAvroEncoder(writer))
                    {
                        encoder.EncodeArrayStart(Details.SupportedVersions.Count, Details.SupportedVersions.Count);
                        var separator = false;
                        for (int i = 0; i < Details.SupportedVersions.Count; i++)
                        {
                            if (separator)
                            {
                                encoder.EncodeArrayItemSeparator();
                            }
                            encoder.EncodeString(Details.SupportedVersions[i].ToString());
                        }
                        encoder.EncodeArrayEnd();
                    }
                    var bytes = System.Text.Encoding.UTF8.GetBytes(writer.ToString());
                    response.ContentLength64 = bytes.Length;
                    response.OutputStream.Write(bytes, 0, bytes.Length);
                }
            }
            else
            {
                var version = EtpVersion.v11;
                if (headers.ContainsKey(EtpHeaders.GetVersion) && string.Equals(headers[EtpHeaders.GetVersion], EtpSubProtocols.v12, StringComparison.OrdinalIgnoreCase))
                {
                    version = EtpVersion.v12;
                }

                var serverCapabilities = ServerManager.ServerCapabilities(version);
                using (var writer = new StringWriter())
                {
                    using (var encoder = new JsonAvroEncoder(writer))
                    {
                        serverCapabilities.Encode(encoder);
                    }

                    response.ContentType = "application/json";
                    var @string = writer.ToString();
                    var bytes   = System.Text.Encoding.UTF8.GetBytes(@string);
                    response.ContentLength64 = bytes.Length;
                    response.OutputStream.Write(bytes, 0, bytes.Length);
                }
            }
        }