示例#1
0
 public GeneratorContext(
     RoslynMetadataHelper roselyn,
     ReadContext read,
     WriteContext write,
     params IValueSerializationGenerator[] generators)
     : this(roselyn, read, write, new CompositeValueGenerator(generators))
 {
 }
示例#2
0
            public TypeConfig(RoslynMetadataHelper roslyn, Type contract, Type implementation, Func <INamedTypeSymbol, INamedTypeSymbol, CollectionImplementation> implementationFactory)
            {
                _contractType          = contract;
                _implementationType    = implementation;
                _implementationFactory = implementationFactory;

                _contract       = (INamedTypeSymbol)roslyn.FindTypeByFullName(contract.FullName);
                _implementation = (INamedTypeSymbol)roslyn.FindTypeByFullName(implementation.FullName);

                _contractFullName = _contract?.ConstructUnboundGenericType().ToDisplayString();
            }
示例#3
0
 public GeneratorContext(
     RoslynMetadataHelper roselyn,
     ReadContext read,
     WriteContext write,
     IValueSerializationGenerator generator)
 {
     Roselyn        = roselyn;
     Read           = read;
     Write          = write;
     ValueGenerator = generator;
 }
示例#4
0
        public ValueTypeSerializersGenerator(string nameSpace, RoslynMetadataHelper roselyn, bool useTryParseOrDefault, SourceFileMetadata generatedCodeMeta)
        {
            _nameSpace         = nameSpace;
            _generatedCodeMeta = generatedCodeMeta;
            _context           = new GeneratorContext(
                roselyn,
                new ReadContext("reader", "firstChar", "overChar"),
                new WriteContext("writer"),
                new DateTimeGenerator(useTryParseOrDefault),
                new JsonReaderGenerator(useTryParseOrDefault),
                new JsonWriterGenerator());

            _typeSymbols = Types
                           .Select(roselyn.GetTypeByFullName)
                           .ToArray();
        }
 public SerializerGenerator(
     string classFullName,
     IStaticSerializerResolver staticSerializerResolver,
     ICollectionImplementationResolver supportedCollections,
     IValueSerializationGenerator valuesGenerator,
     bool isRootImmutableDisabled,
     RoslynMetadataHelper roslyn,
     SourceFileMetadata generatedCodeMeta)
 {
     _classFullName            = classFullName;
     _staticSerializerResolver = staticSerializerResolver;
     _supportedCollections     = supportedCollections;
     _valuesGenerator          = valuesGenerator;
     _isRootImmutableDisabled  = isRootImmutableDisabled;
     _roslyn            = roslyn;
     _generatedCodeMeta = generatedCodeMeta;
 }
        public override IStaticSerializerResolver GetGenerator(
            SerializerGenerationConfiguration config,
            RoslynMetadataHelper metadataHelper,
            SourceFileMetadata generatedCodeMeta)
        {
            var generator = new StaticJsonSerializerGenerator(
                metadataHelper,
                config.SerializersNameSpace,
                config.DisableToUpperConstructor,
                PropertyFinder,
                PropertyGenerators,
                generatedCodeMeta
                );

            PropertyGenerators.Add(new RecursiveStaticSerializerGenerator(generator));

            return(generator);
        }
示例#7
0
 public StaticJsonBuilderSerializerGenerator(
     RoslynMetadataHelper metadataHelper,
     string @namespace,
     bool disableToUpperCtor,
     IPropertyFinder propertyFinder,
     IEnumerable <IValueSerializationGenerator> valueGenerators,
     SourceFileMetadata generatedCodeMeta)
 {
     _metadataHelper     = metadataHelper;
     _namespace          = @namespace;
     _disableToUpperCtor = disableToUpperCtor;
     _propertyFinder     = propertyFinder;
     _valueGenerators    = valueGenerators;
     _generatedCodeMeta  = generatedCodeMeta;
     _propertyGenerator  = () => new GeneratorContext(
         _metadataHelper,
         new ReadContext(SerializerConstants.ReaderParameterName, _overChar),
         new WriteContext(_writer, _object),
         valueGenerators.ToArray()
         );
 }
        private void ResolveTypes()
        {
            var additionalTypes = _config.Entities.Safe().Concat(WellKnownTypes.Types).ToArray();

            // Augment the current compilation by adding types to compilation
            _roslynHelper = new RoslynMetadataHelper(_compilation,
                                                     _project,
                                                     additionalTypes
                                                     );

            // Change current compilation for the new one
            _compilation = _roslynHelper.Compilation;

            _serializationTypeInstance = _config.CreateInstance(_roslynHelper);

            _collections = new CollectionImplementationResolver(_roslynHelper);

            // Prepare known custom deserializers
            _knownCustomDeserializers.Add(
                (INamedTypeSymbol)_roslynHelper.GetTypeByFullName("GeneratedSerializers.JwtData`1"),
                (INamedTypeSymbol)_roslynHelper.GetTypeByFullName("GeneratedSerializers.JwtDataDeserializer`1"));

            SymbolExtensions.KnownCustomDeserializers = _knownCustomDeserializers;

            // Extract types declared by using the [assembly: JsonSerializableType(typeof(<some Type>))] attribute
            var jsonSerializableTypesFromAttributes =
                //Types in current assembly
                GetSerializableTypesFromAttribute(_compilation.Assembly)
                //Types in referenced assemblies
                .Concat(_compilation.Assembly
                        .Modules
                        .First()
                        .ReferencedAssemblySymbols
                        .SelectMany(GetSerializableTypesFromAttribute)
                        );

            // Extract types declared in the configuration file (SerializationConfig.xml) -- that's a legacy feature
            var typesFromConfig = _config
                                  .Entities
                                  .Safe()
                                  .Select(typeName =>
            {
                var type = _roslynHelper.FindTypeByFullName(typeName);

                if (type == null)
                {
                    throw new TypeAccessException("Cannot resolve type {0}.".InvariantCultureFormat(typeName));
                }

                return(type);
            });

            // Join the 2 lists into one
            var configuredTypes = typesFromConfig
                                  .Concat(jsonSerializableTypesFromAttributes)
                                  .Distinct()
                                  .ToList();

            // Unwrap Collections & Dictionaries...
            var simplifiedConfiguredTypes = configuredTypes
                                            .Select(t => t.DictionnaryOf() ?? t.EnumerableOf() ?? t.NullableOf() ?? t)
                                            .Distinct()
                                            .ToArray();

            // Find all types to generate a StaticDeserializer for (include nested types)
            var allNeededTypes = simplifiedConfiguredTypes
                                 .Concat(_serializationTypeInstance
                                         .StaticDeserializerPropertyFinder
                                         .GetNestedTypes(simplifiedConfiguredTypes)
                                         .Where(type => _nonGeneratedNameSpaces.None(ns => type.ContainingNamespace?.ToDisplayString().Equals(ns, StringComparison.OrdinalIgnoreCase) ?? false)))
                                 .ToList();

            foreach (var t in allNeededTypes.ToArray())
            {
                var attr = t.FindAttribute("Uno.ImmutableBuilderAttribute");
                if (attr == null)
                {
                    // no builder for this one
                    continue;
                }

                if (!(attr.ConstructorArguments[0].Value is ITypeSymbol builderType))
                {
                    continue;                     // That would bea weird situation.
                }

                // Add the builder to type to deserialize (neededtype + root types)
                configuredTypes.Add(builderType);
                allNeededTypes.Add(builderType);
            }

            _allNeededTypes = allNeededTypes.ToArray();

            // Check if configuration restrict the generation to configured types
            if (!GetForceGenerateCollectionSerializable(_compilation.Assembly))
            {
                var nullable = (INamedTypeSymbol)_roslynHelper.GetTypeByFullName("System.Nullable`1");

                var collectionTypes = _collections
                                      .GetUnboundedSupportedTypes()
                                      .Where(t => !_config.IsImmutablesAtRootDisabled || !t.Implementation.GetDeclarationGenericFullName().Contains("Immutable"))
                                      .Concat(new Implemtation(_roslynHelper.GetArray(_roslynHelper.GetGenericType())));

                var typesToGenerate = new List <ITypeSymbol>();

                // Add Nullable<T> version for enums
                typesToGenerate.AddRange(_allNeededTypes
                                         .SelectMany(type => type.TypeKind == TypeKind.Enum
                                                ? new[] { type, nullable.Construct(type) }
                                                : new[] { type }));

                // Add all "primitives" types (value types + string)
                typesToGenerate.AddRange(ValueTypeSerializersGenerator.Types.Select(_roslynHelper.GetTypeByFullName));

                // Add all permutations collection/type in collection<type>
                typesToGenerate.AddRange(typesToGenerate
                                         .SelectMany(type => collectionTypes
                                                     .Select(colImp => _roslynHelper.ConstructFromUnbounded(colImp.Contract, type)))
                                         .ToArray());

                // That's all
                _rootTypes = typesToGenerate.ToArray();
            }
            else
            {
                _rootTypes = configuredTypes.ToArray();
            }

            //Find all CustomDeserializer Types present (include nested types)
            _customSerializationAttributeTypes = _serializationTypeInstance
                                                 .CustomDeserializerPropertyFinder
                                                 .GetNestedCustomDeserializerTypes(simplifiedConfiguredTypes)
                                                 .Concat(simplifiedConfiguredTypes.Select(type => type.FindCustomDeserializerType()).Trim())
                                                 .Distinct()
                                                 .ToArray();
        }
示例#9
0
 public CollectionImplementationResolver(RoslynMetadataHelper roslyn, bool preferImmutableCollections = false)
 {
     _roslyn = roslyn;
 }
 public abstract IStaticSerializerResolver GetBuilderGenerator(SerializerGenerationConfiguration config, RoslynMetadataHelper metadataHelper, SourceFileMetadata metadata);
        public static SerializationType CreateInstance(this SerializerGenerationConfiguration configuration, RoslynMetadataHelper helper)
        {
            switch (configuration.SerializationType)
            {
            case SerializationTypeName.Json:
                return(new JsonSerializationType
                {
                    Name = "Json",
                    PropertyFinder = new DefaultPropertyFinder(),
                    StaticDeserializerPropertyFinder = new JsonStaticDeserializerPropertyFinder(),
                    CustomDeserializerPropertyFinder = new JsonCustomDeserializerPropertyFinder(),
                    PropertyGenerators = new List <IValueSerializationGenerator>
                    {
                        // Note: Generators are used in the order of the registration.
                        // Be sure to put "special cases" before "generic cases"
                        //
                        // For instance, put the MicrosoftDateTimeGenerator before the JsonReaderGenerator
                        // which also handles DateTime, but does not validate the attribute.

                        new CustomSerializerGenerator(),                                 // This must be the first in order to be able to override all types

                        new UriGenerator(),
                        new GuidGenerator(),
                        new EnumGenerator(new Enums(string.Empty, true, true, null, new DefaultPropertyFinder(), null)),
                        new MicrosotDateTimeGenerator(configuration.UseTryParseOrDefault),
                        new DateTimeGenerator(configuration.UseTryParseOrDefault),

                        new JsonCollectionGenerator(new CollectionImplementationResolver(helper)),

                        new JsonReaderGenerator(configuration.UseTryParseOrDefault),
                        new JsonWriterGenerator(),
                    }
                });

            default:
                throw new ArgumentOutOfRangeException("typeName", "Unknwon serialization type.");
            }
        }