Example #1
0
        internal static Shark DeserializeShark(JsonElement element)
        {
            if (element.TryGetProperty("fishtype", out JsonElement discriminator))
            {
                switch (discriminator.GetString())
                {
                case "cookiecuttershark": return(Cookiecuttershark.DeserializeCookiecuttershark(element));

                case "goblin": return(Goblinshark.DeserializeGoblinshark(element));

                case "sawshark": return(Sawshark.DeserializeSawshark(element));
                }
            }
            int?           age      = default;
            DateTimeOffset birthday = default;
            string         fishtype = default;
            string         species  = default;
            float          length   = default;
            IList <Fish>   siblings = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("age"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    age = property.Value.GetInt32();
                    continue;
                }
                if (property.NameEquals("birthday"))
                {
                    birthday = property.Value.GetDateTimeOffset("O");
                    continue;
                }
                if (property.NameEquals("fishtype"))
                {
                    fishtype = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("species"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    species = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("length"))
                {
                    length = property.Value.GetSingle();
                    continue;
                }
                if (property.NameEquals("siblings"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List <Fish> array = new List <Fish>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(DeserializeFish(item));
                        }
                    }
                    siblings = array;
                    continue;
                }
            }
            return(new Shark(fishtype, species, length, siblings, age, birthday));
        }
        internal static Fish DeserializeFish(JsonElement element)
        {
            if (element.TryGetProperty("fishtype", out JsonElement discriminator))
            {
                switch (discriminator.GetString())
                {
                case "cookiecuttershark": return(Cookiecuttershark.DeserializeCookiecuttershark(element));

                case "goblin": return(Goblinshark.DeserializeGoblinshark(element));

                case "salmon": return(Salmon.DeserializeSalmon(element));

                case "sawshark": return(Sawshark.DeserializeSawshark(element));

                case "shark": return(Shark.DeserializeShark(element));

                case "smart_salmon": return(SmartSalmon.DeserializeSmartSalmon(element));
                }
            }
            string       fishtype = default;
            string       species  = default;
            float        length   = default;
            IList <Fish> siblings = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("fishtype"))
                {
                    fishtype = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("species"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    species = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("length"))
                {
                    length = property.Value.GetSingle();
                    continue;
                }
                if (property.NameEquals("siblings"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List <Fish> array = new List <Fish>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(DeserializeFish(item));
                        }
                    }
                    siblings = array;
                    continue;
                }
            }
            return(new Fish(fishtype, species, length, siblings));
        }