Skip to content

Commit c888eb5

Browse files
Add a configuration setting, USE_JAYROCK, to replace Jayrock with System.Text.Json at runtime
1 parent af10678 commit c888eb5

File tree

5 files changed

+886
-2
lines changed

5 files changed

+886
-2
lines changed
Lines changed: 360 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,360 @@
1+
#region License, Terms and Conditions
2+
//
3+
// Jayrock - A JSON-RPC implementation for the Microsoft .NET Framework
4+
// Written by Atif Aziz (atif.aziz@skybow.com)
5+
// Copyright (c) Atif Aziz. All rights reserved.
6+
//
7+
// This library is free software; you can redistribute it and/or modify it under
8+
// the terms of the GNU Lesser General Public License as published by the Free
9+
// Software Foundation; either version 2.1 of the License, or (at your option)
10+
// any later version.
11+
//
12+
// This library is distributed in the hope that it will be useful, but WITHOUT
13+
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15+
// details.
16+
//
17+
// You should have received a copy of the GNU Lesser General Public License
18+
// along with this library; if not, write to the Free Software Foundation, Inc.,
19+
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20+
//
21+
#endregion
22+
23+
namespace Imported.Jayrock.Json
24+
{
25+
#region Imports
26+
27+
using System;
28+
using System.Collections;
29+
using System.Globalization;
30+
using System.Text;
31+
using GeneXus.Utils;
32+
33+
#endregion
34+
35+
/// <summary>
36+
/// An ordered sequence of values. This class also provides a number of
37+
/// methods that can be found on a JavaScript Array for sake of parity.
38+
/// </summary>
39+
/// <remarks>
40+
/// <para>
41+
/// Public Domain 2002 JSON.org, ported to C# by Are Bjolseth (teleplan.no)
42+
/// and re-adapted by Atif Aziz (www.raboof.com)</para>
43+
/// </remarks>
44+
45+
[ Serializable ]
46+
internal class JArray : CollectionBase
47+
{
48+
public JArray() {}
49+
50+
public JArray(IEnumerable collection)
51+
{
52+
foreach (object item in collection)
53+
List.Add(item);
54+
}
55+
56+
public virtual object this[int index]
57+
{
58+
get { return InnerList[index]; }
59+
set { List[index] = value; }
60+
}
61+
62+
public int Length
63+
{
64+
get { return Count; }
65+
}
66+
67+
public JArray Put(object value)
68+
{
69+
Add(value);
70+
return this;
71+
}
72+
73+
public virtual void Add(object value)
74+
{
75+
List.Add(value);
76+
}
77+
public virtual void Add(int index, object value)
78+
{
79+
InnerList.Insert(index, value);
80+
}
81+
82+
public virtual void Remove(object value)
83+
{
84+
List.Remove(value);
85+
}
86+
87+
public virtual bool Contains(object value)
88+
{
89+
return List.Contains(value);
90+
}
91+
92+
public virtual int IndexOf(object value)
93+
{
94+
return List.IndexOf(value);
95+
}
96+
97+
public virtual bool HasValueAt(int index)
98+
{
99+
return this[index] != null;
100+
}
101+
102+
public virtual object GetValue(int index)
103+
{
104+
return GetValue(index, null);
105+
}
106+
107+
public virtual object GetValue(int index, object defaultValue)
108+
{
109+
object value = this[index];
110+
return value != null ? value : defaultValue;
111+
}
112+
113+
public virtual bool GetBoolean(int index)
114+
{
115+
return GetBoolean(index, false);
116+
}
117+
118+
public virtual bool GetBoolean(int index, bool defaultValue)
119+
{
120+
object value = GetValue(index);
121+
if (value == null) return defaultValue;
122+
return Convert.ToBoolean(value, CultureInfo.InvariantCulture);
123+
}
124+
125+
public virtual double GetDouble(int index)
126+
{
127+
return GetDouble(index, float.NaN);
128+
}
129+
130+
public virtual double GetDouble(int index, float defaultValue)
131+
{
132+
object value = GetValue(index);
133+
if (value == null) return defaultValue;
134+
return Convert.ToDouble(value, CultureInfo.InvariantCulture);
135+
}
136+
137+
public virtual int GetInt32(int index)
138+
{
139+
return GetInt32(index, 0);
140+
}
141+
142+
public virtual int GetInt32(int index, int defaultValue)
143+
{
144+
object value = GetValue(index);
145+
if (value == null) return defaultValue;
146+
return Convert.ToInt32(value, CultureInfo.InvariantCulture);
147+
}
148+
149+
public virtual string GetString(int index)
150+
{
151+
return GetString(index, string.Empty);
152+
}
153+
154+
public virtual string GetString(int index, string defaultValue)
155+
{
156+
object value = GetValue(index);
157+
if (value == null) return defaultValue;
158+
return value.ToString();
159+
}
160+
161+
public virtual JArray GetArray(int index)
162+
{
163+
return (JArray) GetValue(index);
164+
}
165+
166+
public virtual JObject GetObject(int index)
167+
{
168+
return (JObject) GetValue(index);
169+
}
170+
171+
protected override void OnValidate(object value)
172+
{
173+
//
174+
// Null values are allowed in a JSON array so don't delegate
175+
// to the base class (CollectionBase) implementation since that
176+
// disallows null entries by default.
177+
//
178+
}
179+
180+
/// <summary>
181+
/// Make an JSON external form string of this JsonArray. For
182+
/// compactness, no unnecessary whitespace is added.
183+
/// </summary>
184+
/// <remarks>
185+
/// This method assumes that the data structure is acyclical.
186+
/// </remarks>
187+
188+
public override string ToString()
189+
{
190+
return JSONHelper.WriteJSON(this);
191+
}
192+
193+
194+
/// <summary>
195+
/// Copies the elements to a new object array.
196+
/// </summary>
197+
198+
public virtual object[] ToArray()
199+
{
200+
return (object[]) ToArray(typeof(object));
201+
}
202+
203+
/// <summary>
204+
/// Copies the elements to a new array of the specified type.
205+
/// </summary>
206+
207+
public virtual Array ToArray(Type elementType)
208+
{
209+
return InnerList.ToArray(elementType);
210+
}
211+
212+
public virtual void Reverse()
213+
{
214+
InnerList.Reverse();
215+
}
216+
217+
//
218+
// Methods that imitate the JavaScript array methods.
219+
//
220+
221+
/// <summary>
222+
/// Appends new elements to an array.
223+
/// </summary>
224+
/// <returns>
225+
/// The new length of the array.
226+
/// </returns>
227+
/// <remarks>
228+
/// This method appends elements in the order in which they appear. If
229+
/// one of the arguments is an array, it is added as a single element.
230+
/// Use the <see cref="Concat"/> method to join the elements from two or
231+
/// more arrays.
232+
/// </remarks>
233+
234+
public int Push(object value)
235+
{
236+
Add(value);
237+
return Count;
238+
}
239+
240+
/// <summary>
241+
/// Appends new elements to an array.
242+
/// </summary>
243+
/// <returns>
244+
/// The new length of the array.
245+
/// </returns>
246+
/// <remarks>
247+
/// This method appends elements in the order in which they appear. If
248+
/// one of the arguments is an array, it is added as a single element.
249+
/// Use the <see cref="Concat"/> method to join the elements from two or
250+
/// more arrays.
251+
/// </remarks>
252+
253+
public int Push(params object[] values)
254+
{
255+
if (values != null)
256+
{
257+
foreach (object value in values)
258+
Push(value);
259+
}
260+
261+
return Count;
262+
}
263+
264+
/// <summary>
265+
/// Removes the last element from an array and returns it.
266+
/// </summary>
267+
/// <remarks>
268+
/// If the array is empty, null is returned.
269+
/// </remarks>
270+
271+
public object Pop()
272+
{
273+
if (Count == 0)
274+
return null;
275+
276+
object lastValue = InnerList[Count - 1];
277+
RemoveAt(Count - 1);
278+
return lastValue;
279+
}
280+
281+
/// <summary>
282+
/// Returns a new array consisting of a combination of two or more
283+
/// arrays.
284+
/// </summary>
285+
286+
public JArray Concat(params object[] values)
287+
{
288+
JArray newArray = new JArray(this);
289+
290+
if (values != null)
291+
{
292+
foreach (object value in values)
293+
{
294+
JArray arrayValue = value as JArray;
295+
296+
if (arrayValue != null)
297+
{
298+
foreach (object arrayValueValue in arrayValue)
299+
newArray.Push(arrayValueValue);
300+
}
301+
else
302+
{
303+
newArray.Push(value);
304+
}
305+
}
306+
}
307+
308+
return newArray;
309+
}
310+
311+
/// <summary>
312+
/// Removes the first element from an array and returns it.
313+
/// </summary>
314+
315+
public object Shift()
316+
{
317+
if (Count == 0)
318+
return null;
319+
320+
object firstValue = InnerList[0];
321+
RemoveAt(0);
322+
return firstValue;
323+
}
324+
325+
/// <summary>
326+
/// Returns an array with specified elements inserted at the beginning.
327+
/// </summary>
328+
/// <remarks>
329+
/// The unshift method inserts elements into the start of an array, so
330+
/// they appear in the same order in which they appear in the argument
331+
/// list.
332+
/// </remarks>
333+
334+
public JArray Unshift(object value)
335+
{
336+
List.Insert(0, value);
337+
return this;
338+
}
339+
340+
/// <summary>
341+
/// Returns an array with specified elements inserted at the beginning.
342+
/// </summary>
343+
/// <remarks>
344+
/// The unshift method inserts elements into the start of an array, so
345+
/// they appear in the same order in which they appear in the argument
346+
/// list.
347+
/// </remarks>
348+
349+
public JArray Unshift(params object[] values)
350+
{
351+
if (values != null)
352+
{
353+
foreach (object value in values)
354+
Unshift(value);
355+
}
356+
357+
return this;
358+
}
359+
}
360+
}

0 commit comments

Comments
 (0)