BadScript 2
Loading...
Searching...
No Matches
BadJson.cs
Go to the documentation of this file.
5
6using Newtonsoft.Json.Linq;
7
12
16public static class BadJson
17{
23 public static BadRuntime UseJsonApi(this BadRuntime runtime)
24 {
25 return runtime.UseApi(new BadJsonApi(), true);
26 }
27
33 private static BadArray ConvertArray(JArray array)
34 {
35 List<BadObject> a = array.Where(x => x != null)
36 .Select(ConvertNode)
37 .ToList();
38
39 return new BadArray(a);
40 }
41
47 private static BadTable ConvertObject(JObject obj)
48 {
49 Dictionary<string, BadObject> t = new Dictionary<string, BadObject>();
50
51 foreach (KeyValuePair<string, JToken?> keyValuePair in obj)
52 {
53 t.Add(keyValuePair.Key, keyValuePair.Value == null ? BadObject.Null : ConvertNode(keyValuePair.Value));
54 }
55
56 return new BadTable(t);
57 }
58
65 public static JToken ConvertNode(BadObject value)
66 {
67 if (value == BadObject.Null)
68 {
69 return JValue.CreateNull();
70 }
71
72 return value switch
73 {
74 IBadString s => new JValue(s.Value),
75 IBadNumber n => new JValue(n.Value),
76 IBadBoolean b => new JValue(b.Value),
77 BadArray a => ConvertArray(a),
78 BadTable t => ConvertTable(t),
79 BadReflectedObject ro => JToken.FromObject(ro.Instance),
80 BadFunction f => new JValue(f.ToSafeString()),
81 _ => throw new Exception("Unsupported value type: " + value.GetType()),
82 };
83 }
84
91 private static JObject ConvertTable(BadTable table)
92 {
93 JObject obj = new JObject();
94
95 foreach (KeyValuePair<string, BadObject> keyValuePair in table.InnerTable)
96 {
97 obj.Add(keyValuePair.Key, ConvertNode(keyValuePair.Value));
98 }
99
100 return obj;
101 }
102
108 private static JArray ConvertArray(BadArray value)
109 {
110 JArray array = new JArray();
111
112 foreach (BadObject node in value.InnerArray)
113 {
114 array.Add(ConvertNode(node));
115 }
116
117 return array;
118 }
119
126 private static BadObject ConvertValue(JValue value)
127 {
128 switch (value.Type)
129 {
130 case JTokenType.Integer:
131 if (value.Value is int i)
132 {
133 return i;
134 }
135 if(value.Value is byte b)
136 {
137 return b;
138 }
139 if(value.Value is sbyte sb)
140 {
141 return sb;
142 }
143 if(value.Value is short s)
144 {
145 return s;
146 }
147 if(value.Value is ushort us)
148 {
149 return us;
150 }
151 if(value.Value is uint ui)
152 {
153 return ui;
154 }
155 if(value.Value is long l)
156 {
157 return l;
158 }
159 if(value.Value is ulong ul)
160 {
161 return ul;
162 }
163
164 return (long)value.Value!;
165 case JTokenType.Float:
166 return value.Value switch
167 {
168 float f => (decimal)f,
169 decimal d => d,
170 _ => (decimal)(double)value.Value!,
171 };
172
173 case JTokenType.String:
174 return (string)value.Value!;
175 case JTokenType.Boolean:
176 return (bool)value.Value!;
177 case JTokenType.Guid:
178 return value.Value!.ToString();
179 case JTokenType.Date:
180 return value.Value<DateTime>()
181 .ToString("O");
182 case JTokenType.Null:
183 return BadObject.Null;
184 case JTokenType.None:
185 case JTokenType.Object:
186 case JTokenType.Array:
187 case JTokenType.Constructor:
188 case JTokenType.Property:
189 case JTokenType.Comment:
190 case JTokenType.Undefined:
191 case JTokenType.Raw:
192 case JTokenType.Bytes:
193 case JTokenType.Uri:
194 case JTokenType.TimeSpan:
195 default:
196 throw new Exception("Unsupported Json type: " + value.Type);
197 }
198 }
199
206 public static BadObject ConvertNode(JToken? node)
207 {
208 return node switch
209 {
210 null => BadObject.Null,
211 JArray a => ConvertArray(a),
212 JObject o => ConvertObject(o),
213 JValue v => ConvertValue(v),
214 _ => throw new Exception("Unsupported node type: " + node.GetType()),
215 };
216 }
217
223 public static BadObject FromJson(string s)
224 {
225 JToken o = JToken.Parse(s);
226
227 return ConvertNode(o);
228 }
229
235 public static string ToJson(BadObject o)
236 {
237 JToken token = ConvertNode(o);
238
239 return token.ToString();
240 }
241}
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:30
BadRuntime UseApi(BadInteropApi api, bool replace=false)
Adds or Replaces a specified API.
Implements the "Json" Api.
Definition BadJsonApi.cs:16
Implements a Json to BadObject Converter.
Definition BadJson.cs:17
static JToken ConvertNode(BadObject value)
Converts a BadObject to a JToken.
Definition BadJson.cs:65
static BadObject FromJson(string s)
Converts a Json string to a BadObject.
Definition BadJson.cs:223
static string ToJson(BadObject o)
Converts a BadObject to a Json string.
Definition BadJson.cs:235
static BadRuntime UseJsonApi(this BadRuntime runtime)
Configures the Runtime to use the Json API.
Definition BadJson.cs:23
static BadArray ConvertArray(JArray array)
Converts a JArray to a BadArray.
Definition BadJson.cs:33
static BadObject ConvertValue(JValue value)
Converts a JValue to a BadObject.
Definition BadJson.cs:126
static JObject ConvertTable(BadTable table)
Converts a BadTable to a JObject.
Definition BadJson.cs:91
static BadTable ConvertObject(JObject obj)
Converts a JObject to a BadTable.
Definition BadJson.cs:47
static BadObject ConvertNode(JToken? node)
Converts a JToken to a BadObject.
Definition BadJson.cs:206
static JArray ConvertArray(BadArray value)
Converts a BadArray to a JArray.
Definition BadJson.cs:108
Implements a Dynamic List/Array for the BadScript Language.
Definition BadArray.cs:17
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
override string ToString()
Returns a String Representation of this Object.
Definition BadObject.cs:224
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements a Table Structure for the BadScript Language.
Definition BadTable.cs:14
Implements a function that can be called from the script.
override string ToSafeString(List< BadObject > done)
Implements the Interface for Native Boolean.
Definition IBadBoolean.cs:7
Implements the Interface for Native Numbers.
Definition IBadNumber.cs:7
new decimal Value
The Number Value.
Definition IBadNumber.cs:11
Implements the Interface for Native Strings.
Definition IBadString.cs:7
new string Value
The String Value.
Definition IBadString.cs:11
Contains JSON Extensions and APIs for the BadScript2 Runtime.
Definition BadJson.cs:11
Contains the Classes for Reflection Objects.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10