Exemple #1
0
        private IHero ParseRootHero(
            JsonElement parent,
            string field)
        {
            if (!parent.TryGetProperty(field, out JsonElement obj))
            {
                return(null);
            }

            string type = obj.GetProperty(TypeName).GetString();

            if (string.Equals(type, "Droid", StringComparison.Ordinal))
            {
                var droid = new Droid();
                droid.Height  = (double?)DeserializeFloat(obj, "height");
                droid.Name    = (string)DeserializeString(obj, "name");
                droid.Friends = ParseRootHeroFriends(obj, "friends");
                return(droid);
            }

            if (string.Equals(type, "Human", StringComparison.Ordinal))
            {
                var human = new Human();
                human.Height  = (double?)DeserializeFloat(obj, "height");
                human.Name    = (string)DeserializeString(obj, "name");
                human.Friends = ParseRootHeroFriends(obj, "friends");
                return(human);
            }

            throw new UnknownSchemaTypeException(type);
        }
Exemple #2
0
        private IReadOnlyList <IHasName> ParseRootHeroFriendsNodes(
            JsonElement parent,
            string field)
        {
            if (!parent.TryGetProperty(field, out JsonElement obj))
            {
                return(null);
            }

            string type = obj.GetProperty(TypeName).GetString();

            if (string.Equals(type, "Droid", StringComparison.Ordinal))
            {
                int objLength = obj.GetArrayLength();
                var list      = new IHasName[objLength];

                for (int objIndex = 0; objIndex < objLength; objIndex++)
                {
                    JsonElement element = obj[objIndex];
                    var         entity  = new Droid();
                    entity.Name    = (string)DeserializeString(element, "name");
                    list[objIndex] = entity;
                }

                return(list);
            }

            if (string.Equals(type, "Human", StringComparison.Ordinal))
            {
                int objLength = obj.GetArrayLength();
                var list      = new IHasName[objLength];

                for (int objIndex = 0; objIndex < objLength; objIndex++)
                {
                    JsonElement element = obj[objIndex];
                    var         entity  = new Human();
                    entity.Name    = (string)DeserializeString(element, "name");
                    list[objIndex] = entity;
                }

                return(list);
            }

            throw new UnknownSchemaTypeException(type);
        }