public override Plane Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } if (!reader.Read() || reader.TokenType != JsonTokenType.PropertyName || reader.GetString() != "TypeDiscriminator") { throw new JsonException(); } if (!reader.Read() || reader.TokenType != JsonTokenType.Number) { throw new JsonException(); } Plane baseClass; TypeDiscriminator typeDiscriminator = (TypeDiscriminator)reader.GetInt32(); switch (typeDiscriminator) { case TypeDiscriminator.Cargo: if (!reader.Read() || reader.GetString() != "Cargo") { throw new JsonException(); } if (!reader.Read() || reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } baseClass = (Cargo)JsonSerializer.Deserialize(ref reader, typeof(Cargo)); break; case TypeDiscriminator.Passenger: if (!reader.Read() || reader.GetString() != "Passenger") { throw new JsonException(); } if (!reader.Read() || reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } baseClass = (Passenger)JsonSerializer.Deserialize(ref reader, typeof(Passenger)); break; default: throw new NotSupportedException(); } if (!reader.Read() || reader.TokenType != JsonTokenType.EndObject) { throw new JsonException(); } return(baseClass); }
public void RegisterAllTree() { TypeDiscriminatorFactory fac = new TypeDiscriminatorFactory(); // Register from Base fac.RegisterTree <BaseDao>(); TypeDiscriminator dis = fac.FromType <DocumentDao>(); Assert.Equal(2, dis.Inclusions.Count()); Assert.Single(dis.Exclusions); }
public void RegisterGeneric() { TypeDiscriminatorFactory fac = new TypeDiscriminatorFactory(); fac.RegisterTree(typeof(FileDvo <>)); fac.Register(typeof(BaseDvo <>)); TypeDiscriminator dis = fac.FromType(typeof(DocumentDvo <>)); Assert.Equal(2, dis.Inclusions.Count()); Assert.Single(dis.Exclusions); }
public void RegisterDisabgleState() { TypeDiscriminatorFactory fac = new TypeDiscriminatorFactory(); fac.RegisterTree <BaseDao>(); TypeDiscriminator dis = fac.FromType <FileDao>(); Assert.Equal(3, dis.Inclusions.Count()); Assert.Single(dis.Exclusions); dis = fac.FromType <BaseDao>(); Assert.Equal(7, dis.Inclusions.Count()); Assert.Empty(dis.Exclusions); }
public void ManyClassesSameDiscriminators() { TypeDiscriminatorFactory fac = new TypeDiscriminatorFactory { AllowMoreThanOneTypeByDiscriminator = true }; // Register from Base fac.RegisterTree(typeof(BaseDvo <>)); TypeDiscriminator d2 = fac.FromType(typeof(BaseDvo <>)); Assert.Single(d2.Inclusions.Where(d => d.Name == "Person")); }
public void Create() { TypeDiscriminatorFactory fac = new TypeDiscriminatorFactory { GetIdFunction = (type, att) => att?.Id ?? type.Name, GetNameFunction = (type, att) => att?.Name ?? type.Name.ToUpper() }; fac.RegisterTree <BaseDao>(typeof(BaseDao).Assembly.DefinedTypes.ToArray()); TypeDiscriminator dis = fac.FromType <DocumentDao>(); Assert.Equal(Helpers.TypeDiscriminatorIds.Document, dis.Id); Assert.Equal(Helpers.TypeDiscriminatorIds.Document, dis.Name); Assert.Equal(TypeDiscriminator.TypeDiscriminatorId, dis.TypeKey); Assert.Equal(fac.DiscriminatorTypeName, dis.TypeName); Assert.Equal(2, dis.Inclusions.Count()); Assert.Single(dis.Exclusions); }
public void NewTest() { TypeDiscriminatorFactory fac = new TypeDiscriminatorFactory(); fac.Register(typeof(BaseDao)); fac.Register(typeof(LocationDao)); fac.Register(typeof(CityDao)); fac.Register(typeof(CountryDao)); TypeDiscriminator dao = fac.FromType <BaseDao>(); fac.Reset(); fac.Register(typeof(BaseDao)); fac.Register(typeof(FileDao)); fac.Register(typeof(DocumentDao)); fac.Register(typeof(WordDocumentDao)); dao = fac.FromType <BaseDao>(); }
public void RegisterFileTree() { TypeDiscriminatorFactory fac = new TypeDiscriminatorFactory(); // Register from File fac.RegisterTree <FileDao>(); TypeDiscriminator dis = null; Assert.Throws <TypeDiscriminatorRegistrationValidationException>(() => { dis = fac.FromType <DocumentDao>(); }); fac.Reset(); fac.RegisterTree <FileDao>(); fac.Register <BaseDao>(); dis = fac.FromType <DocumentDao>(); Assert.Equal(2, dis.Inclusions.Count()); Assert.Single(dis.Exclusions); }
public void Equality() { TypeDiscriminatorFactory fac = new TypeDiscriminatorFactory { AllowMoreThanOneTypeByDiscriminator = true }; // Register from Base fac.RegisterTree <BaseDao>(); fac.RegisterTree(typeof(BaseDvo <>)); TypeDiscriminator d1 = fac.FromType <FileDao>(); TypeDiscriminator d2 = fac.FromType(typeof(FileDvo <>)); Assert.True(d1 == d2); Assert.Equal(d1, d2); Assert.Same(d1, d2); TypeDiscriminator[] d1c = new[] { d1 }; Assert.Contains(d2, d1c); }
// <ReadMethod> public override Person Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { Utf8JsonReader readerClone = reader; if (readerClone.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } readerClone.Read(); if (readerClone.TokenType != JsonTokenType.PropertyName) { throw new JsonException(); } string?propertyName = readerClone.GetString(); if (propertyName != "TypeDiscriminator") { throw new JsonException(); } readerClone.Read(); if (readerClone.TokenType != JsonTokenType.Number) { throw new JsonException(); } TypeDiscriminator typeDiscriminator = (TypeDiscriminator)readerClone.GetInt32(); Person person = typeDiscriminator switch { TypeDiscriminator.Customer => JsonSerializer.Deserialize <Customer>(ref reader) !, TypeDiscriminator.Employee => JsonSerializer.Deserialize <Employee>(ref reader) !, _ => throw new JsonException() }; return(person); }
public override MediaDTO Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); // makes sure it's reading from the start } reader.Read(); if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException(); // makes sure it has begun with the property } string propertyName = reader.GetString(); if (propertyName != "TypeDiscriminator") { throw new JsonException(); // makes sure it is a type discriminator } TypeDiscriminator typeDiscriminator = (TypeDiscriminator)reader.GetInt32(); MediaDTO mediaDTO = typeDiscriminator switch { TypeDiscriminator.Picture => new PictureDTO(), TypeDiscriminator.Model3D => new Model3DDTO(), TypeDiscriminator.Music => new MusicDTO(), _ => throw new JsonException() }; // the most beautiful syntax I've ever seen, good lord! while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { return(mediaDTO); // returns the newly created cast object } } throw new JsonException(); // if it fails for any other reason }
public void RegisterOnlyttttFile() { TypeDiscriminatorFactory fac = new TypeDiscriminatorFactory { AllowMoreThanOneTypeByDiscriminator = true }; // Register from generic type BaseDvo<> fac.RegisterTree(typeof(BaseDvo <>)); TypeDiscriminator dis = fac.FromType(typeof(LocationDvo <>)); Assert.Equal(3, dis.Inclusions.Count()); Assert.Single(dis.Exclusions); fac.Reset(); fac.AllowMoreThanOneTypeByDiscriminator = false; // Register from generic type LocationDvo<> fac.RegisterTree(typeof(LocationDvo <>)); dis = fac.FromType <CityDvo>(); Assert.Empty(dis.Inclusions); Assert.Single(dis.Exclusions); fac.Reset(); }
public void TypeDiscriminatorAttribute() { TypeDiscriminatorFactory fac = new TypeDiscriminatorFactory(); fac.Register(typeof(BaseDao)); fac.Register(typeof(LocationDao)); fac.Register(typeof(CityDao)); fac.Register(typeof(CountryDao)); TypeDiscriminator dao = fac.FromType <CityDao>(); fac.Reset(); fac.Register(typeof(BaseDto)); fac.Register(typeof(LocationDto)); fac.Register(typeof(CityDto)); fac.Register(typeof(CountryDto)); TypeDiscriminator dto = fac.FromType <CityDto>(); Assert.True(dao.Id == dto.Id, $"Type discriminators DAO & DTO must have same Id. Values are '{dao.Id}' and '{dto.Id}'"); fac.ClearRegistrations(); fac.Register(typeof(BaseDvo <>)); fac.Register(typeof(LocationDvo <>)); fac.Register(typeof(CityDvo)); fac.Register(typeof(CountryDvo)); TypeDiscriminator dvo = fac.FromId(TypeDiscriminatorIds.City); TypeDiscriminator dvo2 = fac.FromId(TypeDiscriminatorIds.Location); TypeDiscriminator dvo3 = fac.FromType(typeof(LocationDvo <>)); Assert.True(dao.Id == dvo.Id, $"Type discriminators DAO & DVO must have same Id. Values are '{dao.Id}' and '{dvo.Id}'"); Assert.True(dvo2.Id == dvo3.Id, $"Type discriminators DAO & DVO must have same Id. Values are '{dvo2.Id}' and '{dvo3.Id}'"); Assert.True(dvo2.Inclusions.Contains(dvo), $"Type discriminator 'Location' must include 'City'"); }
public override Person Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } reader.Read(); if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException(); } string?propertyName = reader.GetString(); if (propertyName != "TypeDiscriminator") { throw new JsonException(); } reader.Read(); if (reader.TokenType != JsonTokenType.Number) { throw new JsonException(); } TypeDiscriminator typeDiscriminator = (TypeDiscriminator)reader.GetInt32(); Person person = typeDiscriminator switch { TypeDiscriminator.Customer => new Customer(), TypeDiscriminator.Employee => new Employee(), _ => throw new JsonException() }; while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { return(person); } if (reader.TokenType == JsonTokenType.PropertyName) { propertyName = reader.GetString(); reader.Read(); switch (propertyName) { case "CreditLimit": decimal creditLimit = reader.GetDecimal(); ((Customer)person).CreditLimit = creditLimit; break; case "OfficeNumber": string?officeNumber = reader.GetString(); ((Employee)person).OfficeNumber = officeNumber; break; case "Name": string?name = reader.GetString(); person.Name = name; break; } } } throw new JsonException(); }
public static DialogueNode Read(ref Utf8JsonReader reader) { /* * * This first if block is checking to see if the reader has yet to read anything. This method can be called recursively * for nested json structures. Its called recursively with the reference to the same reader - a new one is not creates. * If the reader is already in the middle of the json structure, this block won't execute. * This really just "initializes" the reader to point to the first Token in the json structure rather than nothing. * */ if (reader.TokenType == JsonTokenType.None) { reader.Read(); } if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } reader.Read(); if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException(); } string propertyForType = reader.GetString(); if (propertyForType != "Type") { throw new JsonException(); } reader.Read(); if (reader.TokenType != JsonTokenType.Number) { throw new JsonException(); } TypeDiscriminator typeDiscriminator = (TypeDiscriminator)reader.GetInt32(); DialogueNode dialogueNode = typeDiscriminator switch { TypeDiscriminator.DialogueLine => new DialogueLine(), TypeDiscriminator.DialoguePrompt => new DialoguePrompt(), _ => throw new JsonException() }; while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { return(dialogueNode); } if (reader.TokenType == JsonTokenType.PropertyName) { string propertyName = reader.GetString(); reader.Read(); switch (propertyName) { case "SpeakerName": string speakerName = reader.GetString(); dialogueNode.SpeakerName = speakerName; break; case "Emotion": string emotion = reader.GetString(); dialogueNode.Emotion = emotion; break; case "Text": string text = reader.GetString(); dialogueNode.Text = text; break; case "NextDialogueNode": ((DialogueLine)dialogueNode).NextDialogueNode = Read(ref reader); break; case "DialogueOptions": if (reader.TokenType != JsonTokenType.StartArray) { throw new JsonException(); } ((DialoguePrompt)dialogueNode).Options = new List <DialogueOption>(); reader.Read(); while (reader.TokenType != JsonTokenType.EndArray) { if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } var dialogueOption = new DialogueOption(); while (reader.TokenType != JsonTokenType.EndObject) { reader.Read(); if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException(); } string dialogueOptionPropertyName = reader.GetString(); reader.Read(); switch (dialogueOptionPropertyName) { case "Text": dialogueOption.Text = reader.GetString(); break; case "ResponseDialogueNode": dialogueOption.ResponseDialogueNode = Read(ref reader); break; } } reader.Read(); reader.Read(); ((DialoguePrompt)dialogueNode).Options.Add(dialogueOption); } break; } } } throw new JsonException(); } }
public override Person Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } reader.Read(); if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException(); } string propertyName = reader.GetString(); if (propertyName != "TypeDiscriminator") { throw new JsonException(); } reader.Read(); if (reader.TokenType != JsonTokenType.Number) { throw new JsonException(); } Person value; TypeDiscriminator typeDiscriminator = (TypeDiscriminator)reader.GetInt32(); switch (typeDiscriminator) { case TypeDiscriminator.Customer: value = new Customer(); break; case TypeDiscriminator.Employee: value = new Employee(); break; default: throw new JsonException(); } while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { return(value); } if (reader.TokenType == JsonTokenType.PropertyName) { propertyName = reader.GetString(); reader.Read(); switch (propertyName) { case "CreditLimit": decimal creditLimit = reader.GetDecimal(); ((Customer)value).CreditLimit = creditLimit; break; case "OfficeNumber": string officeNumber = reader.GetString(); ((Employee)value).OfficeNumber = officeNumber; break; case "Name": string name = reader.GetString(); value.Name = name; break; } } } throw new JsonException(); }
public static bool IsClassType(this TypeDiscriminator type) { return(type == TypeDiscriminator.Model || type == TypeDiscriminator.Object); }
public static bool IsCustomType(this TypeDiscriminator type) { return(type == TypeDiscriminator.Enum || type.IsClassType()); }
public static bool ByAny(this IRolCan me, TypeDiscriminator typeDiscriminator, params IDiscriminator[] discriminators) { using (var res = Printer.CallResult <bool>()) return(res.Value = ((IInternalRolCan)me).CheckDiscriminators(false, typeDiscriminator, discriminators)); }
public override Person Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } reader.Read(); if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException(); } string propertyName = reader.GetString(); if (propertyName != "TypeDiscriminator") { throw new JsonException(); } reader.Read(); if (reader.TokenType != JsonTokenType.Number) { throw new JsonException(); } TypeDiscriminator typeDiscriminator = (TypeDiscriminator)reader.GetInt32(); Person person = typeDiscriminator switch { TypeDiscriminator.Student => new Student(), TypeDiscriminator.Teacher => new Teacher(), TypeDiscriminator.Extern => new Extern(), _ => throw new JsonException() }; while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { return(person); } if (reader.TokenType == JsonTokenType.PropertyName) { propertyName = reader.GetString(); reader.Read(); switch (propertyName) { case "Student_ID": string id = reader.GetString(); ((Student)person).Student_ID = id; break; case "FirstName": string fname = reader.GetString(); person.FirstName = fname; break; case "LastName": string lname = reader.GetString(); person.LastName = lname; break; case "Age": int age = reader.GetInt32(); person.Age = age; break; case "Gender": int gender = reader.GetInt32(); person.Gender = (Constants.Gender)gender; break; case "UUID": string uuid = reader.GetString(); person.UUID = uuid; break; } } } throw new JsonException(); }
public override Person Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } reader.Read(); if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException(); } string propertyName = reader.GetString(); if (propertyName != "TypeDiscriminator") { throw new JsonException(); } reader.Read(); if (reader.TokenType != JsonTokenType.Number) { throw new JsonException(); } TypeDiscriminator typeDiscriminator = (TypeDiscriminator)reader.GetInt32(); Person person = typeDiscriminator switch { TypeDiscriminator.Person => new Person(), TypeDiscriminator.Doctor => new Doctor(), TypeDiscriminator.Student => new Student(), _ => throw new JsonException() }; while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { return(person); } if (reader.TokenType == JsonTokenType.PropertyName) { propertyName = reader.GetString(); reader.Read(); switch (propertyName.ToUpper()) { case "HOSPITALNAME": string hname = reader.GetString(); ((Doctor)person).HospitalName = hname; break; case "SCHOOLNAME": string sname = reader.GetString(); ((Student)person).SchoolName = sname; break; case "FIRSTNAME": string fname = reader.GetString(); person.FirstName = fname; break; case "LASTNAME": string lname = reader.GetString(); person.LastName = lname; break; } } } throw new JsonException(); }