public string GetRead(string target, ITypeSymbol targetType, IValueSerializationGeneratorContext context)
        {
            if (_enums.IsResolvable(targetType))
            {
                var value = VariableHelper.GetName(targetType);
                if (targetType.IsNullable())
                {
                    return($@"
						string {value};
						{context.Read<string>(value)}
						if (!string.IsNullOrEmpty({value}))
						{{
							{target} = {Enums.GetConverterName(targetType)}.FromString({value});
						}}"                        );
                }
                else
                {
                    return($@"
						string {value};
						{context.Read<string>(value)}
						{target} = {Enums.GetConverterName(targetType)}.FromString({value});"                        );
                }
            }

            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);
            }
        }
        public string GetRead(string target, IPropertySymbol targetProperty, IValueSerializationGeneratorContext context)
        {
            if (targetProperty.Type is INamedTypeSymbol instanceType &&
                _serializer.IsResolvable(instanceType))
            {
                return($"{target}.{targetProperty.Name} = {_serializer.GetResolve(instanceType)}.Deserialize({context.Read.Reader}, {context.Read.FirstChar}, out {context.Read.OverChar});");
            }

            return(null);
        }
예제 #4
0
        public override string Read(string target, bool isNullable, IValueSerializationGeneratorContext context)
        {
            var uri = VariableHelper.GetName("uri");

            return($@"
				string {uri};
				{context.Read<string>(uri)}
				if (!string.IsNullOrEmpty({uri}))
				{{
					{target} = new Uri({uri}, UriKind.RelativeOrAbsolute);
				}}"                );
        }
예제 #5
0
        public override string Read(string target, bool isNullable, IValueSerializationGeneratorContext context)
        {
            var guid = VariableHelper.GetName("guid");

            return($@"
				string {guid};
				{context.Read<string>(guid)}
				if (!string.IsNullOrWhiteSpace({guid}))
				{{
					{target} = Guid.Parse({guid});
				}}"                );
        }
예제 #6
0
        public string GetRead(string target, ITypeSymbol targetType, IValueSerializationGeneratorContext context)
        {
            string method;

            if (_lookups.TryGetValue(targetType.GetDeclarationGenericFullName(), out method))
            {
                return($"{target} = {context.Read.Reader}.{method}({context.Read.FirstChar}, out {context.Read.OverChar});");
            }
            else
            {
                return(null);
            }
        }
        public string GetRead(string target, IPropertySymbol targetProperty, IValueSerializationGeneratorContext context)
        {
            var serializerType = FindCustomSerializerType(targetProperty);

            if (serializerType != null)
            {
                return($@"{target}.{targetProperty.Name} = {StaticJsonCustomDeserializerGenerator.GetClassName(serializerType)}.Instance.Deserialize(
					{context.Read.Reader},
					{context.Read.FirstChar},
					out {context.Read.OverChar});"                    );
            }

            return(null);
        }
 public string GetRead(string target, ITypeSymbol targetType, IValueSerializationGeneratorContext context)
 {
     if (_supportedTypes.Contains(targetType.GetDeclarationGenericFullName(), StringComparer.OrdinalIgnoreCase))
     {
         return(Read(target, false, context));
     }
     else if (targetType.IsNullable(out targetType) && _supportedTypes.Contains(targetType.GetDeclarationGenericFullName(), StringComparer.OrdinalIgnoreCase))
     {
         return(Read(target, true, context));
     }
     else
     {
         return(null);
     }
 }
예제 #9
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));
        }
        public string GetRead(string target, ITypeSymbol targetType, IValueSerializationGeneratorContext context)
        {
            ITypeSymbol itemType;

            if (targetType.IsDictionary(out itemType) ||
                targetType.IsCollectionOfKeyValuePairOfString(out itemType))
            {
                return(ReadDictionary(target, targetType, itemType, context));
            }
            else if (targetType.IsCollection(out itemType))
            {
                return(ReadCollection(target, targetType, itemType, context));
            }
            else
            {
                return(null);
            }
        }
예제 #11
0
 public static string Read <T>(this IValueSerializationGeneratorContext context, string targetPropertyName)
 {
     return(context.GetRead(targetPropertyName, context.Roselyn.GetTypeByFullName(typeof(T).FullName)));
 }
예제 #12
0
 public Context(IValueSerializationGeneratorContext inner, ReadContext read = null, WriteContext write = null)
 {
     _inner = inner;
     Read   = read ?? inner.Read;
     Write  = write ?? inner.Write;
 }
예제 #13
0
 public static string GetWrite(this IValueSerializationGeneratorContext context, string sourceName, string sourceCode, ITypeSymbol sourceType) => context.ValueGenerator.GetWrite(sourceName, sourceCode, sourceType, context);
예제 #14
0
 public static string GetRead(this IValueSerializationGeneratorContext context, string target, ITypeSymbol targetType) => context.ValueGenerator.GetRead(target, targetType, context);
 public string GetRead(string target, ITypeSymbol targetType, IValueSerializationGeneratorContext context)
 {
     return(_serializer.IsResolvable(targetType)
                         ? $"{target} = {_serializer.GetResolve(targetType)}.Deserialize({context.Read.Reader}, {context.Read.FirstChar}, out {context.Read.OverChar});"
                         : null);
 }
 public string GetRead(string target, ITypeSymbol targetType, IValueSerializationGeneratorContext context)
 {
     return(_generators
            .Select(g => g.GetRead(target, targetType, context))
            .FirstOrDefault(code => code.HasValue()));
 }
 public string GetRead(string target, ITypeSymbol targetType, IValueSerializationGeneratorContext context) => null;
예제 #18
0
 public string GetWrite(string sourceName, string source, IPropertySymbol sourceProperty, IValueSerializationGeneratorContext context) => null;
예제 #19
0
 public override string Write(string sourceName, string sourceCode, bool isNullable, IValueSerializationGeneratorContext context)
 {
     return(context.Write <string>(sourceName, isNullable
                         ? $"{sourceCode}?.ToString(\"D\")"
                         : $"{sourceCode}.ToString(\"D\")"));
 }
예제 #20
0
 public string GetRead(string target, IPropertySymbol targetProperty, IValueSerializationGeneratorContext context) => GetRead(
     $"{target}.{targetProperty.Name}",
     targetProperty.Type,
     context);
        public string GetWrite(string sourceName, string source, IPropertySymbol sourceProperty, IValueSerializationGeneratorContext context)
        {
            var serializerType = FindCustomSerializerType(sourceProperty);

            if (serializerType != null)
            {
                var value = VariableHelper.GetName(sourceProperty.Type);
                return($@"
					var {value} = {source}.{sourceProperty.Name};
					if ({value} != null)
					{{
						{context.Write.Object}.WritePropertyName(""{sourceName}"");
						{StaticJsonCustomDeserializerGenerator.GetClassName(serializerType)}.Instance.Serialize({context.Write.Writer}, {value});
					}}"                    );
            }

            return(null);
        }
예제 #22
0
 public static string Write <T>(this IValueSerializationGeneratorContext context, string sourceName, string source)
 {
     return(context.GetWrite(sourceName, source, context.Roselyn.GetTypeByFullName(typeof(T).FullName)));
 }
 public string GetWrite(string sourceName, string sourceCode, ITypeSymbol sourceType, IValueSerializationGeneratorContext context) => null;
예제 #24
0
 public static IValueSerializationGeneratorContext UsingOverAsFirstChar(this IValueSerializationGeneratorContext context, bool overHasValue = false)
 {
     return(new Context(context, read: context.Read.UsingOverAsFirstChar(overHasValue)));
 }
 public string GetWrite(string sourceName, string sourceCode, ITypeSymbol sourceType, IValueSerializationGeneratorContext context)
 {
     return(_generators
            .Select(g => g.GetWrite(sourceName, sourceCode, sourceType, context))
            .FirstOrDefault(code => code.HasValue()));
 }
예제 #26
0
 public static IValueSerializationGeneratorContext IgnoringCurrentFirstChar(this IValueSerializationGeneratorContext context, bool overHasValue = false)
 {
     return(new Context(context, read: context.Read.IgnoringCurrentFirstChar(overHasValue)));
 }
 public string GetWrite(string sourceName, string source, IPropertySymbol sourceProperty, IValueSerializationGeneratorContext context) => GetWrite(
     sourceName,
     $"{source}.{sourceProperty.Name}",
     sourceProperty.Type,
     context);
예제 #28
0
 public static string GetRead(this IValueSerializationGeneratorContext context, string target, IPropertySymbol targetProperty) => context.ValueGenerator.GetRead(target, targetProperty, context);
        public string GetWrite(string sourceName, string sourceCode, ITypeSymbol sourceType, IValueSerializationGeneratorContext context)
        {
            if (_serializer.IsResolvable(sourceType))
            {
                var value = VariableHelper.GetName(sourceType);

                string result;
                if (sourceName.IsNullOrWhiteSpace())
                {
                    // We are writing an item of a collection or something like that.
                    // We cannot ignore null values, instead we must write the "null" keyword.

                    result =
                        $@"var {value} = {sourceCode};
if ({value} == null)
{{
	{context.Write.Writer}.WriteNullValue();
}}
else
{{
	{_serializer.GetResolve(sourceType)}.Serialize({context.Write.Writer}, {value});
}}";
                }
                else
                {
                    result =
                        $@"var {value} = {sourceCode};
if ({value} != null)
{{
	{context.Write.Object}.WritePropertyName(""{sourceName}"");
	{_serializer.GetResolve(sourceType)}.Serialize({context.Write.Writer}, {value});
}}";
                }

                return(result);
            }

            return(null);
        }
예제 #30
0
 public static string GetWrite(this IValueSerializationGeneratorContext context, string sourceName, string source, IPropertySymbol sourceProperty) => context.ValueGenerator.GetWrite(sourceName, source, sourceProperty, context);