private async Task WriteObjectPropertyDeserializationAsync( CodeWriter writer, IResultParserTypeDescriptor possibleType, string jsonElement, ITypeLookup typeLookup) { for (int i = 0; i < possibleType.ResultDescriptor.Fields.Count; i++) { IFieldDescriptor fieldDescriptor = possibleType.ResultDescriptor.Fields[i]; await writer.WriteIndentAsync().ConfigureAwait(false); if (fieldDescriptor.Type.NamedType().IsLeafType()) { ITypeInfo typeInfo = typeLookup.GetTypeInfo( fieldDescriptor.Type, true); string deserializeMethod = SerializerNameUtils.CreateDeserializerName( fieldDescriptor.Type); await writer.WriteAsync(deserializeMethod).ConfigureAwait(false); await writer.WriteAsync('(').ConfigureAwait(false); await writer.WriteAsync(jsonElement).ConfigureAwait(false); await writer.WriteAsync(", \"").ConfigureAwait(false); await writer.WriteAsync(fieldDescriptor.ResponseName).ConfigureAwait(false); await writer.WriteAsync("\")").ConfigureAwait(false); } else { await writer.WriteAsync("Parse").ConfigureAwait(false); await writer.WriteAsync(GetPathName(fieldDescriptor.Path)) .ConfigureAwait(false); await writer.WriteAsync('(').ConfigureAwait(false); await writer.WriteAsync(jsonElement).ConfigureAwait(false); await writer.WriteAsync(", \"").ConfigureAwait(false); await writer.WriteAsync(fieldDescriptor.ResponseName).ConfigureAwait(false); await writer.WriteAsync("\")").ConfigureAwait(false); } if (i < possibleType.ResultDescriptor.Fields.Count - 1) { await writer.WriteAsync(',').ConfigureAwait(false); } await writer.WriteLineAsync().ConfigureAwait(false); } }
private async Task WriteDeserializeMethodAsync( CodeWriter writer, IResultParserTypeDescriptor possibleType, ITypeLookup typeLookup, ISet <string> generatedMethods) { bool first = true; foreach (IType type in possibleType.ResultDescriptor.Fields .Where(t => t.Type.NamedType().IsLeafType()) .Select(t => t.Type)) { string methodName = SerializerNameUtils.CreateDeserializerName(type); if (generatedMethods.Add(methodName)) { Type serializationType = typeLookup.GetSerializationType(type); string serializerMethod = _jsonMethod[serializationType]; if (!first) { await writer.WriteLineAsync().ConfigureAwait(false); } first = false; if (type.IsListType() && type.ListType().ElementType.IsListType()) { // TODO : implement this throw new NotImplementedException(); } else if (type.IsListType() && type.ListType().ElementType.IsLeafType()) { await WriteDeserializeLeafListAsync( writer, typeLookup, methodName, type, type.NamedType().Name, serializerMethod) .ConfigureAwait(false); } else { await WriteDeserializeLeafAsync( writer, methodName, typeLookup.GetLeafClrTypeName(type), type.NamedType().Name, serializerMethod, type.IsNonNullType()) .ConfigureAwait(false); } } } }
private async Task <string> WriteTypeSerializerMethodAsync( CodeWriter writer, IType type, ISet <string> generatedMethods) { IType actualType = type.IsNonNullType() ? type.InnerType() : type; string serializerName = SerializerNameUtils.CreateSerializerName(actualType); if (!generatedMethods.Add(serializerName)) { return(serializerName); } if (actualType.IsListType()) { IType elementType = type.ElementType(); string itemSerializer = await WriteTypeSerializerMethodAsync( writer, elementType, generatedMethods) .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await WriteTypeSerializerMethodHeaderAsync(writer, serializerName) .ConfigureAwait(false); await writer.WriteIndentedLineAsync("{").ConfigureAwait(false); using (writer.IncreaseIndent()) { if (!type.IsNonNullType()) { await WriteNonNullHandlingAsync(writer).ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentedLineAsync( "IList source = (IList)value;") .ConfigureAwait(false); if (_languageVersion == LanguageVersion.CSharp_8_0) { await writer.WriteIndentedLineAsync( "object?[] result = new object?[source.Count];") .ConfigureAwait(false); } else { await writer.WriteIndentedLineAsync( "object[] result = new object[source.Count];") .ConfigureAwait(false); } await writer.WriteIndentedLineAsync( "for(int i = 0; i < source.Count; i++)") .ConfigureAwait(false); await writer.WriteIndentedLineAsync("{").ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentedLineAsync( $"result[i] = {itemSerializer}(source[i]);") .ConfigureAwait(false); } await writer.WriteIndentedLineAsync("}").ConfigureAwait(false); await writer.WriteIndentedLineAsync("return result;").ConfigureAwait(false); } await writer.WriteIndentedLineAsync("}").ConfigureAwait(false); } else { await WriteTypeSerializerMethodHeaderAsync(writer, serializerName) .ConfigureAwait(false); await writer.WriteIndentedLineAsync("{").ConfigureAwait(false); using (writer.IncreaseIndent()) { if (!type.IsNonNullType()) { await WriteNonNullHandlingAsync(writer).ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync( "return _") .ConfigureAwait(false); await writer.WriteAsync(GetFieldName(actualType.NamedType().Name)) .ConfigureAwait(false); await writer.WriteAsync( _languageVersion == LanguageVersion.CSharp_8_0 ? "Serializer!.Serialize(value);" : "Serializer.Serialize(value);") .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentedLineAsync("}").ConfigureAwait(false); } return(serializerName); }
private async Task WriteSerializeMethodAsync( CodeWriter writer, IInputClassDescriptor descriptor) { await writer.WriteIndentAsync().ConfigureAwait(false); if (_languageVersion == LanguageVersion.CSharp_8_0) { await writer.WriteAsync("public object? Serialize(object? value)") .ConfigureAwait(false); } else { await writer.WriteAsync("public object Serialize(object value)") .ConfigureAwait(false); } await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync('{').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentedLineAsync( "if (_needsInitialization)") .ConfigureAwait(false); await writer.WriteIndentedLineAsync("{") .ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentedLineAsync( "throw new InvalidOperationException(") .ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentedLineAsync( "$\"The serializer for type `{Name}` has not been initialized.\");") .ConfigureAwait(false); } } await writer.WriteIndentedLineAsync("}") .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await WriteNonNullHandlingAsync(writer).ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync($"var input = ({descriptor.Name})value;") .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); if (_languageVersion == LanguageVersion.CSharp_8_0) { await writer.WriteAsync("var map = new Dictionary<string, object?>();") .ConfigureAwait(false); } else { await writer.WriteAsync("var map = new Dictionary<string, object>();") .ConfigureAwait(false); } await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); foreach (IInputFieldDescriptor field in descriptor.Fields) { IType type = field.Type.IsNonNullType() ? field.Type.InnerType() : field.Type; string serializerName = SerializerNameUtils.CreateSerializerName(type); await writer.WriteIndentedLineAsync( $"if (input.{GetPropertyName(field.Name)}.HasValue)") .ConfigureAwait(false); await writer.WriteIndentedLineAsync("{").ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync($"map.Add(\"{field.Field.Name}\", ") .ConfigureAwait(false); await writer.WriteAsync($"{serializerName}(").ConfigureAwait(false); await writer.WriteAsync($"input.{GetPropertyName(field.Name)}.Value));") .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentedLineAsync("}").ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync("return map;").ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync('}').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await WriteTypeSerializerMethodsAsync(writer, descriptor).ConfigureAwait(false); }