예제 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InheritedTypeReaderJsonConverter"/> class.
 /// </summary>
 /// <param name="getTypesToHandleFunc">A func that gets the types that, when encountered, should trigger usage of the converter.</param>
 /// <param name="jsonSerializationConfiguration">The serialization configuration in-use.</param>
 public InheritedTypeReaderJsonConverter(
     Func <ConcurrentDictionary <Type, object> > getTypesToHandleFunc,
     JsonSerializationConfigurationBase jsonSerializationConfiguration)
     : base(getTypesToHandleFunc)
 {
     this.jsonSerializationConfiguration = jsonSerializationConfiguration;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="ObcJsonSerializer"/> class.
        /// </summary>
        /// <param name="jsonSerializationConfigurationType">Optional type of configuration to use; DEFAULT is none.</param>
        public ObcJsonSerializer(
            JsonSerializationConfigurationType jsonSerializationConfigurationType = null)
            : base(jsonSerializationConfigurationType ?? typeof(NullJsonSerializationConfiguration).ToJsonSerializationConfigurationType())
        {
            this.jsonSerializationConfiguration = (JsonSerializationConfigurationBase)this.SerializationConfiguration;

            if (this.jsonSerializationConfiguration.JsonFormattingKind == JsonFormattingKind.Invalid)
            {
                throw new ArgumentOutOfRangeException(Invariant($"'{nameof(this.jsonSerializationConfiguration.JsonFormattingKind)}' == '{JsonFormattingKind.Invalid}'"), (Exception)null);
            }

            this.SerializerRepresentation = new SerializerRepresentation(SerializationKind.Json, jsonSerializationConfigurationType?.ConcreteSerializationConfigurationDerivativeType.ToRepresentation());
        }
        /// <summary>
        /// Build <see cref="JsonSerializerSettings" /> to use for serialization using Newtonsoft.
        /// </summary>
        /// <param name="serializationDirection">Direction of serialization.</param>
        /// <param name="jsonSerializationConfiguration">The serialization configuration in use.</param>
        /// <returns>
        /// Prepared settings to use with Newtonsoft.
        /// </returns>
        public JsonSerializerSettings BuildJsonSerializerSettings(
            SerializationDirection serializationDirection,
            JsonSerializationConfigurationBase jsonSerializationConfiguration)
        {
            if (serializationDirection == SerializationDirection.Unknown)
            {
                throw new ArgumentOutOfRangeException(Invariant($"'{nameof(serializationDirection)}' == '{SerializationDirection.Unknown}'"), (Exception)null);
            }

            if (jsonSerializationConfiguration == null)
            {
                throw new ArgumentNullException(nameof(jsonSerializationConfiguration));
            }

            var jsonFormattingKind = jsonSerializationConfiguration.JsonFormattingKind;

            var jsonSerializerSettingsBuilder = JsonFormattingKindToSettingsSelectorByDirection[jsonFormattingKind](SerializationDirection.Serialize);

            var result = jsonSerializerSettingsBuilder(() => this.RegisteredTypeToRegistrationDetailsMap);

            var specifiedConverters = this.jsonConverterBuilders.Select(_ => _.GetJsonConverterBuilderFuncBySerializationDirection(serializationDirection)()).ToList();

            var defaultConverters = this.GetDefaultConverters(serializationDirection, jsonFormattingKind);

            // Newtonsoft uses the converters in the order they are provided, so specifiedConverters will have priority because they come first
            var converters = new JsonConverter[0]
                             .Concat(specifiedConverters)
                             .Concat(defaultConverters)
                             .ToList();

            result.Converters = converters;

            if ((this.OverrideContractResolver != null) && this.OverrideContractResolver.ContainsKey(serializationDirection))
            {
                var overrideResolver = this.OverrideContractResolver[serializationDirection];

                if (overrideResolver == null)
                {
                    throw new ArgumentException(Invariant($"{nameof(overrideResolver)} is null"));
                }

                result.ContractResolver = overrideResolver.ContractResolverBuilder(() => this.RegisteredTypeToRegistrationDetailsMap);
            }

            return(result);
        }