Exemplo n.º 1
0
        public void TraceJsonWriterTest()
        {
            StringWriter    sw          = new StringWriter(CultureInfo.InvariantCulture);
            JsonTextWriter  w           = new JsonTextWriter(sw);
            TraceJsonWriter traceWriter = new TraceJsonWriter(w);

            traceWriter.WriteStartObject();
            traceWriter.WritePropertyName("Array");
            traceWriter.WriteStartArray();
            traceWriter.WriteValue("String!");
            traceWriter.WriteValue(new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc));
            traceWriter.WriteValue(new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.FromHours(2)));
            traceWriter.WriteValue(1.1f);
            traceWriter.WriteValue(1.1d);
            traceWriter.WriteValue(1.1m);
            traceWriter.WriteValue(1);
            traceWriter.WriteValue((char)'!');
            traceWriter.WriteValue((short)1);
            traceWriter.WriteValue((ushort)1);
            traceWriter.WriteValue((int)1);
            traceWriter.WriteValue((uint)1);
            traceWriter.WriteValue((sbyte)1);
            traceWriter.WriteValue((byte)1);
            traceWriter.WriteValue((long)1);
            traceWriter.WriteValue((ulong)1);
            traceWriter.WriteValue((bool)true);

            traceWriter.WriteValue((DateTime?)new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc));
            traceWriter.WriteValue((DateTimeOffset?)new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.FromHours(2)));
            traceWriter.WriteValue((float?)1.1f);
            traceWriter.WriteValue((double?)1.1d);
            traceWriter.WriteValue((decimal?)1.1m);
            traceWriter.WriteValue((int?)1);
            traceWriter.WriteValue((char?)'!');
            traceWriter.WriteValue((short?)1);
            traceWriter.WriteValue((ushort?)1);
            traceWriter.WriteValue((int?)1);
            traceWriter.WriteValue((uint?)1);
            traceWriter.WriteValue((sbyte?)1);
            traceWriter.WriteValue((byte?)1);
            traceWriter.WriteValue((long?)1);
            traceWriter.WriteValue((ulong?)1);
            traceWriter.WriteValue((bool?)true);
            traceWriter.WriteValue(BigInteger.Parse("9999999990000000000000000000000000000000000"));

            traceWriter.WriteValue((object)true);
            traceWriter.WriteValue(TimeSpan.FromMinutes(1));
            traceWriter.WriteValue(Guid.Empty);
            traceWriter.WriteValue(new Uri("http://www.google.com/"));
            traceWriter.WriteValue(Encoding.UTF8.GetBytes("String!"));
            traceWriter.WriteRawValue("[1],");
            traceWriter.WriteRaw("[1]");
            traceWriter.WriteNull();
            traceWriter.WriteUndefined();
            traceWriter.WriteStartConstructor("ctor");
            traceWriter.WriteValue(1);
            traceWriter.WriteEndConstructor();
            traceWriter.WriteComment("A comment");
            traceWriter.WriteWhitespace("       ");
            traceWriter.WriteEnd();
            traceWriter.WriteEndObject();
            traceWriter.Flush();
            traceWriter.Close();

            Console.WriteLine(traceWriter.GetJson());

            Assert.AreEqual(@"{
  ""Array"": [
    ""String!"",
    ""2000-12-12T12:12:12Z"",
    ""2000-12-12T12:12:12+02:00"",
    1.1,
    1.1,
    1.1,
    1,
    ""!"",
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    true,
    ""2000-12-12T12:12:12Z"",
    ""2000-12-12T12:12:12+02:00"",
    1.1,
    1.1,
    1.1,
    1,
    ""!"",
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    true,
    9999999990000000000000000000000000000000000,
    true,
    true,
    ""00:01:00"",
    ""00000000-0000-0000-0000-000000000000"",
    ""http://www.google.com/"",
    ""U3RyaW5nIQ=="",
    [1],[1],[1],
    null,
    undefined,
    new ctor(
      1
    )
    /*A comment*/       
  ]
}", traceWriter.GetJson());
        }
        internal virtual void SerializeInternal(JsonWriter jsonWriter, object value, Type objectType)
        {
            ValidationUtils.ArgumentNotNull(jsonWriter, "jsonWriter");

            // set serialization options onto writer
            Formatting?previousFormatting = null;

            if (_formatting != null && jsonWriter.Formatting != _formatting)
            {
                previousFormatting    = jsonWriter.Formatting;
                jsonWriter.Formatting = _formatting.Value;
            }

            DateFormatHandling?previousDateFormatHandling = null;

            if (_dateFormatHandling != null && jsonWriter.DateFormatHandling != _dateFormatHandling)
            {
                previousDateFormatHandling    = jsonWriter.DateFormatHandling;
                jsonWriter.DateFormatHandling = _dateFormatHandling.Value;
            }

            DateTimeZoneHandling?previousDateTimeZoneHandling = null;

            if (_dateTimeZoneHandling != null && jsonWriter.DateTimeZoneHandling != _dateTimeZoneHandling)
            {
                previousDateTimeZoneHandling    = jsonWriter.DateTimeZoneHandling;
                jsonWriter.DateTimeZoneHandling = _dateTimeZoneHandling.Value;
            }

            FloatFormatHandling?previousFloatFormatHandling = null;

            if (_floatFormatHandling != null && jsonWriter.FloatFormatHandling != _floatFormatHandling)
            {
                previousFloatFormatHandling    = jsonWriter.FloatFormatHandling;
                jsonWriter.FloatFormatHandling = _floatFormatHandling.Value;
            }

            StringEscapeHandling?previousStringEscapeHandling = null;

            if (_stringEscapeHandling != null && jsonWriter.StringEscapeHandling != _stringEscapeHandling)
            {
                previousStringEscapeHandling    = jsonWriter.StringEscapeHandling;
                jsonWriter.StringEscapeHandling = _stringEscapeHandling.Value;
            }

            CultureInfo previousCulture = null;

            if (_culture != null && !_culture.Equals(jsonWriter.Culture))
            {
                previousCulture    = jsonWriter.Culture;
                jsonWriter.Culture = _culture;
            }

            string previousDateFormatString = null;

            if (_dateFormatStringSet && jsonWriter.DateFormatString != _dateFormatString)
            {
                previousDateFormatString    = jsonWriter.DateFormatString;
                jsonWriter.DateFormatString = _dateFormatString;
            }

            TraceJsonWriter traceJsonWriter = (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
                                          ? new TraceJsonWriter(jsonWriter)
                                          : null;

            JsonSerializerInternalWriter serializerWriter = new JsonSerializerInternalWriter(this);

            serializerWriter.Serialize(traceJsonWriter ?? jsonWriter, value, objectType);

            if (traceJsonWriter != null)
            {
                TraceWriter.Trace(TraceLevel.Verbose, "Serialized JSON: " + Environment.NewLine + traceJsonWriter.GetJson(), null);
            }

            // reset writer back to previous options
            if (previousFormatting != null)
            {
                jsonWriter.Formatting = previousFormatting.Value;
            }
            if (previousDateFormatHandling != null)
            {
                jsonWriter.DateFormatHandling = previousDateFormatHandling.Value;
            }
            if (previousDateTimeZoneHandling != null)
            {
                jsonWriter.DateTimeZoneHandling = previousDateTimeZoneHandling.Value;
            }
            if (previousFloatFormatHandling != null)
            {
                jsonWriter.FloatFormatHandling = previousFloatFormatHandling.Value;
            }
            if (previousStringEscapeHandling != null)
            {
                jsonWriter.StringEscapeHandling = previousStringEscapeHandling.Value;
            }
            if (_dateFormatStringSet)
            {
                jsonWriter.DateFormatString = previousDateFormatString;
            }
            if (previousCulture != null)
            {
                jsonWriter.Culture = previousCulture;
            }
        }
Exemplo n.º 3
0
        internal virtual void SerializeInternal(JsonWriter jsonWriter, object value, Type objectType)
        {
            ValidationUtils.ArgumentNotNull(jsonWriter, "jsonWriter");
            Formatting?formatting = null;

            if (this._formatting.HasValue && jsonWriter.Formatting != this._formatting)
            {
                formatting            = new Formatting?(jsonWriter.Formatting);
                jsonWriter.Formatting = this._formatting.Value;
            }
            DateFormatHandling?dateFormatHandling = null;

            if (this._dateFormatHandling.HasValue && jsonWriter.DateFormatHandling != this._dateFormatHandling)
            {
                dateFormatHandling            = new DateFormatHandling?(jsonWriter.DateFormatHandling);
                jsonWriter.DateFormatHandling = this._dateFormatHandling.Value;
            }
            DateTimeZoneHandling?dateTimeZoneHandling = null;

            if (this._dateTimeZoneHandling.HasValue && jsonWriter.DateTimeZoneHandling != this._dateTimeZoneHandling)
            {
                dateTimeZoneHandling            = new DateTimeZoneHandling?(jsonWriter.DateTimeZoneHandling);
                jsonWriter.DateTimeZoneHandling = this._dateTimeZoneHandling.Value;
            }
            FloatFormatHandling?floatFormatHandling = null;

            if (this._floatFormatHandling.HasValue && jsonWriter.FloatFormatHandling != this._floatFormatHandling)
            {
                floatFormatHandling            = new FloatFormatHandling?(jsonWriter.FloatFormatHandling);
                jsonWriter.FloatFormatHandling = this._floatFormatHandling.Value;
            }
            StringEscapeHandling?stringEscapeHandling = null;

            if (this._stringEscapeHandling.HasValue && jsonWriter.StringEscapeHandling != this._stringEscapeHandling)
            {
                stringEscapeHandling            = new StringEscapeHandling?(jsonWriter.StringEscapeHandling);
                jsonWriter.StringEscapeHandling = this._stringEscapeHandling.Value;
            }
            CultureInfo cultureInfo = null;

            if (this._culture != null && !this._culture.Equals(jsonWriter.Culture))
            {
                cultureInfo        = jsonWriter.Culture;
                jsonWriter.Culture = this._culture;
            }
            string dateFormatString = null;

            if (this._dateFormatStringSet && jsonWriter.DateFormatString != this._dateFormatString)
            {
                dateFormatString            = jsonWriter.DateFormatString;
                jsonWriter.DateFormatString = this._dateFormatString;
            }
            TraceJsonWriter traceJsonWriter = (this.TraceWriter != null && this.TraceWriter.LevelFilter >= TraceLevel.Verbose) ? new TraceJsonWriter(jsonWriter) : null;
            JsonSerializerInternalWriter jsonSerializerInternalWriter = new JsonSerializerInternalWriter(this);

            jsonSerializerInternalWriter.Serialize(traceJsonWriter ?? jsonWriter, value, objectType);
            if (traceJsonWriter != null)
            {
                this.TraceWriter.Trace(TraceLevel.Verbose, "Serialized JSON: " + Environment.NewLine + traceJsonWriter.GetJson(), null);
            }
            if (formatting.HasValue)
            {
                jsonWriter.Formatting = formatting.Value;
            }
            if (dateFormatHandling.HasValue)
            {
                jsonWriter.DateFormatHandling = dateFormatHandling.Value;
            }
            if (dateTimeZoneHandling.HasValue)
            {
                jsonWriter.DateTimeZoneHandling = dateTimeZoneHandling.Value;
            }
            if (floatFormatHandling.HasValue)
            {
                jsonWriter.FloatFormatHandling = floatFormatHandling.Value;
            }
            if (stringEscapeHandling.HasValue)
            {
                jsonWriter.StringEscapeHandling = stringEscapeHandling.Value;
            }
            if (this._dateFormatStringSet)
            {
                jsonWriter.DateFormatString = dateFormatString;
            }
            if (cultureInfo != null)
            {
                jsonWriter.Culture = cultureInfo;
            }
        }