Пример #1
0
        public JsonObjectSerializer(bool withFormatting = false, KnownTypesContainer knownTypes = null, IEnumerable <IMemberValueConverter> converters = null)
        {
            var serializer = new JsonSerializer
            {
                NullValueHandling   = NullValueHandling.Ignore,
                ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
                Formatting          = withFormatting ? Formatting.Indented : Formatting.None
            };

            IContractResolver contractResolver = null;

            var converterList = converters?.ToArray();

            if (converterList?.Length > 0)
            {
                contractResolver = new JsonMemberValueConverterResolver(converterList);
            }

            serializer.ContractResolver = contractResolver ?? new DefaultContractResolver();

            if (knownTypes != null)
            {
                serializer.Converters.Add(new KnownTypesJsonConverter(knownTypes));
            }

            _serializer = serializer;
        }
 public KnownTypesJsonConverter(KnownTypesContainer knownTypes)
 {
     _enumerableType = typeof(IEnumerable);
     _knownTypes     = knownTypes ?? new KnownTypesContainer();
 }