internal void SaveTo(JsonWriter writer, JsonSerializer serializer, SerializationFormattingPolicy formattingPolicy = SerializationFormattingPolicy.None) { if (writer == null) { throw new ArgumentNullException("writer"); } if (serializer == null) { throw new ArgumentNullException("serializer"); } writer.Formatting = formattingPolicy == SerializationFormattingPolicy.Indented ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None; this.OnSave(); if (typeof(DocumentMetadata).IsAssignableFrom(this.GetType()) && !this.GetType().Equals(typeof(DocumentMetadata))) { serializer.Serialize(writer, this); } else { if (JsonSerializable.JustPocoSerialization) { this.propertyBag.WriteTo(writer); } else { serializer.Serialize(writer, this.propertyBag); } } writer.Flush(); }
/// <summary> /// Saves the object to the specified string builder /// </summary> /// <param name="stringBuilder">Saves the object to this output string builder.</param> /// <param name="formattingPolicy">Uses an optional serialization formatting policy when saving the object. The default policy is set to None.</param> internal void SaveTo(StringBuilder stringBuilder, SerializationFormattingPolicy formattingPolicy = SerializationFormattingPolicy.None) { if (stringBuilder == null) { throw new ArgumentNullException("stringBuilder"); } this.SaveTo(new JsonTextWriter(new StringWriter(stringBuilder, CultureInfo.CurrentCulture)), new JsonSerializer(), formattingPolicy); }
/// <summary> /// Saves the object to the specified stream in the Azure Cosmos DB service. /// </summary> /// <param name="stream">Saves the object to this output stream.</param> /// <param name="formattingPolicy">Uses a custom serialization formatting policy when saving the object.</param> /// <param name="settings">The serializer settings to use.</param> public void SaveTo(Stream stream, SerializationFormattingPolicy formattingPolicy, JsonSerializerSettings settings) { if (stream == null) { throw new ArgumentNullException("stream"); } this.SerializerSettings = settings; JsonSerializer serializer = settings == null ? new JsonSerializer() : JsonSerializer.Create(settings); JsonTextWriter jsonWriter = new JsonTextWriter(new StreamWriter(stream)); this.SaveTo(jsonWriter, serializer, formattingPolicy); }
//Public Serialization Helpers. /// <summary> /// Saves the object to the specified stream in the Azure Cosmos DB service. /// </summary> /// <param name="stream">Saves the object to this output stream.</param> /// <param name="formattingPolicy">Uses an optional serialization formatting policy when saving the object. The default policy is set to None.</param> public void SaveTo(Stream stream, SerializationFormattingPolicy formattingPolicy = SerializationFormattingPolicy.None) { this.SaveTo(stream, formattingPolicy, null); }