Exemplo n.º 1
0
        /// <summary>
        /// Writes a <see cref="JsonNode"/> instance to the provided <see cref="StringBuilder"/>
        /// with custom formatting.
        /// </summary>
        /// <param name="node">A <see cref="JsonNode"/> instance or <c>null</c>.</param>
        /// <param name="builder">String builder.</param>
        /// <param name="settings">Custom settings.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <list type="bullet">
        /// <item>If <paramref name="builder"/> is <c>null</c>.</item>
        /// <item>If <paramref name="settings"/> is <c>null</c>.</item>
        /// </list>
        /// </exception>
        /// <seealso cref="JsonWriter"/>
        /// <seealso cref="JsonNode.Write(IJsonWriter)"/>
        public static void WriteTo(this JsonNode node, StringBuilder builder, JsonWriterSettings settings)
        {
            var writer = JsonWriter.Create(builder, settings);

            if (node != null)
            {
                node.Write(writer);
            }
            else
            {
                writer.WriteNull();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new <see cref="JsonWriter"/> instance and write content to the
        /// provided <see cref="Stream"/> with custom formatting.
        /// </summary>
        /// <param name="stream">Stream that data will be written to.</param>
        /// <param name="settings">Custom settings.</param>
        /// <returns>
        /// New <see cref="JsonWriter"/> instance.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// <list type="bullet">
        /// <item>If <paramref name="stream"/> is <c>null</c>.</item>
        /// <item>If <paramref name="settings"/> is <c>null</c>.</item>
        /// </list>
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// If <paramref name="stream"/> is not writable.
        /// </exception>
        public static JsonWriter Create(Stream stream, JsonWriterSettings settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanWrite)
            {
                throw new ArgumentException("Cannot write to stream.", "stream");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            return(new JsonWriter(new StreamWriter(stream), settings));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonWriter"/> class.
        /// </summary>
        /// <param name="textWriter">Text writer.</param>
        /// <param name="settings">Custom settings.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <list type="bullet">
        /// <item>If <paramref name="textWriter"/> is <c>null</c>.</item>
        /// <item>If <paramref name="settings"/> is <c>null</c>.</item>
        /// </list>
        /// </exception>
        private JsonWriter(TextWriter textWriter, JsonWriterSettings settings)
        {
            if (textWriter == null)
            {
                throw new ArgumentNullException("textWriter");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            this.writer   = textWriter;
            this.Settings = settings;

            settings.MarkReadOnly();

            this.writeContextStack.Push(WriteContext.Root);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Creates a new <see cref="JsonWriter"/> instance with custom formatting.
 /// </summary>
 /// <param name="settings">Custom settings.</param>
 /// <returns>
 /// New <see cref="JsonWriter"/> instance.
 /// </returns>
 public static JsonWriter Create(JsonWriterSettings settings)
 {
     return(Create(new StringWriter(), settings));
 }
Exemplo n.º 5
0
 static JsonWriterSettings()
 {
     DefaultSettings = new JsonWriterSettings();
     DefaultSettings.MarkReadOnly();
 }