Ejemplo n.º 1
0
        public static string ToAvroFull(this AvroProtocol protocol)
        {
            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
                WriteAvroFull(sw, protocol);
            return(sb.ToString());
        }
Ejemplo n.º 2
0
        private static AvroProtocol ParseProtocol(JToken jToken, IDictionary <string, NamedSchema> namedTypes, Stack <string> enclosingNamespace)
        {
            switch (jToken.Type)
            {
            case JTokenType.Object:
                var keys = new HashSet <string>()
                {
                    "protocol"
                };
                var optionalKeys = new HashSet <string>()
                {
                    "namespace", "doc", "types", "messages"
                };
                JsonUtil.AssertKeys(jToken, keys, optionalKeys, out _);

                var name     = JsonUtil.GetValue <string>(jToken, "protocol");
                var protocol = new AvroProtocol(name);

                if (JsonUtil.TryGetValue <string>(jToken, "namespace", out var ns))
                {
                    protocol.Namespace = ns;
                }

                if (string.IsNullOrEmpty(protocol.Namespace))
                {
                    protocol.Namespace = enclosingNamespace.Peek();
                }

                enclosingNamespace.Push(protocol.Namespace);

                if (JsonUtil.TryGetValue <string>(jToken, "doc", out var doc))
                {
                    protocol.Doc = doc;
                }
                if (JsonUtil.TryGetValue <JArray>(jToken, "types", out var types))
                {
                    foreach (var type in ParseProtocolTypes(types, namedTypes, enclosingNamespace))
                    {
                        protocol.AddType(type);
                    }
                }
                if (JsonUtil.TryGetValue <JObject>(jToken, "messages", out var messages))
                {
                    foreach (var message in ParseMessages(messages, protocol.Types.ToDictionary(r => r.FullName), enclosingNamespace))
                    {
                        protocol.AddMessage(message);
                    }
                }

                enclosingNamespace.Pop();

                return(protocol);

            default:
                throw new AvroParseException($"Unexpected Json token: '{jToken.Type}'");
            }
        }
Ejemplo n.º 3
0
 public static void WriteAvroCanonical(TextWriter writer, AvroProtocol protocol)
 {
     Write(writer, protocol, WriterMode.Canonical);
 }
Ejemplo n.º 4
0
 public static void WriteAvroFull(TextWriter writer, AvroProtocol protocol)
 {
     Write(writer, protocol, WriterMode.Full);
 }
Ejemplo n.º 5
0
 public static void WriteAvro(TextWriter writer, AvroProtocol protocol)
 {
     Write(writer, protocol, WriterMode.None);
 }
Ejemplo n.º 6
0
        private static void Write(TextWriter writer, AvroProtocol protocol, WriterMode mode)
        {
            var namedSchemas = new HashSet <string>();

            switch (mode)
            {
            case WriterMode.Canonical:
                writer.Write(@"{");
                writer.Write(@"""protocol"":""");
                writer.Write(protocol.FullName);
                writer.Write(@"""");
                if (protocol.Types.Count() > 0)
                {
                    writer.Write(@",""types"":[");
                    WriteTypes(writer, protocol.Types, mode, protocol.Namespace, namedSchemas, true);
                    writer.Write(@"]");
                }
                if (protocol.Messages.Count() > 0)
                {
                    writer.Write(@",""messages"":{");
                    WriteMessages(writer, protocol.Messages, mode, protocol.Namespace, namedSchemas, true);
                    writer.Write(@"}");
                }
                writer.Write("}");;
                break;

            case WriterMode.Full:
                writer.Write(@"{ ");
                writer.Write(@"""namespace"": """);
                writer.Write(protocol.Namespace);
                writer.Write(@""", ");
                writer.Write(@"""protocol"": """);
                writer.Write(protocol.Name);
                writer.Write(@""", ");
                writer.Write(@"""doc"": """);
                writer.Write(protocol.Doc);
                writer.Write(@""", ");
                writer.Write(@"""types"": [");
                WriteTypes(writer, protocol.Types, mode, protocol.Namespace, namedSchemas, true);
                writer.Write(@"], ");
                writer.Write(@"""messages"": {");
                WriteMessages(writer, protocol.Messages, mode, protocol.Namespace, namedSchemas, true);
                writer.Write("}");;
                writer.Write(" }");;
                break;

            default:
                writer.Write(@"{ ");
                if (!string.IsNullOrEmpty(protocol.Namespace))
                {
                    writer.Write(@"""namespace"": """);
                    writer.Write(protocol.Namespace);
                    writer.Write(@""", ");
                }
                writer.Write(@"""protocol"": """);
                writer.Write(protocol.Name);
                writer.Write(@"""");
                if (!string.IsNullOrEmpty(protocol.Doc))
                {
                    writer.Write(@", ""doc"": """);
                    writer.Write(protocol.Doc);
                    writer.Write(@"""");
                }
                if (protocol.Types.Count() > 0)
                {
                    writer.Write(@", ""types"": [");
                    WriteTypes(writer, protocol.Types, mode, protocol.Namespace, namedSchemas, true);
                    writer.Write(@"]");
                }
                if (protocol.Messages.Count() > 0)
                {
                    writer.Write(@", ""messages"": {");
                    WriteMessages(writer, protocol.Messages, mode, protocol.Namespace, namedSchemas, true);
                    writer.Write(@"}");
                }
                writer.Write(" }");
                break;
            }
        }