public async Task <IActionResult> Get(Json2CSharpPostRequestDto vm)
        {
            try
            {
                var gen = new JsonClassGenerator();
                gen.UsePascalCase = vm.UsePascalCase;
                gen.UseProperties = vm.UseProperties;
                gen.Example       = vm.Example;
                gen.MainClass     = "MyJsonObject";

                gen.GenerateClasses();
                var csharpCodeWriter = new CSharpCodeWriter();
                var resultWriter     = new StringWriter();
                csharpCodeWriter.WriteFileStart(gen, resultWriter);
                foreach (var type in gen.Types)
                {
                    csharpCodeWriter.WriteClass(gen, resultWriter, type);
                }
                csharpCodeWriter.WriteFileEnd(gen, resultWriter);

                return(Ok(new Json2CSharpPostResponseDto()
                {
                    Result = resultWriter.ToString()
                }));
            }
            catch (Exception e)
            {
                return(BadRequest());
            }
        }
Ejemplo n.º 2
0
        public JsonResult OnPost()
        {
            APIRequest apiRequest = new APIRequest();

            if (Request.Form.ContainsKey("UsePascalCase"))
            {
                var resultWriter     = new StringWriter();
                var csharpCodeWriter = new CSharpCodeWriter();
                try
                {
                    Json2CSharpPostRequestDto vm = new Json2CSharpPostRequestDto();
                    vm.UsePascalCase      = bool.Parse(Request.Form["UsePascalCase"]);
                    vm.UseProperties      = bool.Parse(Request.Form["UseProperties"]);
                    vm.EscapedDoubleQuote = bool.Parse(Request.Form["EscapedDoubleQuote"]);

                    if (vm.EscapedDoubleQuote)
                    {
                        //Unescape double quote
                        vm.Example = Request.Form["Example"].ToString().Replace("\\\"", "\"");
                    }
                    else
                    {
                        vm.Example = Request.Form["Example"];
                    }

                    var gen = new JsonClassGenerator();
                    gen.UsePascalCase = vm.UsePascalCase;
                    gen.UseProperties = vm.UseProperties;
                    gen.Example       = vm.Example;
                    gen.MainClass     = "MyJsonObject";

                    gen.GenerateClasses();


                    csharpCodeWriter.WriteFileStart(gen, resultWriter);
                    foreach (var type in gen.Types)
                    {
                        csharpCodeWriter.WriteClass(gen, resultWriter, type);
                    }
                    csharpCodeWriter.WriteFileEnd(gen, resultWriter);

                    apiRequest.status = 0;
                    apiRequest.data   = resultWriter.ToString();
                }
                catch (Exception ex)
                {
                    apiRequest.status   = -1;
                    apiRequest.debugmsg = ex.Message;
                }
            }
            else
            {
                //invalid request
                apiRequest.status  = -1;
                apiRequest.message = "Invalid Request";
            }

            return(new JsonResult(apiRequest));
        }