64518785b87351fa6797c4bdfd8feb170df85a57
[mono.git] / mcs / class / System.Json / System.Json / JsonPrimitive.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Globalization;
5 using System.IO;
6 using System.Text;
7
8 namespace System.Json
9 {
10         public class JsonPrimitive : JsonValue
11         {
12                 object value;
13
14                 public JsonPrimitive (bool value)
15                 {
16                         this.value = value;
17                 }
18
19                 public JsonPrimitive (byte value)
20                 {
21                         this.value = value;
22                 }
23
24                 public JsonPrimitive (char value)
25                 {
26                         this.value = value;
27                 }
28
29                 public JsonPrimitive (decimal value)
30                 {
31                         this.value = value;
32                 }
33
34                 public JsonPrimitive (double value)
35                 {
36                         this.value = value;
37                 }
38
39                 public JsonPrimitive (float value)
40                 {
41                         this.value = value;
42                 }
43
44                 public JsonPrimitive (int value)
45                 {
46                         this.value = value;
47                 }
48
49                 public JsonPrimitive (long value)
50                 {
51                         this.value = value;
52                 }
53
54                 public JsonPrimitive (sbyte value)
55                 {
56                         this.value = value;
57                 }
58
59                 public JsonPrimitive (short value)
60                 {
61                         this.value = value;
62                 }
63
64                 public JsonPrimitive (string value)
65                 {
66                         this.value = value;
67                 }
68
69                 public JsonPrimitive (DateTime value)
70                 {
71                         this.value = value;
72                 }
73
74                 public JsonPrimitive (uint value)
75                 {
76                         this.value = value;
77                 }
78
79                 public JsonPrimitive (ulong value)
80                 {
81                         this.value = value;
82                 }
83
84                 public JsonPrimitive (ushort value)
85                 {
86                         this.value = value;
87                 }
88
89                 public JsonPrimitive (DateTimeOffset value)
90                 {
91                         this.value = value;
92                 }
93
94                 public JsonPrimitive (Guid value)
95                 {
96                         this.value = value;
97                 }
98
99                 public JsonPrimitive (TimeSpan value)
100                 {
101                         this.value = value;
102                 }
103
104                 public JsonPrimitive (Uri value)
105                 {
106                         this.value = value;
107                 }
108
109                 internal object Value {
110                         get { return value; }
111                 }
112
113                 public override JsonType JsonType {
114                         get {
115                                 // FIXME: what should we do for null? Handle it as null so far.
116                                 if (value == null)
117                                         return JsonType.String;
118
119                                 switch (Type.GetTypeCode (value.GetType ())) {
120                                 case TypeCode.Boolean:
121                                         return JsonType.Boolean;
122                                 case TypeCode.Char:
123                                 case TypeCode.String:
124                                 case TypeCode.DateTime:
125                                 case TypeCode.Object: // DateTimeOffset || Guid || TimeSpan || Uri
126                                         return JsonType.String;
127                                 default:
128                                         return JsonType.Number;
129                                 }
130                         }
131                 }
132
133                 static readonly byte [] true_bytes = Encoding.UTF8.GetBytes ("true");
134                 static readonly byte [] false_bytes = Encoding.UTF8.GetBytes ("false");
135
136                 public override void Save (Stream stream)
137                 {
138                         switch (JsonType) {
139                         case JsonType.Boolean:
140                                 if ((bool) value)
141                                         stream.Write (true_bytes, 0, 4);
142                                 else
143                                         stream.Write (false_bytes, 0, 5);
144                                 break;
145                         case JsonType.String:
146                                 stream.WriteByte ((byte) '\"');
147                                 byte [] bytes = Encoding.UTF8.GetBytes (EscapeString (value.ToString ()));
148                                 stream.Write (bytes, 0, bytes.Length);
149                                 stream.WriteByte ((byte) '\"');
150                                 break;
151                         default:
152                                 bytes = Encoding.UTF8.GetBytes (GetFormattedString ());
153                                 stream.Write (bytes, 0, bytes.Length);
154                                 break;
155                         }
156                 }
157
158                 internal string GetFormattedString ()
159                 {
160                         switch (JsonType) {
161                         case JsonType.String:
162                                 if (value is string || value == null)
163                                         return (string) value;
164                                 throw new NotImplementedException ("GetFormattedString from value type " + value.GetType ());
165                         case JsonType.Number:
166                                 return ((IFormattable) value).ToString ("G", NumberFormatInfo.InvariantInfo);
167                         default:
168                                 throw new InvalidOperationException ();
169                         }
170                 }
171         }
172 }