Exemplo n.º 1
0
        public override MenuItem Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType != JsonTokenType.StartObject)
            {
                throw new JsonException();
            }

            MenuButtonTypeEnum?buttonType = null;
            Type   miType                      = null;
            object sub_buttonValue             = null;
            Dictionary <string, object> values = new Dictionary <string, object>();

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    break;
                }
                if (reader.TokenType == JsonTokenType.PropertyName)
                {
                    string propertyName = reader.GetString();
                    reader.Read();
                    if ("type".Equals(propertyName, StringComparison.OrdinalIgnoreCase))
                    {
                        if (reader.TokenType == JsonTokenType.Number)
                        {
                            var typeId = reader.GetInt32();
                            buttonType = (MenuButtonTypeEnum)typeId;
                        }
                        else
                        {
                            string typeName = reader.GetString();
                            if (enumMapper.ContainsKey(typeName))
                            {
                                buttonType = enumMapper[typeName];
                            }
                        }
                    }
                    else if ("sub_button".Equals(propertyName, StringComparison.OrdinalIgnoreCase))
                    {
                        if (reader.TokenType == JsonTokenType.StartObject)
                        {
                            miType          = typeof(MenuContainerForResponse);
                            sub_buttonValue = GetPropertyValue(ref reader, typeof(MenuContainerButtons), options);
                        }
                        else if (reader.TokenType == JsonTokenType.StartArray)
                        {
                            miType          = typeof(MenuContainer);
                            sub_buttonValue = GetPropertyValue(ref reader, typeof(List <MenuItem>), options);
                        }
                        else
                        {
                            miType = typeof(MenuContainerForResponse);
                        }
                    }
                    else
                    {
                        if (propertyTypesMap.ContainsKey(propertyName))
                        {
                            values.Add(propertyName, GetPropertyValue(ref reader, propertyTypesMap[propertyName], options));
                        }
                    }
                }
            }


            if (buttonType == null)
            {
            }
            else
            {
                miType = enumToTypeMapper[buttonType.Value];
            }

            MapItem mi   = typeSerialerMapper[miType];
            var     menu = mi.Creator(values);

            if (sub_buttonValue != null)
            {
                switch (menu)
                {
                case MenuContainer mc:
                    mc.SubButtons = sub_buttonValue as List <MenuItem>;
                    break;

                case MenuContainerForResponse mcfr:
                    mcfr.SubButtons = sub_buttonValue as MenuContainerButtons;
                    break;
                }
            }
            return(menu);
        }