public static string ToAvroFull(this AvroProtocol protocol) { var sb = new StringBuilder(); using (var sw = new StringWriter(sb)) WriteAvroFull(sw, protocol); return(sb.ToString()); }
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}'"); } }
public static void WriteAvroCanonical(TextWriter writer, AvroProtocol protocol) { Write(writer, protocol, WriterMode.Canonical); }
public static void WriteAvroFull(TextWriter writer, AvroProtocol protocol) { Write(writer, protocol, WriterMode.Full); }
public static void WriteAvro(TextWriter writer, AvroProtocol protocol) { Write(writer, protocol, WriterMode.None); }
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; } }