/// <summary>
        /// Deserialize JSON into a FHIR BiologicallyDerivedProduct
        /// </summary>
        public static void DeserializeJson(this BiologicallyDerivedProduct current, ref Utf8JsonReader reader, JsonSerializerOptions options)
        {
            string propertyName;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return;
                }

                if (reader.TokenType == JsonTokenType.PropertyName)
                {
                    propertyName = reader.GetString();
                    if (Hl7.Fhir.Serialization.FhirSerializerOptions.Debug)
                    {
                        Console.WriteLine($"BiologicallyDerivedProduct >>> BiologicallyDerivedProduct.{propertyName}, depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    reader.Read();
                    current.DeserializeJsonProperty(ref reader, options, propertyName);
                }
            }

            throw new JsonException($"BiologicallyDerivedProduct: invalid state! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
        }
        /// <summary>
        /// Serialize a FHIR BiologicallyDerivedProduct into JSON
        /// </summary>
        public static void SerializeJson(this BiologicallyDerivedProduct current, Utf8JsonWriter writer, JsonSerializerOptions options, bool includeStartObject = true)
        {
            if (includeStartObject)
            {
                writer.WriteStartObject();
            }
            writer.WriteString("resourceType", "BiologicallyDerivedProduct");
            // Complex: BiologicallyDerivedProduct, Export: BiologicallyDerivedProduct, Base: DomainResource (DomainResource)
            ((Hl7.Fhir.Model.DomainResource)current).SerializeJson(writer, options, false);

            if ((current.Identifier != null) && (current.Identifier.Count != 0))
            {
                writer.WritePropertyName("identifier");
                writer.WriteStartArray();
                foreach (Identifier val in current.Identifier)
                {
                    val.SerializeJson(writer, options, true);
                }
                writer.WriteEndArray();
            }

            if (current.ProductCategoryElement != null)
            {
                if (current.ProductCategoryElement.Value != null)
                {
                    writer.WriteString("productCategory", Hl7.Fhir.Utility.EnumUtility.GetLiteral(current.ProductCategoryElement.Value));
                }
                if (current.ProductCategoryElement.HasExtensions() || (!string.IsNullOrEmpty(current.ProductCategoryElement.ElementId)))
                {
                    JsonStreamUtilities.SerializeExtensionList(writer, options, "_productCategory", false, current.ProductCategoryElement.Extension, current.ProductCategoryElement.ElementId);
                }
            }

            if (current.ProductCode != null)
            {
                writer.WritePropertyName("productCode");
                current.ProductCode.SerializeJson(writer, options);
            }

            if (current.StatusElement != null)
            {
                if (current.StatusElement.Value != null)
                {
                    writer.WriteString("status", Hl7.Fhir.Utility.EnumUtility.GetLiteral(current.StatusElement.Value));
                }
                if (current.StatusElement.HasExtensions() || (!string.IsNullOrEmpty(current.StatusElement.ElementId)))
                {
                    JsonStreamUtilities.SerializeExtensionList(writer, options, "_status", false, current.StatusElement.Extension, current.StatusElement.ElementId);
                }
            }

            if ((current.Request != null) && (current.Request.Count != 0))
            {
                writer.WritePropertyName("request");
                writer.WriteStartArray();
                foreach (ResourceReference val in current.Request)
                {
                    val.SerializeJson(writer, options, true);
                }
                writer.WriteEndArray();
            }

            if (current.QuantityElement != null)
            {
                if (current.QuantityElement.Value != null)
                {
                    writer.WriteNumber("quantity", (int)current.QuantityElement.Value);
                }
                if (current.QuantityElement.HasExtensions() || (!string.IsNullOrEmpty(current.QuantityElement.ElementId)))
                {
                    JsonStreamUtilities.SerializeExtensionList(writer, options, "_quantity", false, current.QuantityElement.Extension, current.QuantityElement.ElementId);
                }
            }

            if ((current.Parent != null) && (current.Parent.Count != 0))
            {
                writer.WritePropertyName("parent");
                writer.WriteStartArray();
                foreach (ResourceReference val in current.Parent)
                {
                    val.SerializeJson(writer, options, true);
                }
                writer.WriteEndArray();
            }

            if (current.Collection != null)
            {
                writer.WritePropertyName("collection");
                current.Collection.SerializeJson(writer, options);
            }

            if ((current.Processing != null) && (current.Processing.Count != 0))
            {
                writer.WritePropertyName("processing");
                writer.WriteStartArray();
                foreach (BiologicallyDerivedProduct.ProcessingComponent val in current.Processing)
                {
                    val.SerializeJson(writer, options, true);
                }
                writer.WriteEndArray();
            }

            if (current.Manipulation != null)
            {
                writer.WritePropertyName("manipulation");
                current.Manipulation.SerializeJson(writer, options);
            }

            if ((current.Storage != null) && (current.Storage.Count != 0))
            {
                writer.WritePropertyName("storage");
                writer.WriteStartArray();
                foreach (BiologicallyDerivedProduct.StorageComponent val in current.Storage)
                {
                    val.SerializeJson(writer, options, true);
                }
                writer.WriteEndArray();
            }

            if (includeStartObject)
            {
                writer.WriteEndObject();
            }
        }
        /// <summary>
        /// Deserialize JSON into a FHIR BiologicallyDerivedProduct
        /// </summary>
        public static void DeserializeJsonProperty(this BiologicallyDerivedProduct current, ref Utf8JsonReader reader, JsonSerializerOptions options, string propertyName)
        {
            switch (propertyName)
            {
            case "identifier":
                if ((reader.TokenType != JsonTokenType.StartArray) || (!reader.Read()))
                {
                    throw new JsonException($"BiologicallyDerivedProduct error reading 'identifier' expected StartArray, found {reader.TokenType}! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                }

                current.Identifier = new List <Identifier>();

                while (reader.TokenType != JsonTokenType.EndArray)
                {
                    Hl7.Fhir.Model.Identifier v_Identifier = new Hl7.Fhir.Model.Identifier();
                    v_Identifier.DeserializeJson(ref reader, options);
                    current.Identifier.Add(v_Identifier);

                    if (!reader.Read())
                    {
                        throw new JsonException($"BiologicallyDerivedProduct error reading 'identifier' array, read failed! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    if (reader.TokenType == JsonTokenType.EndObject)
                    {
                        reader.Read();
                    }
                }

                if (current.Identifier.Count == 0)
                {
                    current.Identifier = null;
                }
                break;

            case "productCategory":
                if (reader.TokenType == JsonTokenType.Null)
                {
                    current.ProductCategoryElement = new Code <Hl7.Fhir.Model.BiologicallyDerivedProduct.BiologicallyDerivedProductCategory>();
                    reader.Skip();
                }
                else
                {
                    current.ProductCategoryElement = new Code <Hl7.Fhir.Model.BiologicallyDerivedProduct.BiologicallyDerivedProductCategory>(Hl7.Fhir.Utility.EnumUtility.ParseLiteral <Hl7.Fhir.Model.BiologicallyDerivedProduct.BiologicallyDerivedProductCategory>(reader.GetString()));
                }
                break;

            case "_productCategory":
                if (current.ProductCategoryElement == null)
                {
                    current.ProductCategoryElement = new Code <Hl7.Fhir.Model.BiologicallyDerivedProduct.BiologicallyDerivedProductCategory>();
                }
                ((Hl7.Fhir.Model.Element)current.ProductCategoryElement).DeserializeJson(ref reader, options);
                break;

            case "productCode":
                current.ProductCode = new Hl7.Fhir.Model.CodeableConcept();
                ((Hl7.Fhir.Model.CodeableConcept)current.ProductCode).DeserializeJson(ref reader, options);
                break;

            case "status":
                if (reader.TokenType == JsonTokenType.Null)
                {
                    current.StatusElement = new Code <Hl7.Fhir.Model.BiologicallyDerivedProduct.BiologicallyDerivedProductStatus>();
                    reader.Skip();
                }
                else
                {
                    current.StatusElement = new Code <Hl7.Fhir.Model.BiologicallyDerivedProduct.BiologicallyDerivedProductStatus>(Hl7.Fhir.Utility.EnumUtility.ParseLiteral <Hl7.Fhir.Model.BiologicallyDerivedProduct.BiologicallyDerivedProductStatus>(reader.GetString()));
                }
                break;

            case "_status":
                if (current.StatusElement == null)
                {
                    current.StatusElement = new Code <Hl7.Fhir.Model.BiologicallyDerivedProduct.BiologicallyDerivedProductStatus>();
                }
                ((Hl7.Fhir.Model.Element)current.StatusElement).DeserializeJson(ref reader, options);
                break;

            case "request":
                if ((reader.TokenType != JsonTokenType.StartArray) || (!reader.Read()))
                {
                    throw new JsonException($"BiologicallyDerivedProduct error reading 'request' expected StartArray, found {reader.TokenType}! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                }

                current.Request = new List <ResourceReference>();

                while (reader.TokenType != JsonTokenType.EndArray)
                {
                    Hl7.Fhir.Model.ResourceReference v_Request = new Hl7.Fhir.Model.ResourceReference();
                    v_Request.DeserializeJson(ref reader, options);
                    current.Request.Add(v_Request);

                    if (!reader.Read())
                    {
                        throw new JsonException($"BiologicallyDerivedProduct error reading 'request' array, read failed! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    if (reader.TokenType == JsonTokenType.EndObject)
                    {
                        reader.Read();
                    }
                }

                if (current.Request.Count == 0)
                {
                    current.Request = null;
                }
                break;

            case "quantity":
                if (reader.TokenType == JsonTokenType.Null)
                {
                    current.QuantityElement = new Integer();
                    reader.Skip();
                }
                else
                {
                    current.QuantityElement = new Integer(reader.GetInt32());
                }
                break;

            case "_quantity":
                if (current.QuantityElement == null)
                {
                    current.QuantityElement = new Integer();
                }
                ((Hl7.Fhir.Model.Element)current.QuantityElement).DeserializeJson(ref reader, options);
                break;

            case "parent":
                if ((reader.TokenType != JsonTokenType.StartArray) || (!reader.Read()))
                {
                    throw new JsonException($"BiologicallyDerivedProduct error reading 'parent' expected StartArray, found {reader.TokenType}! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                }

                current.Parent = new List <ResourceReference>();

                while (reader.TokenType != JsonTokenType.EndArray)
                {
                    Hl7.Fhir.Model.ResourceReference v_Parent = new Hl7.Fhir.Model.ResourceReference();
                    v_Parent.DeserializeJson(ref reader, options);
                    current.Parent.Add(v_Parent);

                    if (!reader.Read())
                    {
                        throw new JsonException($"BiologicallyDerivedProduct error reading 'parent' array, read failed! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    if (reader.TokenType == JsonTokenType.EndObject)
                    {
                        reader.Read();
                    }
                }

                if (current.Parent.Count == 0)
                {
                    current.Parent = null;
                }
                break;

            case "collection":
                current.Collection = new Hl7.Fhir.Model.BiologicallyDerivedProduct.CollectionComponent();
                ((Hl7.Fhir.Model.BiologicallyDerivedProduct.CollectionComponent)current.Collection).DeserializeJson(ref reader, options);
                break;

            case "processing":
                if ((reader.TokenType != JsonTokenType.StartArray) || (!reader.Read()))
                {
                    throw new JsonException($"BiologicallyDerivedProduct error reading 'processing' expected StartArray, found {reader.TokenType}! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                }

                current.Processing = new List <BiologicallyDerivedProduct.ProcessingComponent>();

                while (reader.TokenType != JsonTokenType.EndArray)
                {
                    Hl7.Fhir.Model.BiologicallyDerivedProduct.ProcessingComponent v_Processing = new Hl7.Fhir.Model.BiologicallyDerivedProduct.ProcessingComponent();
                    v_Processing.DeserializeJson(ref reader, options);
                    current.Processing.Add(v_Processing);

                    if (!reader.Read())
                    {
                        throw new JsonException($"BiologicallyDerivedProduct error reading 'processing' array, read failed! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    if (reader.TokenType == JsonTokenType.EndObject)
                    {
                        reader.Read();
                    }
                }

                if (current.Processing.Count == 0)
                {
                    current.Processing = null;
                }
                break;

            case "manipulation":
                current.Manipulation = new Hl7.Fhir.Model.BiologicallyDerivedProduct.ManipulationComponent();
                ((Hl7.Fhir.Model.BiologicallyDerivedProduct.ManipulationComponent)current.Manipulation).DeserializeJson(ref reader, options);
                break;

            case "storage":
                if ((reader.TokenType != JsonTokenType.StartArray) || (!reader.Read()))
                {
                    throw new JsonException($"BiologicallyDerivedProduct error reading 'storage' expected StartArray, found {reader.TokenType}! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                }

                current.Storage = new List <BiologicallyDerivedProduct.StorageComponent>();

                while (reader.TokenType != JsonTokenType.EndArray)
                {
                    Hl7.Fhir.Model.BiologicallyDerivedProduct.StorageComponent v_Storage = new Hl7.Fhir.Model.BiologicallyDerivedProduct.StorageComponent();
                    v_Storage.DeserializeJson(ref reader, options);
                    current.Storage.Add(v_Storage);

                    if (!reader.Read())
                    {
                        throw new JsonException($"BiologicallyDerivedProduct error reading 'storage' array, read failed! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    if (reader.TokenType == JsonTokenType.EndObject)
                    {
                        reader.Read();
                    }
                }

                if (current.Storage.Count == 0)
                {
                    current.Storage = null;
                }
                break;

            // Complex: BiologicallyDerivedProduct, Export: BiologicallyDerivedProduct, Base: DomainResource
            default:
                ((Hl7.Fhir.Model.DomainResource)current).DeserializeJsonProperty(ref reader, options, propertyName);
                break;
            }
        }