Exemplo n.º 1
0
        internal virtual void SerializeInternal(JsonWriter jsonWriter, object value)
        {
            ValidationUtils.ArgumentNotNull(jsonWriter, "jsonWriter");
            JsonSerializerInternalWriter jsonSerializerInternalWriter = new JsonSerializerInternalWriter(this);

            jsonSerializerInternalWriter.Serialize(jsonWriter, value);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Serializes the specified object.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns>The output json string.</returns>
        public static string Serialize(object obj)
        {
            Type type = obj.GetType();

            Newtonsoft.Json.JsonSerializer jsonSerializer = Newtonsoft.Json.JsonSerializer.CreateDefault(Settings);
            jsonSerializer.Formatting = Formatting.Indented;

            StringBuilder sb = new StringBuilder(256);
            StringWriter  sw = new StringWriter(sb, CultureInfo.InvariantCulture);

            using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
            {
                // Prepare writer settings
                jsonWriter.IndentChar           = '\t';
                jsonWriter.Indentation          = 1;
                jsonWriter.Formatting           = jsonSerializer.Formatting;
                jsonWriter.DateFormatHandling   = jsonSerializer.DateFormatHandling;
                jsonWriter.DateTimeZoneHandling = jsonSerializer.DateTimeZoneHandling;
                jsonWriter.FloatFormatHandling  = jsonSerializer.FloatFormatHandling;
                jsonWriter.StringEscapeHandling = jsonSerializer.StringEscapeHandling;
                jsonWriter.Culture          = jsonSerializer.Culture;
                jsonWriter.DateFormatString = jsonSerializer.DateFormatString;

                JsonSerializerInternalWriter serializerWriter = new JsonSerializerInternalWriter(jsonSerializer);
                CurrentWriter.Value = serializerWriter;

                serializerWriter.Serialize(jsonWriter, obj, type);

                CurrentWriter.Value = null;
            }

            return(sw.ToString());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates deep clone for a class if all members of this class are marked as serializable (uses Json serialization).
        /// </summary>
        /// <param name="instance">Current instance of an object</param>
        /// <typeparam name="T">Instance type of an object</typeparam>
        /// <returns>Returns new object of provided class</returns>
        public static T DeepClone <T>(this T instance)
            where T : new()
        {
            Type type = typeof(T);

            JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(Json.JsonSerializer.Settings);

            jsonSerializer.Formatting = Formatting.Indented;

            StringBuilder sb = new StringBuilder(256);
            StringWriter  sw = new StringWriter(sb, CultureInfo.InvariantCulture);

            using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
            {
                // Prepare writer settings
                jsonWriter.IndentChar           = '\t';
                jsonWriter.Indentation          = 1;
                jsonWriter.Formatting           = jsonSerializer.Formatting;
                jsonWriter.DateFormatHandling   = jsonSerializer.DateFormatHandling;
                jsonWriter.DateTimeZoneHandling = jsonSerializer.DateTimeZoneHandling;
                jsonWriter.FloatFormatHandling  = jsonSerializer.FloatFormatHandling;
                jsonWriter.StringEscapeHandling = jsonSerializer.StringEscapeHandling;
                jsonWriter.Culture          = jsonSerializer.Culture;
                jsonWriter.DateFormatString = jsonSerializer.DateFormatString;

                JsonSerializerInternalWriter serializerWriter = new JsonSerializerInternalWriter(jsonSerializer);

                serializerWriter.Serialize(jsonWriter, instance, type);
            }

            return(JsonConvert.DeserializeObject <T>(sb.ToString()));
        }
Exemplo n.º 4
0
        internal virtual void SerializeInternal(JsonWriter jsonWriter, object value, bool useDefaultFormat = true)
        {
            ValidationUtils.ArgumentNotNull(jsonWriter, "jsonWriter");

            JsonSerializerInternalWriter serializerWriter = new JsonSerializerInternalWriter(this, useDefaultFormat);

            serializerWriter.Serialize(jsonWriter, value, useDefaultFormat);
        }
Exemplo n.º 5
0
 internal override void SerializeInternal(JsonWriter jsonWriter, object value, Type rootType)
 {
     if (_serializerWriter != null)
     {
         _serializerWriter.Serialize(jsonWriter, value, rootType);
     }
     else
     {
         _serializer.Serialize(jsonWriter, value);
     }
 }
Exemplo n.º 6
0
        internal virtual void SerializeInternal(JsonWriter jsonWriter, object value)
        {
            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;
            }

            JsonSerializerInternalWriter serializerWriter = new JsonSerializerInternalWriter(this);

            serializerWriter.Serialize(jsonWriter, value);

            // 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;
            }
        }
        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.º 8
0
        internal virtual void SerializeInternal(JsonWriter jsonWriter, object value)
        {
            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;
            }

            JsonSerializerInternalWriter serializerWriter = new JsonSerializerInternalWriter(this);

            serializerWriter.Serialize(jsonWriter, value);

            // 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;
            }
        }
        internal virtual void SerializeInternal(JsonWriter jsonWriter, object value, Type objectType)
        {
            TraceJsonWriter traceJsonWriter;

            ValidationUtils.ArgumentNotNull(jsonWriter, "jsonWriter");
            Newtonsoft.Json.Formatting?nullable = null;
            if (this._formatting.HasValue)
            {
                Newtonsoft.Json.Formatting formatting = jsonWriter.Formatting;
                Newtonsoft.Json.Formatting?nullable1  = this._formatting;
                if ((formatting == nullable1.GetValueOrDefault() ? !nullable1.HasValue : true))
                {
                    nullable = new Newtonsoft.Json.Formatting?(jsonWriter.Formatting);
                    jsonWriter.Formatting = this._formatting.GetValueOrDefault();
                }
            }
            Newtonsoft.Json.DateFormatHandling?nullable2 = null;
            if (this._dateFormatHandling.HasValue)
            {
                Newtonsoft.Json.DateFormatHandling dateFormatHandling = jsonWriter.DateFormatHandling;
                Newtonsoft.Json.DateFormatHandling?nullable3          = this._dateFormatHandling;
                if ((dateFormatHandling == nullable3.GetValueOrDefault() ? !nullable3.HasValue : true))
                {
                    nullable2 = new Newtonsoft.Json.DateFormatHandling?(jsonWriter.DateFormatHandling);
                    jsonWriter.DateFormatHandling = this._dateFormatHandling.GetValueOrDefault();
                }
            }
            Newtonsoft.Json.DateTimeZoneHandling?nullable4 = null;
            if (this._dateTimeZoneHandling.HasValue)
            {
                Newtonsoft.Json.DateTimeZoneHandling dateTimeZoneHandling = jsonWriter.DateTimeZoneHandling;
                Newtonsoft.Json.DateTimeZoneHandling?nullable5            = this._dateTimeZoneHandling;
                if ((dateTimeZoneHandling == nullable5.GetValueOrDefault() ? !nullable5.HasValue : true))
                {
                    nullable4 = new Newtonsoft.Json.DateTimeZoneHandling?(jsonWriter.DateTimeZoneHandling);
                    jsonWriter.DateTimeZoneHandling = this._dateTimeZoneHandling.GetValueOrDefault();
                }
            }
            Newtonsoft.Json.FloatFormatHandling?nullable6 = null;
            if (this._floatFormatHandling.HasValue)
            {
                Newtonsoft.Json.FloatFormatHandling floatFormatHandling = jsonWriter.FloatFormatHandling;
                Newtonsoft.Json.FloatFormatHandling?nullable7           = this._floatFormatHandling;
                if ((floatFormatHandling == nullable7.GetValueOrDefault() ? !nullable7.HasValue : true))
                {
                    nullable6 = new Newtonsoft.Json.FloatFormatHandling?(jsonWriter.FloatFormatHandling);
                    jsonWriter.FloatFormatHandling = this._floatFormatHandling.GetValueOrDefault();
                }
            }
            Newtonsoft.Json.StringEscapeHandling?nullable8 = null;
            if (this._stringEscapeHandling.HasValue)
            {
                Newtonsoft.Json.StringEscapeHandling stringEscapeHandling = jsonWriter.StringEscapeHandling;
                Newtonsoft.Json.StringEscapeHandling?nullable9            = this._stringEscapeHandling;
                if ((stringEscapeHandling == nullable9.GetValueOrDefault() ? !nullable9.HasValue : true))
                {
                    nullable8 = new Newtonsoft.Json.StringEscapeHandling?(jsonWriter.StringEscapeHandling);
                    jsonWriter.StringEscapeHandling = this._stringEscapeHandling.GetValueOrDefault();
                }
            }
            CultureInfo culture = null;

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

            if (this._dateFormatStringSet && jsonWriter.DateFormatString != this._dateFormatString)
            {
                dateFormatString            = jsonWriter.DateFormatString;
                jsonWriter.DateFormatString = this._dateFormatString;
            }
            if (this.TraceWriter == null || this.TraceWriter.LevelFilter < TraceLevel.Verbose)
            {
                traceJsonWriter = null;
            }
            else
            {
                traceJsonWriter = new TraceJsonWriter(jsonWriter);
            }
            TraceJsonWriter traceJsonWriter1 = traceJsonWriter;
            JsonSerializerInternalWriter jsonSerializerInternalWriter = new JsonSerializerInternalWriter(this);
            JsonWriter jsonWriter1 = traceJsonWriter1;

            if (jsonWriter1 == null)
            {
                jsonWriter1 = jsonWriter;
            }
            jsonSerializerInternalWriter.Serialize(jsonWriter1, value, objectType);
            if (traceJsonWriter1 != null)
            {
                this.TraceWriter.Trace(TraceLevel.Verbose, traceJsonWriter1.GetSerializedJsonMessage(), null);
            }
            if (nullable.HasValue)
            {
                jsonWriter.Formatting = nullable.GetValueOrDefault();
            }
            if (nullable2.HasValue)
            {
                jsonWriter.DateFormatHandling = nullable2.GetValueOrDefault();
            }
            if (nullable4.HasValue)
            {
                jsonWriter.DateTimeZoneHandling = nullable4.GetValueOrDefault();
            }
            if (nullable6.HasValue)
            {
                jsonWriter.FloatFormatHandling = nullable6.GetValueOrDefault();
            }
            if (nullable8.HasValue)
            {
                jsonWriter.StringEscapeHandling = nullable8.GetValueOrDefault();
            }
            if (this._dateFormatStringSet)
            {
                jsonWriter.DateFormatString = dateFormatString;
            }
            if (culture != null)
            {
                jsonWriter.Culture = culture;
            }
        }
Exemplo n.º 10
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;
            }
        }