Merge pull request #3213 from henricm/fix-for-win-securestring-to-bstr
[mono.git] / mcs / class / referencesource / System.Runtime.Serialization / System / Runtime / Serialization / Json / JsonReaderWriterFactory.cs
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4
5 namespace System.Runtime.Serialization.Json
6 {
7     using System;
8     using System.Collections.Generic;
9     using System.Text;
10     using System.Xml;
11     using System.IO;
12 #if !MONO
13     using System.ServiceModel;
14 #endif
15     using System.Runtime.Serialization.Json;
16     using System.Runtime.CompilerServices;
17
18     [TypeForwardedFrom("System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
19     public static class JsonReaderWriterFactory
20     {
21         const string DefaultIndentChars = "  ";
22
23         public static XmlDictionaryReader CreateJsonReader(Stream stream, XmlDictionaryReaderQuotas quotas)
24         {
25             return CreateJsonReader(stream, null, quotas, null);
26         }
27
28         public static XmlDictionaryReader CreateJsonReader(byte[] buffer, XmlDictionaryReaderQuotas quotas)
29         {
30             if (buffer == null)
31             {
32                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("buffer");
33             }
34
35             return CreateJsonReader(buffer, 0, buffer.Length, null, quotas, null);
36         }
37
38         public static XmlDictionaryReader CreateJsonReader(Stream stream, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
39         {
40             XmlJsonReader reader = new XmlJsonReader();
41             reader.SetInput(stream, encoding, quotas, onClose);
42             return reader;
43         }
44
45         public static XmlDictionaryReader CreateJsonReader(byte[] buffer, int offset, int count, XmlDictionaryReaderQuotas quotas)
46         {
47             return CreateJsonReader(buffer, offset, count, null, quotas, null);
48         }
49
50         public static XmlDictionaryReader CreateJsonReader(byte[] buffer, int offset, int count, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
51         {
52             XmlJsonReader reader = new XmlJsonReader();
53             reader.SetInput(buffer, offset, count, encoding, quotas, onClose);
54             return reader;
55         }
56
57         public static XmlDictionaryWriter CreateJsonWriter(Stream stream)
58         {
59             return CreateJsonWriter(stream, Encoding.UTF8, true);
60         }
61
62         public static XmlDictionaryWriter CreateJsonWriter(Stream stream, Encoding encoding)
63         {
64             return CreateJsonWriter(stream, encoding, true);
65         }
66
67         public static XmlDictionaryWriter CreateJsonWriter(Stream stream, Encoding encoding, bool ownsStream)
68         {
69             return CreateJsonWriter(stream, encoding, ownsStream, false);
70         }
71
72         public static XmlDictionaryWriter CreateJsonWriter(Stream stream, Encoding encoding, bool ownsStream, bool indent)
73         {
74             return CreateJsonWriter(stream, encoding, ownsStream, indent, JsonReaderWriterFactory.DefaultIndentChars);
75         }
76
77         public static XmlDictionaryWriter CreateJsonWriter(Stream stream, Encoding encoding, bool ownsStream, bool indent, string indentChars)
78         {
79             XmlJsonWriter writer = new XmlJsonWriter(indent, indentChars);
80             writer.SetOutput(stream, encoding, ownsStream);
81             return writer;
82         }
83     }
84 }