private void GenerateConstructorWithTextParameter(CodeTypeDeclaration classObject, JsonObject jsonObject) { CodeConstructor ctor = new CodeConstructor(); classObject.Members.Add(ctor); ctor.Attributes = MemberAttributes.Public; ctor.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "text")); // declare parser variable CodeVariableDeclarationStatement parserCreate = new CodeVariableDeclarationStatement( typeof(JsonTextParser), "parser", new CodeObjectCreateExpression(new CodeTypeReference(typeof(JsonTextParser)))); ctor.Statements.Add(parserCreate); // invoke Parse method on parser object CodeMethodInvokeExpression invokeParse = new CodeMethodInvokeExpression( new CodeMethodReferenceExpression(new CodeVariableReferenceExpression("parser"), "Parse"), new CodeVariableReferenceExpression("text")); // assign result of Parse method to RootObject CodeAssignStatement assignObject = new CodeAssignStatement( new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "RootObject"), new CodeCastExpression(new CodeTypeReference(jsonObject.GetType()), invokeParse)); ctor.Statements.Add(assignObject); ctor.Statements.Add(new CodeMethodReturnStatement()); }
private void GenerateDefaultConstructor(CodeTypeDeclaration classObject, JsonObject jsonObject) { CodeConstructor constructor = new CodeConstructor(); classObject.Members.Add(constructor); constructor.Attributes = MemberAttributes.Public; CodeAssignStatement statement = new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "RootObject"), new CodeObjectCreateExpression(jsonObject.GetType(), new CodeExpression[0])); constructor.Statements.Add(statement); }
public void GenerateLibrary(string objectName, JsonObject jsonObject, string path) { CodeCompileUnit unit = new CodeCompileUnit(); CodeNamespace namespace2 = new CodeNamespace("System.Net.Json.Generated"); namespace2.Imports.Add(new CodeNamespaceImport("System.Net.Json")); unit.ReferencedAssemblies.Add("System.Net.Json.dll"); unit.Namespaces.Add(namespace2); CodeTypeDeclaration declaration = new CodeTypeDeclaration(objectName); namespace2.Types.Add(declaration); declaration.TypeAttributes = TypeAttributes.Public; CodeMemberField field = new CodeMemberField(jsonObject.GetType(), "RootObject"); field.Attributes = MemberAttributes.Family; declaration.Members.Add(field); this.GenerateToStringDefaultMethod(declaration); this.GenerateDefaultConstructor(declaration, jsonObject); this.GenerateConstructorWithTextParameter(declaration, jsonObject); this.GenerateParseStaticMethod(declaration); if (typeof(JsonObjectCollection) != jsonObject.GetType()) { throw new NotImplementedException("Only objects supported in root level, not arrays or other variables."); } this.GenerateObjectCollection(declaration, (JsonObjectCollection)jsonObject); CodeDomProvider provider = CodeDomProvider.CreateProvider("cs"); CompilerParameters options = new CompilerParameters(); options.GenerateExecutable = false; if (!string.IsNullOrEmpty(path)) { options.OutputAssembly = Path.ChangeExtension(Path.Combine(path, objectName), ".dll"); } else { options.OutputAssembly = Path.ChangeExtension(objectName, ".dll"); } options.IncludeDebugInformation = false; CompilerResults results = provider.CompileAssemblyFromDom(options, new CodeCompileUnit[] { unit }); if (results.NativeCompilerReturnValue != 0) { throw new GeneratorException("Cannot compile your library.\r\nPlease send json text from which you trying to generate library to [email protected]", results); } }
public void GenerateLibrary(string objectName, JsonObject jsonObject, string path) { CodeCompileUnit unit = new CodeCompileUnit(); CodeNamespace namespace2 = new CodeNamespace("System.Net.Json.Generated"); namespace2.Imports.Add(new CodeNamespaceImport("System.Net.Json")); unit.ReferencedAssemblies.Add("System.Net.Json.dll"); unit.Namespaces.Add(namespace2); CodeTypeDeclaration declaration = new CodeTypeDeclaration(objectName); namespace2.Types.Add(declaration); declaration.TypeAttributes = TypeAttributes.Public; CodeMemberField field = new CodeMemberField(jsonObject.GetType(), "RootObject"); field.Attributes = MemberAttributes.Family; declaration.Members.Add(field); this.GenerateToStringDefaultMethod(declaration); this.GenerateDefaultConstructor(declaration, jsonObject); this.GenerateConstructorWithTextParameter(declaration, jsonObject); this.GenerateParseStaticMethod(declaration); if (typeof(JsonObjectCollection) != jsonObject.GetType()) { throw new NotImplementedException("Only objects supported in root level, not arrays or other variables."); } this.GenerateObjectCollection(declaration, (JsonObjectCollection)jsonObject); CodeDomProvider provider = CodeDomProvider.CreateProvider("cs"); CompilerParameters options = new CompilerParameters(); options.GenerateExecutable = false; if (!string.IsNullOrEmpty(path)) { options.OutputAssembly = Path.ChangeExtension(Path.Combine(path, objectName), ".dll"); } else { options.OutputAssembly = Path.ChangeExtension(objectName, ".dll"); } options.IncludeDebugInformation = false; CompilerResults results = provider.CompileAssemblyFromDom(options, new CodeCompileUnit[] { unit }); if (results.NativeCompilerReturnValue != 0) { throw new GeneratorException("Cannot compile your library.\r\nPlease send json text from which you trying to generate library to [email protected]", results); } }
private void GenerateConstructorWithTextParameter(CodeTypeDeclaration classObject, JsonObject jsonObject) { CodeConstructor constructor = new CodeConstructor(); classObject.Members.Add(constructor); constructor.Attributes = MemberAttributes.Public; constructor.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "text")); CodeVariableDeclarationStatement statement = new CodeVariableDeclarationStatement(typeof(JsonTextParser), "parser", new CodeObjectCreateExpression(new CodeTypeReference(typeof(JsonTextParser)), new CodeExpression[0])); constructor.Statements.Add(statement); CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeVariableReferenceExpression("parser"), "Parse"), new CodeExpression[] { new CodeVariableReferenceExpression("text") }); CodeAssignStatement statement2 = new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "RootObject"), new CodeCastExpression(new CodeTypeReference(jsonObject.GetType()), expression)); constructor.Statements.Add(statement2); constructor.Statements.Add(new CodeMethodReturnStatement()); }
private void GenerateDefaultConstructor(CodeTypeDeclaration classObject, JsonObject jsonObject) { CodeConstructor ctor = new CodeConstructor(); classObject.Members.Add(ctor); ctor.Attributes = MemberAttributes.Public; //RootObject = new JsonObjectCollection(); CodeAssignStatement assignRootObject = new CodeAssignStatement( new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "RootObject"), new CodeObjectCreateExpression(jsonObject.GetType())); ctor.Statements.Add(assignRootObject); //JsonStringValue name = new JsonStringValue("Name"); //JsonStringValue surName = new JsonStringValue("SurName"); //RootObject.Add(name); //RootObject.Add(surName); }
private void GenerateDefaultConstructor(CodeTypeDeclaration classObject, JsonObject jsonObject) { CodeConstructor constructor = new CodeConstructor(); classObject.Members.Add(constructor); constructor.Attributes = MemberAttributes.Public; CodeAssignStatement statement = new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "RootObject"), new CodeObjectCreateExpression(jsonObject.GetType(), new CodeExpression[0])); constructor.Statements.Add(statement); }
private void GenerateConstructorWithTextParameter(CodeTypeDeclaration classObject, JsonObject jsonObject) { CodeConstructor constructor = new CodeConstructor(); classObject.Members.Add(constructor); constructor.Attributes = MemberAttributes.Public; constructor.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "text")); CodeVariableDeclarationStatement statement = new CodeVariableDeclarationStatement(typeof(JsonTextParser), "parser", new CodeObjectCreateExpression(new CodeTypeReference(typeof(JsonTextParser)), new CodeExpression[0])); constructor.Statements.Add(statement); CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeVariableReferenceExpression("parser"), "Parse"), new CodeExpression[] { new CodeVariableReferenceExpression("text") }); CodeAssignStatement statement2 = new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "RootObject"), new CodeCastExpression(new CodeTypeReference(jsonObject.GetType()), expression)); constructor.Statements.Add(statement2); constructor.Statements.Add(new CodeMethodReturnStatement()); }
/// <summary> /// Only his method is public /// </summary> /// <param name="objectName"></param> /// <param name="jsonObject"></param> public void GenerateLibrary(string objectName, JsonObject jsonObject, string path) { CodeCompileUnit ccu = new CodeCompileUnit(); CodeNamespace ns = new CodeNamespace("System.Net.Json.Generated"); ns.Imports.Add(new CodeNamespaceImport("System.Net.Json")); ccu.ReferencedAssemblies.Add(@"System.Net.Json.dll"); ccu.Namespaces.Add(ns); // root class CodeTypeDeclaration rootClass = new CodeTypeDeclaration(objectName); ns.Types.Add(rootClass); rootClass.TypeAttributes = TypeAttributes.Class | TypeAttributes.Public; // root object CodeMemberField rootObject = new CodeMemberField(jsonObject.GetType(), "RootObject"); rootObject.Attributes = MemberAttributes.Family; rootClass.Members.Add(rootObject); // [rootClass].ToString() method GenerateToStringDefaultMethod(rootClass); // .ctor() GenerateDefaultConstructor(rootClass, jsonObject); // .ctor(string text) GenerateConstructorWithTextParameter(rootClass, jsonObject); // static Parse(string text) GenerateParseStaticMethod(rootClass); // generate nested data. if (typeof(JsonObjectCollection) == jsonObject.GetType()) { GenerateObjectCollection(rootClass, (JsonObjectCollection)jsonObject); } else { throw new NotImplementedException("Only objects supported in root level, not arrays or other variables."); } // prepare for compile CodeDomProvider cdp = CodeDomProvider.CreateProvider("cs"); CompilerParameters cp = new CompilerParameters(); cp.GenerateExecutable = false; if (!string.IsNullOrEmpty(path)) { cp.OutputAssembly = System.IO.Path.ChangeExtension(System.IO.Path.Combine(path, objectName), ".dll"); } else { cp.OutputAssembly = System.IO.Path.ChangeExtension(objectName, ".dll"); } cp.IncludeDebugInformation = false; // compile code CompilerResults cr = cdp.CompileAssemblyFromDom(cp, ccu); if (cr.NativeCompilerReturnValue != 0) { throw new GeneratorException("Cannot compile your library.\r\n" + "Please send json text from which you trying to generate library to [email protected]", cr); } // show errors //Console.WriteLine("Returned code: " + cr.NativeCompilerReturnValue); //Console.WriteLine("Path: " + cr.PathToAssembly); //foreach (CompilerError e in cr.Errors) //{ //Console.WriteLine(e); //} }
private void GenerateDefaultConstructor(CodeTypeDeclaration classObject, JsonObject jsonObject) { CodeConstructor ctor = new CodeConstructor(); classObject.Members.Add(ctor); ctor.Attributes = MemberAttributes.Public; //RootObject = new JsonObjectCollection(); CodeAssignStatement assignRootObject = new CodeAssignStatement( new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "RootObject"), new CodeObjectCreateExpression(jsonObject.GetType())); ctor.Statements.Add(assignRootObject); //JsonStringValue name = new JsonStringValue("Name"); //JsonStringValue surName = new JsonStringValue("SurName"); //RootObject.Add(name); //RootObject.Add(surName); }
private void GenerateConstructorWithTextParameter(CodeTypeDeclaration classObject, JsonObject jsonObject) { CodeConstructor ctor = new CodeConstructor(); classObject.Members.Add(ctor); ctor.Attributes = MemberAttributes.Public; ctor.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "text")); // declare parser variable CodeVariableDeclarationStatement parserCreate = new CodeVariableDeclarationStatement( typeof(JsonTextParser), "parser", new CodeObjectCreateExpression(new CodeTypeReference(typeof(JsonTextParser)))); ctor.Statements.Add(parserCreate); // invoke Parse method on parser object CodeMethodInvokeExpression invokeParse = new CodeMethodInvokeExpression( new CodeMethodReferenceExpression(new CodeVariableReferenceExpression("parser"), "Parse"), new CodeVariableReferenceExpression("text")); // assign result of Parse method to RootObject CodeAssignStatement assignObject = new CodeAssignStatement( new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "RootObject"), new CodeCastExpression(new CodeTypeReference(jsonObject.GetType()), invokeParse)); ctor.Statements.Add(assignObject); ctor.Statements.Add(new CodeMethodReturnStatement()); }
private static Object ParseJson(JsonObject jsonObject) { Type type = jsonObject.GetType(); if (type == typeof(JsonObjectCollection)) { Hashtable val = new Hashtable(); foreach (JsonObject subObj in (jsonObject as JsonObjectCollection)) { val.Add(subObj.Name, ParseJson(subObj)); } if (val.ContainsKey("__DataType")) { if (val["__DataType"] as string == "Date") { return BaseDateTime.AddMilliseconds((Double)val["__Value"]); } else if (val["__DataType"] as string == "Exception") { return new Exception((val["__Value"] as Hashtable)["Message"] as string); } else { return val; } } else { return val; } } else if (type == typeof(JsonArrayCollection)) { List<object> val = new List<object>(); foreach (JsonObject subObj in (jsonObject as JsonArrayCollection)) { val.Add(ParseJson(subObj)); } return val.ToArray(); } else if (type == typeof(JsonBooleanValue)) { return jsonObject.GetValue(); } else if (type == typeof(JsonStringValue)) { return jsonObject.GetValue(); } else if (type == typeof(JsonNumericValue)) { return jsonObject.GetValue(); } else return null; }
/// <summary> /// Only his method is public /// </summary> /// <param name="objectName"></param> /// <param name="jsonObject"></param> public void GenerateLibrary(string objectName, JsonObject jsonObject, string path) { CodeCompileUnit ccu = new CodeCompileUnit(); CodeNamespace ns = new CodeNamespace("System.Net.Json.Generated"); ns.Imports.Add(new CodeNamespaceImport("System.Net.Json")); ccu.ReferencedAssemblies.Add(@"System.Net.Json.dll"); ccu.Namespaces.Add(ns); // root class CodeTypeDeclaration rootClass = new CodeTypeDeclaration(objectName); ns.Types.Add(rootClass); rootClass.TypeAttributes = TypeAttributes.Class | TypeAttributes.Public; // root object CodeMemberField rootObject = new CodeMemberField(jsonObject.GetType(), "RootObject"); rootObject.Attributes = MemberAttributes.Family; rootClass.Members.Add(rootObject); // [rootClass].ToString() method GenerateToStringDefaultMethod(rootClass); // .ctor() GenerateDefaultConstructor(rootClass, jsonObject); // .ctor(string text) GenerateConstructorWithTextParameter(rootClass, jsonObject); // static Parse(string text) GenerateParseStaticMethod(rootClass); // generate nested data. if (typeof(JsonObjectCollection) == jsonObject.GetType()) { GenerateObjectCollection(rootClass, (JsonObjectCollection)jsonObject); } else { throw new NotImplementedException("Only objects supported in root level, not arrays or other variables."); } // prepare for compile CodeDomProvider cdp = CodeDomProvider.CreateProvider("cs"); CompilerParameters cp = new CompilerParameters(); cp.GenerateExecutable = false; if (!string.IsNullOrEmpty(path)) { cp.OutputAssembly = System.IO.Path.ChangeExtension(System.IO.Path.Combine(path, objectName), ".dll"); } else { cp.OutputAssembly = System.IO.Path.ChangeExtension(objectName, ".dll"); } cp.IncludeDebugInformation = false; // compile code CompilerResults cr = cdp.CompileAssemblyFromDom(cp, ccu); if (cr.NativeCompilerReturnValue != 0) { throw new GeneratorException("Cannot compile your library.\r\n" + "Please send json text from which you trying to generate library to [email protected]", cr); } // show errors //Console.WriteLine("Returned code: " + cr.NativeCompilerReturnValue); //Console.WriteLine("Path: " + cr.PathToAssembly); //foreach (CompilerError e in cr.Errors) //{ //Console.WriteLine(e); //} }