/// <summary> Fills valid properties of the object with values deserialized from JSON data. </summary> /// <param name="instanceDeserializedFromJson"> The temporary non-persistent object storing deserialized data. </param> public virtual void InitializeWithDeserializedJson(IDeserializedFromJsonWithGaijinId instanceDeserializedFromJson) { var properties = GetType() .GetProperties() .Where(property => property.GetCustomAttribute <PropertyAttribute>() is PropertyAttribute) .ToDictionary(property => property.Name) ; var jsonProperties = instanceDeserializedFromJson .GetType() .GetProperties() .ToDictionary(property => property.Name) ; foreach (var property in properties) { if (jsonProperties.TryGetValue(property.Key, out var jsonProperty)) { property.Value.SetValue(this, jsonProperty.GetValue(instanceDeserializedFromJson)); } } }
public static void InsertJsonPropertyValueIntoGameModeParameterSet(this IDictionary <string, VehicleGameModeParameterSetBase> parameterSets, IDeserializedFromJsonWithGaijinId instanceDeserializedFromJson, PropertyInfo jsonProperty) { var persistAsDictionaryItemAttribute = jsonProperty.GetCustomAttribute <PersistAsDictionaryItemAttribute>(); if (persistAsDictionaryItemAttribute is null) // We are not interested in any properties not marked for consolidation via PersistAsDictionaryItemAttribute. { return; } if (!parameterSets.TryGetValue(persistAsDictionaryItemAttribute.Key, out var parameterSet)) { return; } var jsonPropertyValue = jsonProperty.GetValue(instanceDeserializedFromJson); #region Adjust value inputs for nullability of dictionary values (in case of non-required JSON properties) if (jsonProperty.PropertyType == typeof(int)) { jsonPropertyValue = new int?((int)jsonPropertyValue); } else if (jsonProperty.PropertyType == typeof(decimal)) { jsonPropertyValue = new decimal?((decimal)jsonPropertyValue); } #endregion Adjust value inputs for nullability of dictionary values (in case of non-required JSON properties switch (persistAsDictionaryItemAttribute.GameMode) { case EGameMode.Arcade: parameterSet.InternalArcade = jsonPropertyValue; break; case EGameMode.Realistic: parameterSet.InternalRealistic = jsonPropertyValue; break; case EGameMode.Simulator: parameterSet.InternalSimulator = jsonPropertyValue; break; case EGameMode.Event: parameterSet.InternalEvent = jsonPropertyValue; break; } ; }