Exemplo n.º 1
0
        public static void SerializeObject <T>(T value, TextWriter textWriter, JsonFormatterOptions options)
        {
            if ((options & ReferenceOptions) != 0)
            {
                var jsonSerializer = new JsonReferenceSerializer(options);

                jsonSerializer.textWriter = textWriter;

                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.indentedChars = DefaultIndentedChars;
                    jsonSerializer.lineBreak     = DefaultLineBreak;
                    jsonSerializer.middleChars   = DefaultMiddleChars;
                }

                ValueInterface <T> .Content.WriteValue(jsonSerializer, value);

                jsonSerializer.WriteTo(textWriter);

                return;
            }

            if ((options & JsonFormatterOptions.PriorCheckReferences) != 0)
            {
                options ^= JsonFormatterOptions.PriorCheckReferences;
            }

            if (options == JsonFormatterOptions.Default)
            {
                var jsonSerializer = new JsonDefaultSerializer(DefaultMaxDepth);

                jsonSerializer.textWriter = textWriter;

                ValueInterface <T> .Content.WriteValue(jsonSerializer, value);

                jsonSerializer.WriteTo(textWriter);
            }
            else
            {
                var jsonSerializer = new JsonSerializer(options, DefaultMaxDepth);

                jsonSerializer.textWriter = textWriter;

                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.indentedChars = DefaultIndentedChars;
                    jsonSerializer.lineBreak     = DefaultLineBreak;
                    jsonSerializer.middleChars   = DefaultMiddleChars;
                }

                ValueInterface <T> .Content.WriteValue(jsonSerializer, value);

                jsonSerializer.WriteTo(textWriter);
            }
        }
Exemplo n.º 2
0
        public void Serialize <T>(T value, TextWriter textWriter)
        {
            var options = Options;
            var id      = Id;

            if ((options & ReferenceOptions) != 0)
            {
                var jsonSerializer = new JsonReferenceSerializer(options);

                if (id != 0)
                {
                    jsonSerializer.id = id;
                }

                jsonSerializer.textWriter = textWriter;

                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.indentedChars = IndentedChars;
                    jsonSerializer.lineBreak     = LineBreak;
                    jsonSerializer.middleChars   = MiddleChars;
                }

                ValueInterface <T> .Content.WriteValue(jsonSerializer, value);

                jsonSerializer.WriteTo(textWriter);

                return;
            }

            if ((options & JsonFormatterOptions.PriorCheckReferences) != 0)
            {
                options ^= JsonFormatterOptions.PriorCheckReferences;
            }

            if (options == JsonFormatterOptions.Default)
            {
                var jsonSerializer = new JsonDefaultSerializer(MaxDepth);

                if (id != 0)
                {
                    jsonSerializer.id = id;
                }

                jsonSerializer.textWriter = textWriter;

                ValueInterface <T> .Content.WriteValue(jsonSerializer, value);

                jsonSerializer.WriteTo(textWriter);
            }
            else
            {
                var jsonSerializer = new JsonSerializer(options, MaxDepth);

                if (id != 0)
                {
                    jsonSerializer.id = id;
                }

                jsonSerializer.textWriter = textWriter;

                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.indentedChars = IndentedChars;
                    jsonSerializer.lineBreak     = LineBreak;
                    jsonSerializer.middleChars   = MiddleChars;
                }

                ValueInterface <T> .Content.WriteValue(jsonSerializer, value);

                jsonSerializer.WriteTo(textWriter);
            }
        }