示例#1
0
 public static T FromJSON <T>(this string filename,
                              TypeNameHandling handling             = TypeNameHandling.All,
                              TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full)
 {
     return(JsonConvert.DeserializeObject <T>(File.ReadAllText(filename),
                                              new JsonSerializerSettings {
         TypeNameHandling = handling, TypeNameAssemblyFormatHandling = format
     }));
 }
示例#2
0
        /*
         * public static T FromBSON<T>(this string filename, bool root_is_array = false)
         * {
         *  using (var fo = File.OpenRead(filename))
         *  using (var br = new BsonDataReader(fo, root_is_array, DateTimeKind.Local))
         *  {
         *      var serializer = JsonSerializer.Create(new JsonSerializerSettings
         *          {TypeNameHandling = TypeNameHandling.Auto});
         *      return serializer.Deserialize<T>(br);
         *  }
         * }*/

        public static T FromJSONString <T>(this string data,
                                           TypeNameHandling handling             = TypeNameHandling.All,
                                           TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full)
        {
            return(JsonConvert.DeserializeObject <T>(data,
                                                     new JsonSerializerSettings {
                TypeNameHandling = handling, TypeNameAssemblyFormatHandling = format
            }));
        }
示例#3
0
 public static string ToJSON <T>(this T obj,
                                 TypeNameHandling handling             = TypeNameHandling.All,
                                 TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full)
 {
     return(JsonConvert.SerializeObject(obj, Formatting.Indented,
                                        new JsonSerializerSettings {
         TypeNameHandling = handling, TypeNameAssemblyFormatHandling = format
     }));
 }
示例#4
0
        // Token: 0x06000E1E RID: 3614 RVA: 0x0005192C File Offset: 0x0004FB2C
        public static string GetTypeName(Type t, TypeNameAssemblyFormatHandling assemblyFormat, [Nullable(2)] ISerializationBinder binder)
        {
            string fullyQualifiedTypeName = ReflectionUtils.GetFullyQualifiedTypeName(t, binder);

            if (assemblyFormat == TypeNameAssemblyFormatHandling.Simple)
            {
                return(ReflectionUtils.RemoveAssemblyDetails(fullyQualifiedTypeName));
            }
            if (assemblyFormat != TypeNameAssemblyFormatHandling.Full)
            {
                throw new ArgumentOutOfRangeException();
            }
            return(fullyQualifiedTypeName);
        }
示例#5
0
        public static string GetTypeName(Type t, TypeNameAssemblyFormatHandling assemblyFormat, ISerializationBinder binder)
        {
            string fullyQualifiedTypeName = GetFullyQualifiedTypeName(t, binder);

            switch (assemblyFormat)
            {
            case TypeNameAssemblyFormatHandling.Simple:
                return(RemoveAssemblyDetails(fullyQualifiedTypeName));

            case TypeNameAssemblyFormatHandling.Full:
                return(fullyQualifiedTypeName);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        /// <inheritdoc />
        public IAndJsonSerializerSettingsTestBuilder WithTypeNameAssemblyFormatHandling(
            TypeNameAssemblyFormatHandling typeNameAssemblyFormatHandling)
        {
            this.jsonSerializerSettings.TypeNameAssemblyFormatHandling = typeNameAssemblyFormatHandling;
            this.validations.Add((expected, actual) =>
            {
                if (!CommonValidator.CheckEquality(expected.TypeNameAssemblyFormatHandling, actual.TypeNameAssemblyFormatHandling))
                {
                    this.ThrowNewJsonResultAssertionException(
                        $"{expected.TypeNameAssemblyFormatHandling} type name assembly format handling",
                        $"in fact found {actual.TypeNameAssemblyFormatHandling}");
                }
            });

            return(this);
        }
示例#7
0
        public void GenerateSchema_HonorsSerializerSetting_TypeNameHandling(
            TypeNameHandling typeNameHandling,
            TypeNameAssemblyFormatHandling typeNameAssemblyFormatHandling,
            bool expectedDiscriminatorPresent,
            string expectedDiscriminatorMappingKeyFormat)
        {
            var subject = Subject(
                configureGenerator: c =>
            {
                c.UseAllOfForInheritance = true;
            },
                configureSerializer: c =>
            {
                c.TypeNameHandling = typeNameHandling;
                c.TypeNameAssemblyFormatHandling = typeNameAssemblyFormatHandling;
            }
                );
            var schemaRepository = new SchemaRepository();

            var referenceSchema = subject.GenerateSchema(typeof(BaseType), schemaRepository);

            var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];

            if (expectedDiscriminatorPresent)
            {
                Assert.Contains("$type", schema.Properties.Keys);
                Assert.Contains("$type", schema.Required);
                Assert.NotNull(schema.Discriminator);
                Assert.Equal("$type", schema.Discriminator.PropertyName);
                Assert.Equal(
                    expected: new Dictionary <string, string>
                {
                    [string.Format(expectedDiscriminatorMappingKeyFormat, "BaseType")] = "#/components/schemas/BaseType",
                    [string.Format(expectedDiscriminatorMappingKeyFormat, "SubType1")] = "#/components/schemas/SubType1",
                    [string.Format(expectedDiscriminatorMappingKeyFormat, "SubType2")] = "#/components/schemas/SubType2"
                },
                    actual: schema.Discriminator.Mapping);
            }
            else
            {
                Assert.DoesNotContain("$type", schema.Properties.Keys);
                Assert.DoesNotContain("$type", schema.Required);
                Assert.Null(schema.Discriminator);
            }
        }
        public void GenerateSchema_HonorsSerializerSetting_TypeNameHandling(
            TypeNameHandling typeNameHandling,
            TypeNameAssemblyFormatHandling typeNameAssemblyFormatHandling,
            bool expectedDiscriminatorPresent,
            string expectedDiscriminatorMappingKeyFormat)
        {
            var subject = Subject(
                configureGenerator: c =>
            {
                c.UseOneOfForPolymorphism = true;
            },
                configureSerializer: c =>
            {
                c.TypeNameHandling = typeNameHandling;
                c.TypeNameAssemblyFormatHandling = typeNameAssemblyFormatHandling;
            }
                );
            var schemaRepository = new SchemaRepository();

            var schema = subject.GenerateSchema(typeof(BaseType), schemaRepository);

            if (expectedDiscriminatorPresent)
            {
                Assert.NotNull(schema.Discriminator);
                Assert.Equal("$type", schema.Discriminator.PropertyName);
                var expectedDiscriminatorMapping = schema.OneOf
                                                   .ToDictionary(
                    possibleSchema => string.Format(expectedDiscriminatorMappingKeyFormat, possibleSchema.Reference.Id),
                    possibleSchema => possibleSchema.Reference.ReferenceV3
                    );
                Assert.Equal(expectedDiscriminatorMapping, schema.Discriminator.Mapping);
            }
            else
            {
                Assert.Null(schema.Discriminator);
                foreach (var referenceSchema in schema.OneOf)
                {
                    Assert.DoesNotContain("$type", schemaRepository.Schemas[referenceSchema.Reference.Id].Properties.Keys);
                }
            }
        }
示例#9
0
 public string GetTypeName(Type t, TypeNameAssemblyFormatHandling assemblyFormat, ISerializationBinder binder)
 {
     return(Types.GetTypeName(t, assemblyFormat, binder));
 }