Example #1
0
        public void BasicTest()
        {
            using var sw = new StringWriter();
            using var iw = new IndentedWriter(sw)
                  {
                      IndentString = "#", NewLine = "\n"
                  };

            Assert.Equal("\n", iw.NewLine);
            Assert.Equal(sw.Encoding, iw.Encoding);

            iw.WriteLine("line1");
            iw.Write("line");
            iw.IncreaseIndent();
            iw.WriteLine("2");
            iw.Write("line");
            iw.Write('3');
            iw.WriteLine();
            iw.IncreaseIndent(2);
            iw.WriteLine("line4");
            iw.DecreaseIndent();
            iw.WriteLine("line5");
            iw.DecreaseIndent(6);
            iw.WriteLine("line6");

            Assert.Equal("line1\nline2\n#line3\n###line4\n##line5\nline6\n", sw.ToString());
        }
Example #2
0
        private void Object_BuildPrettyString(IndentedWriter writer)
        {
            writer.Write("{");
            writer.Indent++;

            var keys = Keys.AssertCast <String>();

            if (keys.IsNotEmpty())
            {
                keys.ForEach((key, i) =>
                {
                    writer.WriteLine();
                    writer.Write("\"{0}\" : ", key);
                    if (((IEnumerable)this[key].Keys).Cast <Object>().Count() > 0)
                    {
                        writer.WriteLine();
                    }
                    this[key].BuildPrettyString(writer);
                    if (i < this.Count() - 1)
                    {
                        writer.Write(",");
                    }
                });

                writer.WriteLine();
            }

            writer.Indent--;
            writer.Write("}");
        }
        private void GenerateModelClasses()
        {
            foreach (var c in _model.Classes)
            {
                if (_model.Public)
                {
                    _w.Write("public ");
                }

                _w.WriteLine("class {0}", c.Name);
                _w.WriteLine("{").Push();
                _w.WriteLine("public StringTreeNode _rawTreeNode;");

                foreach (var f in c.Fields)
                {
                    var typeName = f.Name.Trim('@');

                    if (f.IsCollection)
                    {
                        typeName += "[]";
                    }

                    _w.WriteLine("public {0} {1}{2};", typeName, f.Name, f.IsCollection ? "s" : string.Empty);
                }

                _w.Pop().WriteLine("}");
                _w.WriteLine();
            }
        }
Example #4
0
        private void Array_BuildPrettyString(IndentedWriter writer)
        {
            writer.Write("[");
            writer.Indent++;

            var keys = Keys.AssertCast <int>();

            if (keys.IsNotEmpty())
            {
                keys.ForEach((key, i) =>
                {
                    (key == i).AssertTrue();
                    // todo. omg what's with these dynamic invocations?!
                    if (((IEnumerable)this[key].Keys).Cast <Object>().Count() > 0)
                    {
                        writer.WriteLine();
                    }
                    this[key].BuildPrettyString(writer);
                    if (i < this.Count() - 1)
                    {
                        writer.Write(",");
                    }
                });

                writer.WriteLine();
            }

            writer.Indent--;
            writer.Write("]");
        }
Example #5
0
 internal override void Stringify(JsonStringifyOption config, IndentedWriter iw)
 {
     if (IsFloat)
     {
         iw.Write(ValueFloat);
     }
     else
     {
         iw.Write(ValueInt);
     }
 }
Example #6
0
 private void PrintIndent()
 {
     foreach (var item in _indents)
     {
         _w.Write(item);
     }
 }
Example #7
0
        internal static bool GenerateSerializerToStream(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, Assembly assembly, Hashtable assemblies, Stream stream)
        {
            var compiler = new Compiler();

            try
            {
                var scopeTable = new Hashtable();
                foreach (XmlMapping mapping in xmlMappings)
                {
                    scopeTable[mapping.Scope] = mapping;
                }

                var scopes = new TypeScope[scopeTable.Keys.Count];
                scopeTable.Keys.CopyTo(scopes, 0);
                assemblies.Clear();
                var importedTypes = new Hashtable();

                foreach (TypeScope scope in scopes)
                {
                    foreach (Type t in scope.Types)
                    {
                        compiler.AddImport(t, importedTypes);
                        Assembly a    = t.Assembly;
                        string   name = a.FullName;
                        if (assemblies[name] != null)
                        {
                            continue;
                        }

                        if (!a.GlobalAssemblyCache)
                        {
                            assemblies[name] = a;
                        }
                    }
                }

                for (int i = 0; i < types.Length; i++)
                {
                    compiler.AddImport(types[i], importedTypes);
                }

                compiler.AddImport(typeof(object).Assembly);
                compiler.AddImport(typeof(System.Xml.Serialization.XmlSerializer).Assembly);
                var writer = new IndentedWriter(compiler.Source, false);
                writer.WriteLine("[assembly:System.Security.AllowPartiallyTrustedCallers()]");
                writer.WriteLine("[assembly:System.Security.SecurityTransparent()]");
                writer.WriteLine("[assembly:System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]");

                if (assembly != null && types.Length > 0)
                {
                    for (int i = 0; i < types.Length; i++)
                    {
                        Type type = types[i];
                        if (type == null)
                        {
                            continue;
                        }

                        if (DynamicAssemblies.IsTypeDynamic(type))
                        {
                            throw new InvalidOperationException(SR.Format(SR.XmlPregenTypeDynamic, types[i].FullName));
                        }
                    }

                    writer.Write("[assembly:");
                    writer.Write(typeof(XmlSerializerVersionAttribute).FullName);
                    writer.Write("(");
                    writer.Write("ParentAssemblyId=");
                    ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, GenerateAssemblyId(types[0]));
                    writer.Write(", Version=");
                    ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, ThisAssembly.Version);
                    if (defaultNamespace != null)
                    {
                        writer.Write(", Namespace=");
                        ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, defaultNamespace);
                    }

                    writer.WriteLine(")]");
                }

                var classes = new CodeIdentifiers();
                classes.AddUnique("XmlSerializationWriter", "XmlSerializationWriter");
                classes.AddUnique("XmlSerializationReader", "XmlSerializationReader");
                string suffix = null;

                if (types != null && types.Length == 1 && types[0] != null)
                {
                    suffix = CodeIdentifier.MakeValid(types[0].Name);
                    if (types[0].IsArray)
                    {
                        suffix += "Array";
                    }
                }

                writer.WriteLine("namespace " + GeneratedAssemblyNamespace + " {");
                writer.Indent++;
                writer.WriteLine();

                string writerClass = "XmlSerializationWriter" + suffix;
                writerClass = classes.AddUnique(writerClass, writerClass);
                var writerCodeGen = new XmlSerializationWriterCodeGen(writer, scopes, "public", writerClass);
                writerCodeGen.GenerateBegin();
                string[] writeMethodNames = new string[xmlMappings.Length];

                for (int i = 0; i < xmlMappings.Length; i++)
                {
                    writeMethodNames[i] = writerCodeGen.GenerateElement(xmlMappings[i]);
                }

                writerCodeGen.GenerateEnd();
                writer.WriteLine();

                string readerClass = "XmlSerializationReader" + suffix;
                readerClass = classes.AddUnique(readerClass, readerClass);
                var readerCodeGen = new XmlSerializationReaderCodeGen(writer, scopes, "public", readerClass);
                readerCodeGen.GenerateBegin();
                string[] readMethodNames = new string[xmlMappings.Length];
                for (int i = 0; i < xmlMappings.Length; i++)
                {
                    readMethodNames[i] = readerCodeGen.GenerateElement(xmlMappings[i]);
                }

                readerCodeGen.GenerateEnd(readMethodNames, xmlMappings, types);

                string baseSerializer = readerCodeGen.GenerateBaseSerializer("XmlSerializer1", readerClass, writerClass, classes);
                var    serializers    = new Hashtable();
                for (int i = 0; i < xmlMappings.Length; i++)
                {
                    if (serializers[xmlMappings[i].Key] == null)
                    {
                        serializers[xmlMappings[i].Key] = readerCodeGen.GenerateTypedSerializer(readMethodNames[i], writeMethodNames[i], xmlMappings[i], classes, baseSerializer, readerClass, writerClass);
                    }
                }

                readerCodeGen.GenerateSerializerContract("XmlSerializerContract", xmlMappings, types, readerClass, readMethodNames, writerClass, writeMethodNames, serializers);
                writer.Indent--;
                writer.WriteLine("}");

                string codecontent = compiler.Source.ToString();
                Byte[] info        = new UTF8Encoding(true).GetBytes(codecontent);
                stream.Write(info, 0, info.Length);
                stream.Flush();
                return(true);
            }
            finally
            {
                compiler.Close();
            }
        }
Example #8
0
 internal override void Stringify(JsonStringifyOption config, IndentedWriter iw)
 {
     iw.Write("null");
 }
Example #9
0
 internal override void Stringify(JsonStringifyOption config, IndentedWriter iw)
 {
     iw.Write('\"');
     iw.Write(config.HasFlag(JsonStringifyOption.AsciiOnly) ? EscapeSequenceConverter.EncodeToAscii(Value) : EscapeSequenceConverter.Encode(Value));
     iw.Write('\"');
 }
Example #10
0
 internal override void Stringify(JsonStringifyOption config, IndentedWriter iw)
 {
     iw.Write(Value ? "true" : "false");
 }
Example #11
0
 protected void Write(string value)
 {
     _writer.Write(value);
 }