private void ProcessTypeToRegisterForJson( TypeToRegisterForJson typeToRegisterForJson) { var type = typeToRegisterForJson.Type; // generic type definitions are not a problem here because // TypeToRegisterForJson doesn't allow generic type definitions to be associated with converters. var jsonConverterBuilder = typeToRegisterForJson.JsonConverterBuilder; if (jsonConverterBuilder != null) { this.typesWithConverters.Add(type, null); if (this.jsonConverterBuilders.All(_ => _.Id != jsonConverterBuilder.Id)) { this.jsonConverterBuilders.Add(jsonConverterBuilder); } } var keyInDictionaryStringSerializer = typeToRegisterForJson.KeyInDictionaryStringSerializer; if (keyInDictionaryStringSerializer != null) { this.typeToKeyInDictionaryStringSerializerMap.Add(type, keyInDictionaryStringSerializer); } }
/// <inheritdoc /> public override TypeToRegister CreateSpawnedTypeToRegister( Type type, TypeToIncludeOrigin typeToIncludeOrigin) { var keyInDictionaryStringSerializer = this.KeyInDictionaryStringSerializer; // If a type has an associated KeyInDictionaryStringSerializer and we explore members // (JsonConverterBuilder will be null, otherwise the constructor throws if MemberTypesToInclude != None) // it is very highly unlikely that we want to use that serializer for members that are keys in // dictionaries. Without the code below which null it out, the config would require two entries for // the same type - one where MemberTypesToInclude is None, specifying the dictionary key serializer // and the other with All. Further, the one with the serializer would need to be registered first // (the second registration exists solely to explore members since the type itself will get // registered with the first entry). In the future, we should introduce an enum that describes // how to spawn and just follow those instructions (e.g. something like // CopyKeyInDictionaryStringSerializerWhenTypeToIncludeOriginIsGettingRelatedTypes - which would // only copy the serializer for related types, but not member types). if (typeToIncludeOrigin == TypeToIncludeOrigin.GettingMemberTypes) { keyInDictionaryStringSerializer = null; } var result = new TypeToRegisterForJson(type, this.RecursiveOriginType, this.Type, this.MemberTypesToInclude, this.RelatedTypesToInclude, this.JsonConverterBuilder, keyInDictionaryStringSerializer); return(result); }
/// <summary> /// Builds a <see cref="TypeToRegisterForJson"/> for a type using the most sensible settings, /// with a specified <see cref="IStringSerializeAndDeserialize"/> to use everywhere the type appears. /// </summary> /// <param name="type">The type to register.</param> /// <param name="stringSerializer">The string serializer to use for <paramref name="type"/>.</param> /// <returns> /// The type to register for JSON serialization. /// </returns> public static TypeToRegisterForJson ToTypeToRegisterForJsonUsingStringSerializer( this Type type, IStringSerializeAndDeserialize stringSerializer) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (stringSerializer == null) { throw new ArgumentNullException(nameof(stringSerializer)); } var canConvertTypeMatchStrategy = type.ResolveDefaultIntoActionableRelatedTypesToInclude().ToCanConvertTypeMatchStrategy(); var jsonConverterBuilderId = Guid.NewGuid().ToString() + "-" + Guid.NewGuid().ToString(); JsonConverter ConverterBuilderFunc() => new StringSerializerBackedJsonConverter(type, stringSerializer, canConvertTypeMatchStrategy); var jsonConverterBuilder = new JsonConverterBuilder(jsonConverterBuilderId, ConverterBuilderFunc, ConverterBuilderFunc); var result = new TypeToRegisterForJson(type, MemberTypesToInclude.None, RelatedTypesToInclude.Default, jsonConverterBuilder, stringSerializer); return(result); }
/// <inheritdoc /> protected sealed override TypeToRegister BuildTypeToRegisterForPostInitializationRegistration( Type type, Type recursiveOriginType, Type directOriginType, MemberTypesToInclude memberTypesToInclude, RelatedTypesToInclude relatedTypesToInclude) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (recursiveOriginType == null) { throw new ArgumentNullException(nameof(recursiveOriginType)); } if (directOriginType == null) { throw new ArgumentNullException(nameof(directOriginType)); } var genericTypeDefinition = type.GetGenericTypeDefinition(); var genericTypeDefinitionTypeToRegister = (TypeToRegisterForJson)this.RegisteredTypeToRegistrationDetailsMap[genericTypeDefinition].TypeToRegister; var result = new TypeToRegisterForJson(type, recursiveOriginType, directOriginType, memberTypesToInclude, relatedTypesToInclude, genericTypeDefinitionTypeToRegister.JsonConverterBuilder, genericTypeDefinitionTypeToRegister.KeyInDictionaryStringSerializer); return(result); }
/// <summary> /// Builds a <see cref="TypeToRegisterForJson"/> for a type using the most sensible settings. /// </summary> /// <param name="type">The type to register.</param> /// <returns> /// The type to register for JSON serialization. /// </returns> public static TypeToRegisterForJson ToTypeToRegisterForJson( this Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } var result = new TypeToRegisterForJson(type, MemberTypesToInclude.All, RelatedTypesToInclude.Default, null, null); return(result); }
/// <summary> /// Builds a <see cref="TypeToRegisterForJson"/> for a type using the most sensible settings, /// with a specified <see cref="IStringSerializeAndDeserialize"/> to use when dictionaries are keyed on that type. /// </summary> /// <param name="type">The type to register.</param> /// <param name="stringSerializer">The string serializer to use when dictionaries are keyed on <paramref name="type"/>.</param> /// <returns> /// The type to register for JSON serialization. /// </returns> public static TypeToRegisterForJson ToTypeToRegisterForJsonUsingKeyInDictionaryStringSerializer( this Type type, IStringSerializeAndDeserialize stringSerializer) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (stringSerializer == null) { throw new ArgumentNullException(nameof(stringSerializer)); } var result = new TypeToRegisterForJson(type, MemberTypesToInclude.All, RelatedTypesToInclude.Default, null, stringSerializer); return(result); }