Beispiel #1
0
        public void GenerateClasses(string nameSpace)
        {
            try
            {
                var className = Name.SanitiseClassName();

                var finalNamespace = nameSpace + "." + className + "Input";
                var outputStream   = new MemoryStream();
                var outputWriter   = new StreamWriter(outputStream);

                var jsg = new JsonClassGenerator
                {
                    Example                = Json,
                    Namespace              = finalNamespace,
                    MainClass              = className,
                    OutputStream           = outputWriter,
                    NoHelperClass          = true,
                    UseProperties          = true,
                    GeneratePartialClasses = true
                };

                jsg.GenerateClasses();

                outputWriter.Flush();
                outputStream.Seek(0, SeekOrigin.Begin);

                var classDef = new StreamReader(outputStream)
                               .ReadToEnd()
                               .Replace("IList<", "List<");

                classDef =
                    classDef.Substring(classDef.IndexOf(String.Format("namespace {0}", nameSpace),
                                                        StringComparison.Ordinal));

                NamespacesToAdd.Add(finalNamespace);

                _generatedClass = new JsonTextGeneratedClass(this)
                {
                    Namespace       = finalNamespace,
                    ClassName       = className,
                    ClassDefinition = classDef,
                    Success         = true
                };
            }
            catch (Exception e)
            {
                _generatedClass = new JsonTextGeneratedClass(this)
                {
                    Success = false,
                    Error   = e
                };
            }
        }
        public void GenerateClasses(string nameSpace)
        {
            var numSamples = NumRowsToSample;

            _generatedClasses =
                GetInputFiles()
                .Select(f =>
            {
                // TODO: Be a better error handler
                try
                {
                    var fs  = new FileStream(f, FileMode.Open);
                    var sr  = new StreamReader(fs);
                    var jtr = new JsonTextReader(sr);

                    var examples =
                        Enumerable
                        .Range(0, numSamples)
                        .Select(_ =>
                    {
                        while (jtr.Read())
                        {
                            if (jtr.TokenType == JsonToken.StartObject)
                            {
                                return(JObject.Load(jtr).ToString());
                            }
                        }
                        return(null);
                    })
                        .Where(json => json != null);

                    var examplesJson = String.Format("[{0}]", String.Join(",\r\n", examples));

                    jtr.Close();
                    sr.Close();
                    fs.Close();

                    var className      = Path.GetFileNameWithoutExtension(f).SanitiseClassName();
                    var finalNamespace = nameSpace + "." + className + "Input";
                    var outputStream   = new MemoryStream();
                    var outputWriter   = new StreamWriter(outputStream);

                    var jsg = new JsonClassGenerator
                    {
                        Example                = examplesJson,
                        Namespace              = finalNamespace,
                        MainClass              = className,
                        OutputStream           = outputWriter,
                        NoHelperClass          = true,
                        UseProperties          = true,
                        GeneratePartialClasses = true
                    };

                    jsg.GenerateClasses();

                    outputWriter.Flush();
                    outputStream.Seek(0, SeekOrigin.Begin);

                    var classDef = new StreamReader(outputStream)
                                   .ReadToEnd()
                                   .Replace("IList<", "List<");

                    classDef =
                        classDef.Substring(classDef.IndexOf(String.Format("namespace {0}", nameSpace),
                                                            StringComparison.Ordinal));

                    NamespacesToAdd.Add(finalNamespace);

                    return(new JsonFileGeneratedClass(this)
                    {
                        Namespace = finalNamespace,
                        ClassName = className,
                        DataFilePath = f,
                        ClassDefinition = classDef,
                        Success = true
                    });
                }
                catch (Exception e)
                {
                    return(new JsonFileGeneratedClass(this)
                    {
                        DataFilePath = f,
                        Success = false,
                        Error = e
                    });
                }
            })
                .ToList();
        }
Beispiel #3
0
        public void GenerateClasses(string nameSpace)
        {
            try
            {
                var req = (HttpWebRequest)HttpWebRequest.Create(Url);
                req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
                req.UserAgent = @"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";

                var examplesJson = "";
                using (var sr = new StreamReader(req.GetResponse().GetResponseStream()))
                    examplesJson = sr.ReadToEnd();

                var className      = Name.SanitiseClassName();
                var finalNamespace = nameSpace + "." + className + "Input";
                var outputStream   = new MemoryStream();
                var outputWriter   = new StreamWriter(outputStream);

                var jsg = new JsonClassGenerator
                {
                    Example                = examplesJson,
                    Namespace              = finalNamespace,
                    MainClass              = className,
                    OutputStream           = outputWriter,
                    NoHelperClass          = true,
                    UseProperties          = true,
                    GeneratePartialClasses = true
                };

                jsg.GenerateClasses();

                outputWriter.Flush();
                outputStream.Seek(0, SeekOrigin.Begin);

                var classDef = new StreamReader(outputStream)
                               .ReadToEnd()
                               .Replace("IList<", "List<");

                classDef =
                    classDef.Substring(classDef.IndexOf(String.Format("namespace {0}", nameSpace),
                                                        StringComparison.Ordinal));

                NamespacesToAdd.Add(finalNamespace);

                GeneratedClasses = new List <IGeneratedClass>
                {
                    new JsonUrlGeneratedClass(this)
                    {
                        Namespace       = finalNamespace,
                        ClassName       = className,
                        Url             = Url,
                        ClassDefinition = classDef,
                        Success         = true
                    }
                };
            }
            catch (Exception e)
            {
                GeneratedClasses = new List <IGeneratedClass>
                {
                    new JsonUrlGeneratedClass(this)
                    {
                        Url     = Url,
                        Success = false,
                        Error   = e
                    }
                };
            }
        }