2010-01-20 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / System.Json / System.Json / JsonValue.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Text;
6
7 using JsonPair = System.Collections.Generic.KeyValuePair<string, System.Json.JsonValue>;
8
9
10 namespace System.Json
11 {
12         public abstract class JsonValue : IEnumerable
13         {
14                 public static JsonValue Load (Stream stream)
15                 {
16                         if (stream == null)
17                                 throw new ArgumentNullException ("stream");
18                         return Load (new StreamReader (stream));
19                 }
20
21                 public static JsonValue Load (TextReader textReader)
22                 {
23                         if (textReader == null)
24                                 throw new ArgumentNullException ("textReader");
25                         return new JsonReader (textReader).Read ();
26                 }
27
28                 public static JsonValue Parse (string jsonString)
29                 {
30                         if (jsonString == null)
31                                 throw new ArgumentNullException ("jsonString");
32                         return Load (new StringReader (jsonString));
33                 }
34
35                 public virtual int Count {
36                         get { throw new InvalidOperationException (); }
37                 }
38
39                 public abstract JsonType JsonType { get; }
40
41                 public virtual JsonValue this [int index] {
42                         get { throw new InvalidOperationException (); }
43                         set { throw new InvalidOperationException (); }
44                 }
45
46                 public virtual JsonValue this [string key] {
47                         get { throw new InvalidOperationException (); }
48                         set { throw new InvalidOperationException (); }
49                 }
50
51                 public virtual bool ContainsKey (string key)
52                 {
53                         throw new InvalidOperationException ();
54                 }
55
56                 public virtual void Save (Stream stream)
57                 {
58                         if (stream == null)
59                                 throw new ArgumentNullException ("stream");
60                         Save (new StreamWriter (stream));
61                 }
62
63                 public virtual void Save (TextWriter textWriter)
64                 {
65                         if (textWriter == null)
66                                 throw new ArgumentNullException ("textWriter");
67                         SaveInternal (textWriter);
68                 }
69                 
70                 void SaveInternal (TextWriter w)
71                 {
72                         switch (JsonType) {
73                         case JsonType.Object:
74                                 w.Write ('{');
75                                 bool following = false;
76                                 foreach (JsonPair pair in ((JsonObject) this)) {
77                                         if (following)
78                                                 w.Write (", ");
79                                         w.Write ('\"');
80                                         w.Write (EscapeString (pair.Key));
81                                         w.Write ("\": ");
82                                         pair.Value.SaveInternal (w);
83                                         following = true;
84                                 }
85                                 w.Write ('}');
86                                 break;
87                         case JsonType.Array:
88                                 w.Write ('[');
89                                 following = false;
90                                 foreach (JsonValue v in ((JsonArray) this)) {
91                                         if (following)
92                                                 w.Write (", ");
93                                         v.SaveInternal (w);
94                                         following = true;
95                                 }
96                                 w.Write (']');
97                                 break;
98                         case JsonType.Boolean:
99                                 w.Write ((bool) this ? "true" : "false");
100                                 break;
101                         case JsonType.String:
102                                 w.Write ('"');
103                                 w.Write (EscapeString (((JsonPrimitive) this).GetFormattedString ()));
104                                 w.Write ('"');
105                                 break;
106                         default:
107                                 w.Write (((JsonPrimitive) this).GetFormattedString ());
108                                 break;
109                         }
110                 }
111
112                 public override string ToString ()
113                 {
114                         StringWriter sw = new StringWriter ();
115                         Save (sw);
116                         return sw.ToString ();
117                 }
118
119                 IEnumerator IEnumerable.GetEnumerator ()
120                 {
121                         throw new InvalidOperationException ();
122                 }
123
124                 internal string EscapeString (string src)
125                 {
126                         if (src == null)
127                                 return null;
128
129                         for (int i = 0; i < src.Length; i++)
130                                 if (src [i] == '"' || src [i] == '\\') {
131                                         var sb = new StringBuilder ();
132                                         if (i > 0)
133                                                 sb.Append (src, 0, i - 1);
134                                         return DoEscapeString (sb, src, i);
135                         }
136                         return src;
137                 }
138
139                 string DoEscapeString (StringBuilder sb, string src, int cur)
140                 {
141                         for (int i = cur; i < src.Length; i++)
142                                 if (src [i] == '"' || src [i] == '\\') {
143                                         sb.Append ('\\');
144                                         sb.Append (src [i++]);
145                                         return DoEscapeString (sb, src, i);
146                                 }
147                         sb.Append (src, cur, src.Length - cur);
148                         return sb.ToString ();
149                 }
150
151                 // CLI -> JsonValue
152
153                 public static implicit operator JsonValue (bool value)
154                 {
155                         return new JsonPrimitive (value);
156                 }
157
158                 public static implicit operator JsonValue (byte value)
159                 {
160                         return new JsonPrimitive (value);
161                 }
162
163                 public static implicit operator JsonValue (char value)
164                 {
165                         return new JsonPrimitive (value);
166                 }
167
168                 public static implicit operator JsonValue (decimal value)
169                 {
170                         return new JsonPrimitive (value);
171                 }
172
173                 public static implicit operator JsonValue (double value)
174                 {
175                         return new JsonPrimitive (value);
176                 }
177
178                 public static implicit operator JsonValue (float value)
179                 {
180                         return new JsonPrimitive (value);
181                 }
182
183                 public static implicit operator JsonValue (int value)
184                 {
185                         return new JsonPrimitive (value);
186                 }
187
188                 public static implicit operator JsonValue (long value)
189                 {
190                         return new JsonPrimitive (value);
191                 }
192
193                 public static implicit operator JsonValue (sbyte value)
194                 {
195                         return new JsonPrimitive (value);
196                 }
197
198                 public static implicit operator JsonValue (short value)
199                 {
200                         return new JsonPrimitive (value);
201                 }
202
203                 public static implicit operator JsonValue (string value)
204                 {
205                         return new JsonPrimitive (value);
206                 }
207
208                 public static implicit operator JsonValue (uint value)
209                 {
210                         return new JsonPrimitive (value);
211                 }
212
213                 public static implicit operator JsonValue (ulong value)
214                 {
215                         return new JsonPrimitive (value);
216                 }
217
218                 public static implicit operator JsonValue (ushort value)
219                 {
220                         return new JsonPrimitive (value);
221                 }
222
223                 public static implicit operator JsonValue (DateTime value)
224                 {
225                         return new JsonPrimitive (value);
226                 }
227
228                 public static implicit operator JsonValue (DateTimeOffset value)
229                 {
230                         return new JsonPrimitive (value);
231                 }
232
233                 public static implicit operator JsonValue (Guid value)
234                 {
235                         return new JsonPrimitive (value);
236                 }
237
238                 public static implicit operator JsonValue (TimeSpan value)
239                 {
240                         return new JsonPrimitive (value);
241                 }
242
243                 public static implicit operator JsonValue (Uri value)
244                 {
245                         return new JsonPrimitive (value);
246                 }
247
248                 // JsonValue -> CLI
249
250                 public static implicit operator bool (JsonValue value)
251                 {
252                         if (value == null)
253                                 throw new ArgumentNullException ("value");
254                         return (bool) ((JsonPrimitive) value).Value;
255                 }
256
257                 public static implicit operator byte (JsonValue value)
258                 {
259                         if (value == null)
260                                 throw new ArgumentNullException ("value");
261                         return (byte) ((JsonPrimitive) value).Value;
262                 }
263
264                 public static implicit operator char (JsonValue value)
265                 {
266                         if (value == null)
267                                 throw new ArgumentNullException ("value");
268                         return (char) ((JsonPrimitive) value).Value;
269                 }
270
271                 public static implicit operator decimal (JsonValue value)
272                 {
273                         if (value == null)
274                                 throw new ArgumentNullException ("value");
275                         return (decimal) ((JsonPrimitive) value).Value;
276                 }
277
278                 public static implicit operator double (JsonValue value)
279                 {
280                         if (value == null)
281                                 throw new ArgumentNullException ("value");
282                         return (double) ((JsonPrimitive) value).Value;
283                 }
284
285                 public static implicit operator float (JsonValue value)
286                 {
287                         if (value == null)
288                                 throw new ArgumentNullException ("value");
289                         return (float) ((JsonPrimitive) value).Value;
290                 }
291
292                 public static implicit operator int (JsonValue value)
293                 {
294                         if (value == null)
295                                 throw new ArgumentNullException ("value");
296                         return (int) ((JsonPrimitive) value).Value;
297                 }
298
299                 public static implicit operator long (JsonValue value)
300                 {
301                         if (value == null)
302                                 throw new ArgumentNullException ("value");
303                         return (long) ((JsonPrimitive) value).Value;
304                 }
305
306                 public static implicit operator sbyte (JsonValue value)
307                 {
308                         if (value == null)
309                                 throw new ArgumentNullException ("value");
310                         return (sbyte) ((JsonPrimitive) value).Value;
311                 }
312
313                 public static implicit operator short (JsonValue value)
314                 {
315                         if (value == null)
316                                 throw new ArgumentNullException ("value");
317                         return (short) ((JsonPrimitive) value).Value;
318                 }
319
320                 public static implicit operator string (JsonValue value)
321                 {
322                         if (value == null)
323                                 return null;
324                         return (string) ((JsonPrimitive) value).Value;
325                 }
326
327                 public static implicit operator uint (JsonValue value)
328                 {
329                         if (value == null)
330                                 throw new ArgumentNullException ("value");
331                         return (uint) ((JsonPrimitive) value).Value;
332                 }
333
334                 public static implicit operator ulong (JsonValue value)
335                 {
336                         if (value == null)
337                                 throw new ArgumentNullException ("value");
338                         return (ulong) ((JsonPrimitive) value).Value;
339                 }
340
341                 public static implicit operator ushort (JsonValue value)
342                 {
343                         if (value == null)
344                                 throw new ArgumentNullException ("value");
345                         return (ushort) ((JsonPrimitive) value).Value;
346                 }
347
348                 public static implicit operator DateTime (JsonValue value)
349                 {
350                         if (value == null)
351                                 throw new ArgumentNullException ("value");
352                         return (DateTime) ((JsonPrimitive) value).Value;
353                 }
354
355                 public static implicit operator DateTimeOffset (JsonValue value)
356                 {
357                         if (value == null)
358                                 throw new ArgumentNullException ("value");
359                         return (DateTimeOffset) ((JsonPrimitive) value).Value;
360                 }
361
362                 public static implicit operator TimeSpan (JsonValue value)
363                 {
364                         if (value == null)
365                                 throw new ArgumentNullException ("value");
366                         return (TimeSpan) ((JsonPrimitive) value).Value;
367                 }
368
369                 public static implicit operator Guid (JsonValue value)
370                 {
371                         if (value == null)
372                                 throw new ArgumentNullException ("value");
373                         return (Guid) ((JsonPrimitive) value).Value;
374                 }
375
376                 public static implicit operator Uri (JsonValue value)
377                 {
378                         if (value == null)
379                                 throw new ArgumentNullException ("value");
380                         return (Uri) ((JsonPrimitive) value).Value;
381                 }
382         }
383 }