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).Select(ConvertNode).ToList();
36
37 return new BadArray(a);
38 }
39
45 private static BadTable ConvertObject(JObject obj)
46 {
47 Dictionary<string, BadObject> t = new Dictionary<string, BadObject>();
48
49 foreach (KeyValuePair<string, JToken?> keyValuePair in obj)
50 {
51 t.Add(keyValuePair.Key, keyValuePair.Value == null ? BadObject.Null : ConvertNode(keyValuePair.Value));
52 }
53
54 return new BadTable(t);
55 }
56
63 public static JToken ConvertNode(BadObject value)
64 {
65 if (value == BadObject.Null)
66 {
67 return JValue.CreateNull();
68 }
69
70 return value switch
71 {
72 IBadString s => new JValue(s.Value),
73 IBadNumber n => new JValue(n.Value),
74 IBadBoolean b => new JValue(b.Value),
75 BadArray a => ConvertArray(a),
76 BadTable t => ConvertTable(t),
77 BadReflectedObject ro => JToken.FromObject(ro.Instance),
78 BadFunction f => new JValue(f.ToSafeString()),
79 _ => throw new Exception("Unsupported value type: " + value.GetType()),
80 };
81 }
82
89 private static JObject ConvertTable(BadTable table)
90 {
91 JObject obj = new JObject();
92
93 foreach (KeyValuePair<string, BadObject> keyValuePair in table.InnerTable)
94 {
95 obj.Add(keyValuePair.Key, ConvertNode(keyValuePair.Value));
96 }
97
98 return obj;
99 }
100
106 private static JArray ConvertArray(BadArray value)
107 {
108 JArray array = new JArray();
109
110 foreach (BadObject node in value.InnerArray)
111 {
112 array.Add(ConvertNode(node));
113 }
114
115 return array;
116 }
117
124 private static BadObject ConvertValue(JValue value)
125 {
126 switch (value.Type)
127 {
128 case JTokenType.Integer:
129 if (value.Value is int i)
130 {
131 return i;
132 }
133
134 return (decimal)(long)value.Value!;
135 case JTokenType.Float:
136 return value.Value switch
137 {
138 float f => (decimal)f,
139 decimal d => d,
140 _ => (decimal)(double)value.Value!,
141 };
142
143 case JTokenType.String:
144 return (string)value.Value!;
145 case JTokenType.Boolean:
146 return (bool)value.Value!;
147 case JTokenType.Guid:
148 return value.Value!.ToString();
149 case JTokenType.Date:
150 return value.Value<DateTime>().ToString("O");
151 case JTokenType.Null:
152 return BadObject.Null;
153 case JTokenType.None:
154 case JTokenType.Object:
155 case JTokenType.Array:
156 case JTokenType.Constructor:
157 case JTokenType.Property:
158 case JTokenType.Comment:
159 case JTokenType.Undefined:
160 case JTokenType.Raw:
161 case JTokenType.Bytes:
162 case JTokenType.Uri:
163 case JTokenType.TimeSpan:
164 default:
165 throw new Exception("Unsupported Json type: " + value.Type);
166 }
167 }
168
175 public static BadObject ConvertNode(JToken? node)
176 {
177 return node switch
178 {
179 null => BadObject.Null,
180 JArray a => ConvertArray(a),
181 JObject o => ConvertObject(o),
182 JValue v => ConvertValue(v),
183 _ => throw new Exception("Unsupported node type: " + node.GetType()),
184 };
185 }
186
192 public static BadObject FromJson(string s)
193 {
194 JToken o = JToken.Parse(s);
195
196 return ConvertNode(o);
197 }
198
204 public static string ToJson(BadObject o)
205 {
206 JToken token = ConvertNode(o);
207
208 return token.ToString();
209 }
210}
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:28
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:63
static BadObject FromJson(string s)
Converts a Json string to a BadObject.
Definition BadJson.cs:192
static string ToJson(BadObject o)
Converts a BadObject to a Json string.
Definition BadJson.cs:204
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:124
static JObject ConvertTable(BadTable table)
Converts a BadTable to a JObject.
Definition BadJson.cs:89
static BadTable ConvertObject(JObject obj)
Converts a JObject to a BadTable.
Definition BadJson.cs:45
static BadObject ConvertNode(JToken? node)
Converts a JToken to a BadObject.
Definition BadJson.cs:175
static JArray ConvertArray(BadArray value)
Converts a BadArray to a JArray.
Definition BadJson.cs:106
Implements a Dynamic List/Array for the BadScript Language.
Definition BadArray.cs:17
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
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