BGC Tools
Static Public Member Functions
BGC.Extensions.JsonExtensions Class Reference

Static Public Member Functions

static JsonArray TryGetArray (this JsonObject json, string key)
 Tries to get array and returns an empty array if not found More...
 
static JsonValue TryGetValue (this JsonObject json, string key)
 Try and get value, if key is not there it adds the key More...
 
static List< int > JsonArrayToIntList (this JsonArray jsonArray)
 Converts a Json Array to a List of generic type T More...
 
static List< string > JsonaArrayToStringList (this JsonArray jsonArray)
 
static List< float > JsonArrayToFloatList (this JsonArray jsonArray)
 Converts a Json Array to a List of floats More...
 
static List< T > JsonArrayToEnumList< T > (this JsonArray jsonArray, EnumSerialization enumSerialization)
 Converts Any Json Array to a List of Enums More...
 
static JsonArray IntListToJsonArray (this IList< int > list)
 Converts an int list to a Json Array of Int Values More...
 
static JsonArray FloatListToJsonArray (this IList< float > list)
 Converts a float list to a Json Array of float values More...
 
static JsonArray AnyListToStringJsonArray< T > (this IList< T > list)
 Converts any list to a json array of string values More...
 
static JsonArray ConvertToJsonArray< T > (this List< T > list, Func< T, JsonValue > convertToJsonValue)
 Converts any list to a Json Array of user defined values More...
 
static List< T > JsonArrayToList< T > (this JsonArray jsonArray, Func< JsonValue, T > convertToObj)
 Converts any JsonArray to List of user defined values More...
 

Detailed Description

Definition at line 9 of file JsonExtensions.cs.

Member Function Documentation

◆ AnyListToStringJsonArray< T >()

static JsonArray BGC.Extensions.JsonExtensions.AnyListToStringJsonArray< T > ( this IList< T >  list)
inlinestatic

Converts any list to a json array of string values

Definition at line 136 of file JsonExtensions.cs.

References LightJson.JsonArray.Add(), and LightJson.JsonArray.Count.

137  {
138  int size = list.Count;
139  JsonArray jsonArray = new JsonArray();
140 
141  for (int i = 0; i < size; ++i)
142  {
143  jsonArray.Add(list[i].ToString());
144  }
145 
146  return jsonArray;
147 
148  }
Represents an ordered collection of JsonValues.
Definition: JsonArray.cs:11
JsonArray Add(JsonValue value)
Adds the given value to this collection.
Definition: JsonArray.cs:65
Here is the call graph for this function:

◆ ConvertToJsonArray< T >()

static JsonArray BGC.Extensions.JsonExtensions.ConvertToJsonArray< T > ( this List< T >  list,
Func< T, JsonValue convertToJsonValue 
)
inlinestatic

Converts any list to a Json Array of user defined values

Definition at line 153 of file JsonExtensions.cs.

References LightJson.JsonArray.Add().

154  {
155  JsonArray array = new JsonArray();
156 
157  int length = list.Count;
158  for(int i = 0; i < length; i++)
159  {
160  array.Add(convertToJsonValue(list[i]));
161  }
162 
163  return array;
164  }
Represents an ordered collection of JsonValues.
Definition: JsonArray.cs:11
JsonArray Add(JsonValue value)
Adds the given value to this collection.
Definition: JsonArray.cs:65
Here is the call graph for this function:

◆ FloatListToJsonArray()

static JsonArray BGC.Extensions.JsonExtensions.FloatListToJsonArray ( this IList< float >  list)
inlinestatic

Converts a float list to a Json Array of float values

Definition at line 120 of file JsonExtensions.cs.

References LightJson.JsonArray.Add(), and LightJson.JsonArray.Count.

121  {
122  int size = list.Count;
123  JsonArray jsonArray = new JsonArray();
124 
125  for (int i = 0; i < size; ++i)
126  {
127  jsonArray.Add(list[i]);
128  }
129 
130  return jsonArray;
131  }
Represents an ordered collection of JsonValues.
Definition: JsonArray.cs:11
JsonArray Add(JsonValue value)
Adds the given value to this collection.
Definition: JsonArray.cs:65
Here is the call graph for this function:

◆ IntListToJsonArray()

static JsonArray BGC.Extensions.JsonExtensions.IntListToJsonArray ( this IList< int >  list)
inlinestatic

Converts an int list to a Json Array of Int Values

Definition at line 104 of file JsonExtensions.cs.

References LightJson.JsonArray.Add(), and LightJson.JsonArray.Count.

105  {
106  int size = list.Count;
107  JsonArray jsonArray = new JsonArray();
108 
109  for (int i = 0; i < size; ++i)
110  {
111  jsonArray.Add(list[i]);
112  }
113 
114  return jsonArray;
115  }
Represents an ordered collection of JsonValues.
Definition: JsonArray.cs:11
JsonArray Add(JsonValue value)
Adds the given value to this collection.
Definition: JsonArray.cs:65
Here is the call graph for this function:

◆ JsonaArrayToStringList()

static List<string> BGC.Extensions.JsonExtensions.JsonaArrayToStringList ( this JsonArray  jsonArray)
inlinestatic

Definition at line 56 of file JsonExtensions.cs.

References LightJson.JsonArray.Count.

57  {
58  List<string> stringList = new List<string>();
59 
60  int count = jsonArray.Count;
61  for (int i = 0; i < count; ++i)
62  {
63  stringList.Add(jsonArray[i]);
64  }
65 
66  return stringList;
67  }

◆ JsonArrayToEnumList< T >()

static List<T> BGC.Extensions.JsonExtensions.JsonArrayToEnumList< T > ( this JsonArray  jsonArray,
EnumSerialization  enumSerialization 
)
inlinestatic

Converts Any Json Array to a List of Enums

Definition at line 88 of file JsonExtensions.cs.

89  {
90  int size = jsonArray.Count;
91  List<T> enumList = new List<T>(size);
92 
93  for (int i = 0; i < size; ++i)
94  {
95  enumList.Add(enumSerialization.StringToEnum<T>(jsonArray[i].AsString));
96  }
97 
98  return enumList;
99  }

◆ JsonArrayToFloatList()

static List<float> BGC.Extensions.JsonExtensions.JsonArrayToFloatList ( this JsonArray  jsonArray)
inlinestatic

Converts a Json Array to a List of floats

Definition at line 72 of file JsonExtensions.cs.

References LightJson.JsonArray.Count.

73  {
74  int size = jsonArray.Count;
75  List<float> floatList = new List<float>(size);
76 
77  for (int i = 0; i < size; ++i)
78  {
79  floatList.Add((float) jsonArray[i].AsNumber);
80  }
81 
82  return floatList;
83  }

◆ JsonArrayToIntList()

static List<int> BGC.Extensions.JsonExtensions.JsonArrayToIntList ( this JsonArray  jsonArray)
inlinestatic

Converts a Json Array to a List of generic type T

Definition at line 48 of file JsonExtensions.cs.

References LightJson.JsonValue.AsInteger.

49  {
50  return jsonArray.JsonArrayToList((JsonValue val) =>
51  {
52  return val.AsInteger;
53  });
54  }
A wrapper object that contains a valid JSON value.
Definition: JsonValue.cs:13
int AsInteger
This value as an Integer type.
Definition: JsonValue.cs:82

◆ JsonArrayToList< T >()

static List<T> BGC.Extensions.JsonExtensions.JsonArrayToList< T > ( this JsonArray  jsonArray,
Func< JsonValue, T >  convertToObj 
)
inlinestatic

Converts any JsonArray to List of user defined values

Definition at line 169 of file JsonExtensions.cs.

References LightJson.JsonArray.Add().

170  {
171  List<T> list = new List<T>();
172  for(int i = 0; i < jsonArray.Count; ++i)
173  {
174  list.Add(convertToObj(jsonArray[i]));
175  }
176 
177  return list;
178  }
Here is the call graph for this function:

◆ TryGetArray()

static JsonArray BGC.Extensions.JsonExtensions.TryGetArray ( this JsonObject  json,
string  key 
)
inlinestatic

Tries to get array and returns an empty array if not found

Definition at line 14 of file JsonExtensions.cs.

15  {
16  Assert.IsNotNull(json);
17  Assert.IsFalse(string.IsNullOrEmpty(key));
18 
19  JsonArray jsonArr = json.TryGetValue(key);
20 
21  if(jsonArr == null)
22  {
23  return new JsonArray();
24  }
25 
26  return jsonArr;
27  }
Represents an ordered collection of JsonValues.
Definition: JsonArray.cs:11

◆ TryGetValue()

static JsonValue BGC.Extensions.JsonExtensions.TryGetValue ( this JsonObject  json,
string  key 
)
inlinestatic

Try and get value, if key is not there it adds the key

Definition at line 32 of file JsonExtensions.cs.

References LightJson.JsonObject.Add(), and LightJson.JsonObject.ContainsKey().

33  {
34  Assert.IsNotNull(json);
35  Assert.IsFalse(string.IsNullOrEmpty(key));
36 
37  if (json.ContainsKey(key) == false)
38  {
39  json.Add(key);
40  }
41 
42  return json[key];
43  }
Here is the call graph for this function:

The documentation for this class was generated from the following file: