public void AssertValues() { var jToken = JToken.Parse(@"{""num"":""1"",""happy"":""face""}"); Assert.DoesNotThrow( () => JsonUtil.AssertValue(jToken, "num", "1") ); Assert.DoesNotThrow( () => JsonUtil.AssertValue(jToken, "happy", "face") ); Assert.DoesNotThrow( () => JsonUtil.AssertValues(jToken, "happy", "face", "X", "Y") ); Assert.Throws( typeof(ArgumentException), () => JsonUtil.AssertValue(jToken, "num", "X") ); Assert.Throws( typeof(ArgumentException), () => JsonUtil.AssertValue(jToken, "q2345", "X") ); Assert.Throws( typeof(ArgumentException), () => JsonUtil.AssertValues(jToken, "num", "face", "X", "Y") ); }
private static AvroSchema ParseRecordSchema(JToken jToken, IDictionary <string, NamedSchema> namedTypes, Stack <string> enclosingNamespace) { var keys = new HashSet <string>() { "type", "name", "fields" }; var optionalKeys = new HashSet <string>() { "namespace", "aliases", "doc" }; JsonUtil.AssertKeys(jToken, keys, optionalKeys, out var tags); JsonUtil.AssertValues(jToken, "type", "record", "error"); var type = JsonUtil.GetValue <string>(jToken, "type"); var name = JsonUtil.GetValue <string>(jToken, "name"); var fields = JsonUtil.GetValue <JArray>(jToken, "fields"); var recordSchema = new RecordSchema(name); if (type.ToString() == "error") { recordSchema = new ErrorSchema(name); } recordSchema.AddTags(tags); if (JsonUtil.TryGetValue <string>(jToken, "namespace", out var ns)) { recordSchema.Namespace = ns; } if (string.IsNullOrEmpty(recordSchema.Namespace)) { recordSchema.Namespace = enclosingNamespace.Peek(); } enclosingNamespace.Push(recordSchema.Namespace); if (JsonUtil.TryGetValue <JArray>(jToken, "aliases", out var aliases)) { recordSchema.Aliases = aliases.Values <string>().ToArray(); } if (JsonUtil.TryGetValue <string>(jToken, "doc", out var doc)) { recordSchema.Doc = doc; } namedTypes.Add(recordSchema.FullName, recordSchema); foreach (var field in ParseRecordFieldSchema(fields, namedTypes, enclosingNamespace)) { recordSchema.Add(field); } enclosingNamespace.Pop(); return(recordSchema); }