Exemplo n.º 1
0
        private void WriteFieldType(ClassType classType, string propertyName, string paramName, int depth)
        {
            if (classType.CollectionType == CollectionType.NotACollection)
            {
                WriteBasicField(classType.Name, $"\"{propertyName}\"", paramName);
            }
            else if (classType.CollectionType == CollectionType.Enum)
            {
                writer.WriteLine($"output.WriteNumber(\"{propertyName}\", ({classType.EnumUnderlayingType.Name}){paramName});");
            }
            else if (classType.CollectionType == CollectionType.Array || classType.CollectionType == CollectionType.List)
            {
                var genericType = classType.GenericTypes.First();
                if (IsValidPropertyName(propertyName))
                {
                    writer.WriteLine($"output.WritePropertyName(\"{propertyName}\");");
                }

                if (this.manager.IsKnownClassOrBase(genericType.Name))
                {
                    writer.WriteLine($"Write({paramName}, output, Write);");
                }
                else
                {
                    if (TryGetReadListPrimitive(genericType.Name, out var jsonType))
                    {
                        writer.WriteLine($"Write({paramName}, output);");
                    }
                    else
                    {
                        var itemName = $"item{(depth == 0 ? "" : depth.ToString())}";

                        writer.WriteLine("output.WriteStartArray();");
                        WriteStart(ContainerType.Array, propertyName);
                        writer.WriteLine($"foreach (var {itemName} in {paramName})");
                        writer.WriteLine("{");
                        writer.Indent++;
                        WriteFieldType(classType.GenericTypes.First(), "", itemName, depth + 1);
                        writer.Indent--;
                        writer.WriteLine("}");
                        writer.WriteLine("output.WriteEndArray();");
                    }
                }
                writer.WriteLine();
            }
            else if (classType.CollectionType == CollectionType.Dictionary)
            {
                var itemName  = $"kvp{(depth == 0 ? "" : depth.ToString())}";
                var keyName   = $"{itemName}.Key";
                var valueName = $"{itemName}.Value";

                // Simple key in dictionary
                if (TryGetBasicJsonType(classType.GenericTypes[0].Name, out var jsonType))
                {
                    WriteStart(ContainerType.Object, propertyName);
                    writer.WriteLine($"foreach (var {itemName} in {paramName})");
                    writer.WriteLine("{");
                    writer.Indent++;

                    if (jsonType != "String")
                    {
                        keyName += ".ToString()";
                    }
                    WriteBasicField(classType.GenericTypes[1].Name, keyName, valueName);

                    writer.Indent--;
                    writer.WriteLine("}");
                    writer.WriteLine("output.WriteEndObject();");
                }
                // Complex key in dictionary
                else
                {
                    WriteStart(ContainerType.Array, propertyName);
                    writer.WriteLine($"foreach (var {itemName} in {paramName})");
                    writer.WriteLine("{");
                    writer.Indent++;

                    writer.WriteLine("output.WriteStartObject();");
                    writer.WriteLine($"output.WritePropertyName(\"key\");");
                    WriteFieldType(classType.GenericTypes[0], "key", keyName, depth + 1);

                    writer.WriteLine($"output.WritePropertyName(\"value\");");
                    WriteFieldType(classType.GenericTypes[1], "value", valueName, depth + 1);
                    writer.WriteLine("output.WriteEndObject();");

                    writer.Indent--;
                    writer.WriteLine("}");
                    writer.WriteLine("output.WriteEndArray();");
                }

                writer.WriteLine();
            }
        }
Exemplo n.º 2
0
        private string ReadFieldType(string input, string resultName, ClassType classType, int depth)
        {
            if (classType.CollectionType == CollectionType.NotACollection)
            {
                if (manager.IsKnownClassOrBase(classType.Name))
                {
                    var readName = this.MakeReadValueMethod(classType.Name);
                    if (classType.GenericTypes.Any())
                    {
                        var generics = this.MakeGenericType(classType);
                        return($"{readName}<{generics}>({input})");
                    }
                    return($"{readName}({input})");
                }
                else
                {
                    var primitiveType = CodeGeneratorUtils.GetPrimitiveName(classType.Name);
                    return($"{input}.Get{primitiveType}()");
                }
            }
            else if (classType.CollectionType == CollectionType.Enum)
            {
                var primitiveType = CodeGeneratorUtils.GetPrimitiveName(classType.EnumUnderlayingType.Name);
                return($"({classType.Name.Value}){input}.Get{primitiveType}()");
            }
            else if (classType.CollectionType == CollectionType.List || classType.CollectionType == CollectionType.Array)
            {
                var genericType     = classType.GenericTypes.First();
                var genericTypeName = this.MakeGenericType(classType);
                var depthStr        = depth == 0 ? "" : depth.ToString();
                resultName = CodeGeneratorUtils.ToCamelCase(resultName);
                var indexName = $"value{depthStr}";

                writer.WriteLine($"var {resultName}Json = {input};");
                if (manager.IsKnownClassOrBase(genericType.Name))
                {
                    var readName = this.MakeReadValueMethod(genericType.Name);
                    if (genericType.GenericTypes.Any())
                    {
                        var generics = this.MakeGenericType(genericType);
                        readName += $"<{generics}>";
                    }

                    writer.WriteLine($"var {resultName} = new {genericTypeName}(ReadList({resultName}Json, {readName}));");
                }
                else if (TryGetReadListPrimitive(genericType.Name, out var readListType))
                {
                    writer.WriteLine($"var {resultName} = new {genericTypeName}(ReadList{readListType}({resultName}Json));");
                }
                else
                {
                    writer.WriteLine($"var {resultName} = new {genericTypeName}();");
                    writer.WriteLine($"foreach (var {indexName} in {resultName}Json.EnumerateArray())");
                    writer.WriteLine("{");
                    writer.Indent++;
                    writer.WriteLine($"{resultName}.Add({ReadFieldType(indexName, resultName + (depth + 1), genericType, depth + 1)});");
                    writer.Indent--;
                    writer.WriteLine("}");
                }
                writer.WriteLine();

                return(resultName);
            }
            else if (classType.CollectionType == CollectionType.Dictionary)
            {
                var keyType     = classType.GenericTypes[0];
                var valueType   = classType.GenericTypes[1];
                var depthStr    = depth == 0 ? "" : depth.ToString();
                var indexName   = $"prop{depthStr}";
                var keyName     = $"key{depthStr}";
                var valueName   = $"value{depthStr}";
                var genericName = this.MakeGenericType(classType);
                resultName = CodeGeneratorUtils.ToCamelCase(resultName);


                writer.WriteLine($"var {resultName} = new {genericName}();");

                if (!TryGetBasicJsonType(classType.GenericTypes[0].Name, out var jsonType))
                {
                    var indexKeyName   = $"{indexName}.GetProperty(\"key\")";
                    var indexValueName = $"{indexName}.GetProperty(\"value\")";

                    writer.WriteLine($"foreach (var {indexName} in {input}.EnumerateArray())");
                    writer.WriteLine("{");
                    writer.Indent++;

                    writer.WriteLine($"var {keyName} = {ReadFieldType(indexKeyName, keyName, keyType, depth + 1)};");
                    writer.WriteLine($"var {valueName} = {ReadFieldType(indexValueName, valueName, valueType, depth + 1)};");

                    writer.WriteLine($"{resultName}[{keyName}] = {valueName};");

                    writer.Indent--;
                    writer.WriteLine("}");
                }
                else
                {
                    writer.WriteLine($"foreach (var {indexName} in {input}.EnumerateObject())");
                    writer.WriteLine("{");
                    writer.Indent++;

                    if (jsonType == "String")
                    {
                        writer.WriteLine($"var {keyName} = {indexName}.Name;");
                    }
                    else
                    {
                        var primitiveType = CodeGeneratorUtils.GetPrimitiveName(keyType.Name);
                        writer.WriteLine($"var {keyName} = Convert.To{primitiveType}({indexName}.Name);");
                    }
                    writer.WriteLine($"var {valueName} = {ReadFieldType($"{indexName}.Value", valueName, valueType, depth + 1)};");

                    writer.WriteLine($"{resultName}[{keyName}] = {valueName};");

                    writer.Indent--;
                    writer.WriteLine("}");
                }

                return(resultName);
            }

            return("OH NO");
        }
Exemplo n.º 3
0
        private string WriteFieldType(ClassType classType, string paramName, int depth)
        {
            if (classType.CollectionType == CollectionType.NotACollection)
            {
                return(WriteBasicField(classType.Name, paramName));
            }
            else if (classType.CollectionType == CollectionType.Enum)
            {
                return($"{classType.EnumUnderlayingType.Name}){paramName}");
            }
            else if (classType.CollectionType == CollectionType.Array || classType.CollectionType == CollectionType.List)
            {
                var genericType = classType.GenericTypes.First();

                if (this.manager.IsKnownClassOrBase(genericType.Name))
                {
                    return($"Write({paramName}, Write)");
                }
                else
                {
                    if (TryGetBasicJsonType(genericType.Name, out var jsonType))
                    {
                        return($"Write({paramName})");
                    }
                    else
                    {
                        var itemName = $"item{(depth == 0 ? "" : depth.ToString())}";
                        writer.WriteLine();
                        var arrayName = $"array{CodeGeneratorUtils.ToTitleCase(paramName.Replace(".", ""))}";

                        writer.WriteLine($"var {arrayName} = new JSONArray();");
                        writer.WriteLine($"foreach (var {itemName} in {paramName})");
                        writer.WriteLine("{");
                        writer.Indent++;

                        var arrayValue = WriteFieldType(classType.GenericTypes.First(), itemName, depth + 1);
                        writer.WriteLine($"{arrayName}.Add({arrayValue});");

                        writer.Indent--;
                        writer.WriteLine("}");

                        return(arrayName);
                    }
                }
            }
            else if (classType.CollectionType == CollectionType.Dictionary)
            {
                var itemName   = $"kvp{(depth == 0 ? "" : depth.ToString())}";
                var keyName    = $"{itemName}.Key";
                var valueName  = $"{itemName}.Value";
                var objectName = $"object{CodeGeneratorUtils.ToTitleCase(paramName.Replace(".", ""))}";

                writer.WriteLine();

                // Simple key in dictionary
                if (TryGetBasicJsonType(classType.GenericTypes[0].Name, out var jsonType))
                {
                    writer.WriteLine($"var {objectName} = new JSONObject();");
                    writer.WriteLine($"foreach (var {itemName} in {paramName})");
                    writer.WriteLine("{");
                    writer.Indent++;

                    var keyString   = WriteBasicField(classType.GenericTypes[0].Name, keyName);
                    var valueString = WriteBasicField(classType.GenericTypes[1].Name, valueName);

                    if (jsonType != "String")
                    {
                        keyString += ".ToString()";
                    }

                    writer.WriteLine($"{objectName}[{keyString}] = {valueString};");

                    writer.Indent--;
                    writer.WriteLine("}");
                }
                // Complex key in dictionary
                else
                {
                    writer.WriteLine($"var {objectName} = new JSONArray();");
                    writer.WriteLine($"foreach (var {itemName} in {paramName})");
                    writer.WriteLine("{");
                    writer.Indent++;

                    var keyPairName = $"keypair{(depth == 0 ? "" : depth.ToString())}";

                    writer.WriteLine($"var {keyPairName} = new JSONObject();");
                    writer.WriteLine($"{keyPairName}[\"key\"] = {WriteFieldType(classType.GenericTypes[0], keyName, depth + 1)};");
                    writer.WriteLine($"{keyPairName}[\"value\"] = {WriteFieldType(classType.GenericTypes[1], valueName, depth + 1)};");

                    writer.WriteLine($"{objectName}.Add({keyPairName});");

                    writer.Indent--;
                    writer.WriteLine("}");
                }

                return(objectName);
            }

            return("OH NO");
        }