Exemple #1
0
        public void GenerateClasses()
        {
            if (CodeWriter == null)
            {
                CodeWriter = new CSharpCodeWriter();
            }
            if (ExplicitDeserialization && !(CodeWriter is CSharpCodeWriter))
            {
                throw new ArgumentException("Explicit deserialization is obsolete and is only supported by the C# provider.");
            }

            if (used)
            {
                throw new InvalidOperationException("This instance of JsonClassGenerator has already been used. Please create a new instance.");
            }

            used = true;

            var writeToDisk = TargetFolder != null;

            if (writeToDisk && !Directory.Exists(TargetFolder))
            {
                Directory.CreateDirectory(TargetFolder);
            }

            JObject[] examples;
            var       example = Example.StartsWith("HTTP/") ? Example.Substring(Example.IndexOf("\r\n\r\n")) : Example;

            using (var sr = new StringReader(example))
                using (var reader = new JsonTextReader(sr))
                {
                    var json = JToken.ReadFrom(reader);
                    if (json is JArray)
                    {
                        examples = ((JArray)json).Cast <JObject>().ToArray();
                    }
                    else if (json is JObject)
                    {
                        examples = new[] { (JObject)json };
                    }
                    else
                    {
                        throw new Exception("Sample JSON must be either a JSON array, or a JSON object.");
                    }
                }

            Types = new List <JsonType>();
            Names.Add(MainClass);

            var rootType = new JsonType(this, examples[0]);

            rootType.IsRoot = true;
            rootType.AssignName(MainClass);
            GenerateClass(examples, rootType);

            //if (writeToDisk)
            //{
            //    var parentFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            //    if (writeToDisk && !NoHelperClass && ExplicitDeserialization)
            //        File.WriteAllBytes(Path.Combine(TargetFolder, "JsonClassHelper.cs"), Properties.Resources.JsonClassHelper);

            //    if (SingleFile)
            //    {
            //        WriteClassesToFile(Path.Combine(TargetFolder, MainClass + CodeWriter.FileExtension), Types);
            //    }
            //    else
            //    {

            //        foreach (var type in Types)
            //        {
            //            var folder = TargetFolder;
            //            if (!UseNestedClasses && !type.IsRoot && SecondaryNamespace != null)
            //            {
            //                var s = SecondaryNamespace;
            //                if (s.StartsWith(Namespace + ".")) s = s.Substring(Namespace.Length + 1);
            //                folder = Path.Combine(folder, s);
            //                Directory.CreateDirectory(folder);
            //            }
            //            WriteClassesToFile(Path.Combine(folder, (UseNestedClasses && !type.IsRoot ? MainClass + "." : string.Empty) + type.AssignedName + CodeWriter.FileExtension), new[] { type });
            //        }
            //    }
            //}
            //else
            if (OutputStream != null)
            {
                WriteClassesToFile(OutputStream, Types);
            }
        }
Exemple #2
0
        public void GenerateClasses()
        {
            if (CodeWriter == null) CodeWriter = new CSharpCodeWriter();
            if (ExplicitDeserialization && !(CodeWriter is CSharpCodeWriter))
                throw new ArgumentException("Explicit deserialization is obsolete and is only supported by the C# provider.");

            if (used)
                throw new InvalidOperationException("This instance of JsonClassGenerator has already been used. Please create a new instance.");

            used = true;

            var writeToDisk = TargetFolder != null;
            if (writeToDisk && !Directory.Exists(TargetFolder)) Directory.CreateDirectory(TargetFolder);

            JObject[] examples;
            var example = Example.StartsWith("HTTP/") ? Example.Substring(Example.IndexOf("\r\n\r\n")) : Example;
            using (var sr = new StringReader(example))
            using (var reader = new JsonTextReader(sr))
            {
                var json = JToken.ReadFrom(reader);
                if (json is JArray)
                {
                    examples = ((JArray)json).Cast<JObject>().ToArray();
                }
                else if (json is JObject)
                {
                    examples = new[] { (JObject)json };
                }
                else
                {
                    throw new Exception("Sample JSON must be either a JSON array, or a JSON object.");
                }
            }

            Types = new List<JsonType>();
            Names.Add(MainClass);

            var rootType = new JsonType(this, examples[0]);
            rootType.IsRoot = true;
            rootType.AssignName(MainClass);
            GenerateClass(examples, rootType);

            //if (writeToDisk)
            //{
            //    var parentFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            //    if (writeToDisk && !NoHelperClass && ExplicitDeserialization)
            //        File.WriteAllBytes(Path.Combine(TargetFolder, "JsonClassHelper.cs"), Properties.Resources.JsonClassHelper);

            //    if (SingleFile)
            //    {
            //        WriteClassesToFile(Path.Combine(TargetFolder, MainClass + CodeWriter.FileExtension), Types);
            //    }
            //    else
            //    {

            //        foreach (var type in Types)
            //        {
            //            var folder = TargetFolder;
            //            if (!UseNestedClasses && !type.IsRoot && SecondaryNamespace != null)
            //            {
            //                var s = SecondaryNamespace;
            //                if (s.StartsWith(Namespace + ".")) s = s.Substring(Namespace.Length + 1);
            //                folder = Path.Combine(folder, s);
            //                Directory.CreateDirectory(folder);
            //            }
            //            WriteClassesToFile(Path.Combine(folder, (UseNestedClasses && !type.IsRoot ? MainClass + "." : string.Empty) + type.AssignedName + CodeWriter.FileExtension), new[] { type });
            //        }
            //    }
            //}
            //else
            if (OutputStream != null)
            {
                WriteClassesToFile(OutputStream, Types);
            }
        }