예제 #1
0
        public string GetWrite(string sourceName, string source, IPropertySymbol sourceProperty, IValueSerializationGeneratorContext context)
        {
            if (sourceProperty.FindAttributeByShortName("MicrosoftDateTimeFormatAttribute") != null)
            {
                var type = sourceProperty.Type;
                if (_supportedTypes.Contains(type.GetDeclarationGenericFullName()))
                {
                    return(context.Write <string>(sourceName, $"GeneratedSerializers.MicrosoftDateTimeHelper.ToString({source}.{sourceProperty.Name})"));
                }
                else if (type.IsNullable(out type) && _supportedTypes.Contains(type.GetDeclarationGenericFullName()))
                {
                    var value = VariableHelper.GetName(sourceProperty.Type);
                    return($@"
						var {value} = {source}.{sourceProperty.Name};
						if ({value}.HasValue)
						{{
							{context.Write<string>(sourceName, $"GeneratedSerializers.MicrosoftDateTimeHelper.ToString({value}.Value)")}
						}}"                        );
                }
                else
                {
                    return($"\r\n #error {_nonSupportedTypeError}");
                }
            }
            else
            {
                return(null);
            }
        }
예제 #2
0
        public string GetRead(string target, IPropertySymbol targetProperty, IValueSerializationGeneratorContext context)
        {
            if (targetProperty.FindAttributeByShortName("MicrosoftDateTimeFormatAttribute") != null)
            {
                var value      = VariableHelper.GetName("dateTime");
                var targetName = $"{target}.{targetProperty.Name}";
                var type       = targetProperty.Type;
                if (_supportedTypes.Contains(type.GetDeclarationGenericFullName()))
                {
                    return($@"
						string {value};
						{context.Read<string>(value)}
						{GetParseMethod(value, targetName, type)}"                        );
                }
                else if (type.IsNullable(out type) && _supportedTypes.Contains(type.GetDeclarationGenericFullName()))
                {
                    return($@"
						string {value};
						{context.Read<string>(value)}
						if (!string.IsNullOrWhiteSpace({value}))
						{{
							{GetParseMethod(value, targetName, type)}
						}}"                        );
                }
                else
                {
                    return($"\r\n #error {_nonSupportedTypeError}");
                }
            }
            else
            {
                return(null);
            }
        }
예제 #3
0
        public string GetWrite(string sourceName, string source, IPropertySymbol sourceProperty, IValueSerializationGeneratorContext context)
        {
            var formatAttribute = sourceProperty.FindAttributeByShortName("DateTimeFormatAttribute");

            string format = null;

            if (formatAttribute != null)
            {
                format = (formatAttribute.NamedArguments.FirstOrDefault(a => a.Key == "Format").Value.Value
                          ?? formatAttribute.ConstructorArguments.FirstOrDefault(x => x.Type.Name == "String").Value) as string;
            }

            return(GetWrite(sourceName, $"{source}.{sourceProperty.Name}", sourceProperty.Type, format, context));
        }
예제 #4
0
        public string GetRead(string target, IPropertySymbol targetProperty, IValueSerializationGeneratorContext context)
        {
            var formatAttribute = targetProperty.FindAttributeByShortName("DateTimeFormatAttribute");

            string format = null, styles = null;

            if (formatAttribute != null)
            {
                format = (formatAttribute.NamedArguments.FirstOrDefault(a => a.Key == "Format").Value.Value
                          ?? formatAttribute.ConstructorArguments.FirstOrDefault(x => x.Type.Name == "String").Value) as string;

                styles = (formatAttribute.NamedArguments.FirstOrDefault(a => a.Key == "Styles").Value.Value
                          ?? formatAttribute.ConstructorArguments.FirstOrDefault(x => x.Type.Name == "DateTimeStyles").Value) as string;
            }

            return(GetRead($"{target}.{targetProperty.Name}", targetProperty.Type, format, styles, context));
        }
예제 #5
0
 private static bool IsIgnored(IPropertySymbol propInfo)
 {
     return(propInfo.FindAttributeByShortName("SerializationIgnoreAttribute") != null ||
            propInfo.FindAttribute("Newtonsoft.Json.JsonIgnoreAttribute") != null);
 }