コード例 #1
0
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var jsonProp = base.CreateProperty(member, memberSerialization);

            void Skip()
            {
                jsonProp.ShouldSerialize   = o => false;
                jsonProp.ShouldDeserialize = o => false;
            }

            void Allow()
            {
                jsonProp.ShouldSerialize   = o => true;
                jsonProp.ShouldDeserialize = o => true;
            }

            if (member is FieldInfo field)
            {
                jsonProp.Readable = true;
                jsonProp.Writable = true;
                if (JsonBlueprints.IsBlacklisted(field))
                {
                    Skip();
                    return(null);
                }
                //BodyPartTypes used by EquipmentEntities are stored as longs with a LongAsEnumAttribute
                if (field.FieldType == typeof(long))
                {
                    var attribute = field.GetCustomAttribute <LongAsEnumAttribute>();
                    if (attribute != null)
                    {
                        jsonProp.ValueProvider   = new LongAsEnumValueProvider(field, attribute.EnumType);
                        jsonProp.PropertyType    = attribute.EnumType;
                        jsonProp.MemberConverter = stringEnumConverter;
                        jsonProp.Converter       = stringEnumConverter;
                    }
                }
                //BlueprintSettingsRoot contains Action and Func fields
                if (field.FieldType == typeof(System.Action) || field.FieldType == typeof(System.Func <bool>))
                {
                    Skip();
                    return(null);
                }
                if (typeof(IEnumerable <BlueprintScriptableObject>).IsAssignableFrom(field.FieldType))
                {
                    //Needed to deserialize AbilityFocus.b689c0b78297dda40a6ae2ff3b8adb5c.json
                    //Newtonsoft.Json.JsonSerializationException
                    // Error converting value "Blueprint:fc4b01e4c4ebbb4448016c03df01902f:MandragoraSwarmDamageFeature" to type 'Kingmaker.Blueprints.BlueprintScriptableObject'.Path 'CustomParameterVariants[0]'
                    //ArgumentException: Could not cast or convert from System.String to Kingmaker.Blueprints.BlueprintScriptableObject.
                    jsonProp.ItemConverter = BlueprintAssetIdConverter;
                }
                if (typeof(BlueprintScriptableObject).IsAssignableFrom(field.FieldType))
                {
                    //MemberConverter required to deserialize see
                    //https://stackoverflow.com/questions/24946362/custom-jsonconverter-is-ignored-for-deserialization-when-using-custom-contract-r
                    //jsonProp.MemberConverter = BlueprintAssetIdConverter;
                    //jsonProp.Converter = BlueprintAssetIdConverter;
                    //Allow();
                }
            }
            else if (member is PropertyInfo property)
            {
            }
            else
            {
                throw new NotImplementedException($"Member type {member.MemberType} not implemented");
            }

            return(jsonProp);
        }