public static string WriteDictionary(string sourceName, string sourceCode, ITypeSymbol itemType, IValueSerializationGeneratorContext context) { var itemTypeName = $"System.Collections.Generic.KeyValuePair<string, {itemType.GetDeclarationGenericFullName()}>"; var value = VariableHelper.GetName(itemType); var enumerator = VariableHelper.GetName("enumerator"); if (sourceName.IsNullOrWhiteSpace()) { return($@" var {value} = {sourceCode}; if ({value} == null) {{ {context.Write.Writer}.WriteNullValue(); }} else {{ {context.Write.Writer}.Write('{{'); var {enumerator} = {value}.GetEnumerator(); if ({enumerator}.MoveNext()) {{ {context.Write.Writer}.WritePropertyName((({itemTypeName}){enumerator}.Current).Key); {context.GetWrite(null, $"(({itemTypeName}){enumerator}.Current).Value", itemType)}; }} while ({enumerator}.MoveNext()) {{ {context.Write.Writer}.Write(','); {context.Write.Writer}.WritePropertyName((({itemTypeName}){enumerator}.Current).Key); {context.GetWrite(null, $"(({itemTypeName}){enumerator}.Current).Value", itemType)}; }} {context.Write.Writer}.Write('}}'); }}" ); } else { return($@" var {value} = {sourceCode}; if ({value} != null) {{ {context.Write.Object}.WritePropertyName(""{sourceName}""); {context.Write.Writer}.Write('{{'); var {enumerator} = {value}.GetEnumerator(); if ({enumerator}.MoveNext()) {{ {context.Write.Writer}.WritePropertyName((({itemTypeName}){enumerator}.Current).Key); {context.GetWrite(null, $"(({itemTypeName}){enumerator}.Current).Value", itemType)}; }} while ({enumerator}.MoveNext()) {{ {context.Write.Writer}.Write(','); {context.Write.Writer}.WritePropertyName((({itemTypeName}){enumerator}.Current).Key); {context.GetWrite(null, $"(({itemTypeName}){enumerator}.Current).Value", itemType)}; }} {context.Write.Writer}.Write('}}'); }}" ); } }
public static string Write <T>(this IValueSerializationGeneratorContext context, string sourceName, string source) { return(context.GetWrite(sourceName, source, context.Roselyn.GetTypeByFullName(typeof(T).FullName))); }