BGC Tools
Public Member Functions | Static Public Member Functions | Properties | Private Member Functions | Static Private Member Functions | Private Attributes
LightJson.Serialization.JsonWriter Class Reference

Represents a writer that can write string representations of JsonValues. More...

Collaboration diagram for LightJson.Serialization.JsonWriter:
Collaboration graph
[legend]

Public Member Functions

 JsonWriter (TextWriter innerWriter)
 Initializes a new instance of JsonWriter. More...
 
 JsonWriter (TextWriter innerWriter, bool pretty)
 Initializes a new instance of JsonWriter. More...
 
void Write (JsonValue jsonValue)
 Writes the given value to the InnerWriter. More...
 

Static Public Member Functions

static string Serialize (JsonValue value)
 Returns a string representation of the given JsonValue. More...
 
static string Serialize (JsonValue value, bool pretty)
 Generates a string representation of the given value. More...
 

Properties

string IndentString [get, set]
 The string representing indentation in the output. More...
 
string SpacingString [get, set]
 The string representing spacing in the output. More...
 
string NewLineString [get, set]
 The string representing a new line on the output. More...
 
bool SortObjects [get, set]
 Whether JsonObject properties should be written in a deterministic order. More...
 
TextWriter InnerWriter [get, set]
 The TextWriter to which this JsonWriter writes. More...
 

Private Member Functions

void Write (string text)
 
void WriteEncodedJsonValue (JsonValue value)
 
void WriteEncodedString (string text)
 
void WriteIndentation ()
 
void WriteSpacing ()
 
void WriteLine ()
 
void WriteLine (string line)
 
void AddRenderingCollection (IEnumerable< JsonValue > value)
 
void RemoveRenderingCollection (IEnumerable< JsonValue > value)
 
void Render (JsonValue value)
 
void Render (JsonArray value)
 
void Render (JsonObject value)
 
IEnumerator< KeyValuePair< string, JsonValue > > GetJsonObjectEnumerator (JsonObject jsonObject)
 Gets a JsonObject enumarator based on the configuration of this JsonWriter. If JsonWriter.SortObjects is set to true, then a ordered enumerator is returned. Otherwise, a faster non-deterministic enumerator is returned. More...
 

Static Private Member Functions

static bool IsValidNumber (double number)
 

Private Attributes

int indent
 
bool isNewLine
 
HashSet< IEnumerable< JsonValue > > renderingCollections = new HashSet<IEnumerable<JsonValue>>()
 A set of containing all the collection objects (JsonObject/JsonArray) being rendered. It is used to prevent circular references; since collections that contain themselves will never finish rendering. More...
 

Detailed Description

Represents a writer that can write string representations of JsonValues.

Definition at line 14 of file JsonWriter.cs.

Constructor & Destructor Documentation

◆ JsonWriter() [1/2]

LightJson.Serialization.JsonWriter.JsonWriter ( TextWriter  innerWriter)
inline

Initializes a new instance of JsonWriter.

Parameters
innerWriterThe TextWriter used to write JsonValues.

Definition at line 45 of file JsonWriter.cs.

45 : this(innerWriter, false) { }

◆ JsonWriter() [2/2]

LightJson.Serialization.JsonWriter.JsonWriter ( TextWriter  innerWriter,
bool  pretty 
)
inline

Initializes a new instance of JsonWriter.

Parameters
innerWriterThe TextWriter used to write JsonValues.
prettyWhether the output of the writer should be human-readable.

Definition at line 52 of file JsonWriter.cs.

53  {
54  if (pretty)
55  {
56  IndentString = "\t";
57  SpacingString = " ";
58  NewLineString = "\n";
59  }
60 
61  InnerWriter = innerWriter;
62  }
string SpacingString
The string representing spacing in the output.
Definition: JsonWriter.cs:30
string NewLineString
The string representing a new line on the output.
Definition: JsonWriter.cs:33
TextWriter InnerWriter
The TextWriter to which this JsonWriter writes.
Definition: JsonWriter.cs:39
string IndentString
The string representing indentation in the output.
Definition: JsonWriter.cs:27

Member Function Documentation

◆ AddRenderingCollection()

void LightJson.Serialization.JsonWriter.AddRenderingCollection ( IEnumerable< JsonValue value)
inlineprivate

Definition at line 190 of file JsonWriter.cs.

191  {
192  if (!renderingCollections.Add(value))
193  {
194  throw new JsonSerializationException(ErrorType.CircularReference);
195  }
196  }
HashSet< IEnumerable< JsonValue > > renderingCollections
A set of containing all the collection objects (JsonObject/JsonArray) being rendered. It is used to prevent circular references; since collections that contain themselves will never finish rendering.
Definition: JsonWriter.cs:24

◆ GetJsonObjectEnumerator()

IEnumerator<KeyValuePair<string, JsonValue> > LightJson.Serialization.JsonWriter.GetJsonObjectEnumerator ( JsonObject  jsonObject)
inlineprivate

Gets a JsonObject enumarator based on the configuration of this JsonWriter. If JsonWriter.SortObjects is set to true, then a ordered enumerator is returned. Otherwise, a faster non-deterministic enumerator is returned.

Parameters
jsonObjectThe JsonObject for which to get an enumerator.

Definition at line 308 of file JsonWriter.cs.

References LightJson.JsonObject.GetEnumerator().

309  {
310  if (SortObjects)
311  {
312  SortedDictionary<string, JsonValue> sortedDictionary = new SortedDictionary<string, JsonValue>(StringComparer.Ordinal);
313 
314  foreach (KeyValuePair<string, JsonValue> item in jsonObject)
315  {
316  sortedDictionary.Add(item.Key, item.Value);
317  }
318 
319  return sortedDictionary.GetEnumerator();
320  }
321  else
322  {
323  return jsonObject.GetEnumerator();
324  }
325  }
bool SortObjects
Whether JsonObject properties should be written in a deterministic order.
Definition: JsonWriter.cs:36
Here is the call graph for this function:

◆ IsValidNumber()

static bool LightJson.Serialization.JsonWriter.IsValidNumber ( double  number)
staticprivate

◆ RemoveRenderingCollection()

void LightJson.Serialization.JsonWriter.RemoveRenderingCollection ( IEnumerable< JsonValue value)
inlineprivate

Definition at line 198 of file JsonWriter.cs.

199  {
200  renderingCollections.Remove(value);
201  }
HashSet< IEnumerable< JsonValue > > renderingCollections
A set of containing all the collection objects (JsonObject/JsonArray) being rendered. It is used to prevent circular references; since collections that contain themselves will never finish rendering.
Definition: JsonWriter.cs:24

◆ Render() [1/3]

void LightJson.Serialization.JsonWriter.Render ( JsonValue  value)
inlineprivate

Definition at line 203 of file JsonWriter.cs.

References LightJson.JsonValue.Type.

204  {
205  switch (value.Type)
206  {
207  case JsonValueType.Null:
208  case JsonValueType.Boolean:
209  case JsonValueType.Number:
210  case JsonValueType.String:
211  WriteEncodedJsonValue(value);
212  break;
213 
214  case JsonValueType.Object:
215  Render((JsonObject)value);
216  break;
217 
218  case JsonValueType.Array:
219  Render((JsonArray)value);
220  break;
221 
222  default:
223  throw new JsonSerializationException(ErrorType.InvalidValueType);
224  }
225  }
void WriteEncodedJsonValue(JsonValue value)
Definition: JsonWriter.cs:75
JsonValueType
Enumerates the types of Json values.
Definition: JsonValueType.cs:8
void Render(JsonValue value)
Definition: JsonWriter.cs:203

◆ Render() [2/3]

void LightJson.Serialization.JsonWriter.Render ( JsonArray  value)
inlineprivate

Definition at line 227 of file JsonWriter.cs.

References LightJson.JsonArray.GetEnumerator().

228  {
229  AddRenderingCollection(value);
230 
231  WriteLine("[");
232 
233  indent += 1;
234 
235  using (IEnumerator<JsonValue> enumerator = value.GetEnumerator())
236  {
237  bool hasNext = enumerator.MoveNext();
238 
239  while (hasNext)
240  {
241  Render(enumerator.Current);
242 
243  hasNext = enumerator.MoveNext();
244 
245  if (hasNext)
246  {
247  WriteLine(",");
248  }
249  else
250  {
251  WriteLine();
252  }
253  }
254  }
255 
256  indent -= 1;
257 
258  Write("]");
259 
261  }
void Render(JsonValue value)
Definition: JsonWriter.cs:203
void AddRenderingCollection(IEnumerable< JsonValue > value)
Definition: JsonWriter.cs:190
void RemoveRenderingCollection(IEnumerable< JsonValue > value)
Definition: JsonWriter.cs:198
Here is the call graph for this function:

◆ Render() [3/3]

void LightJson.Serialization.JsonWriter.Render ( JsonObject  value)
inlineprivate

Definition at line 263 of file JsonWriter.cs.

264  {
265  AddRenderingCollection(value);
266 
267  WriteLine("{");
268 
269  indent += 1;
270 
271  using (IEnumerator<KeyValuePair<string, JsonValue>> enumerator = GetJsonObjectEnumerator(value))
272  {
273  bool hasNext = enumerator.MoveNext();
274 
275  while (hasNext)
276  {
277  WriteEncodedString(enumerator.Current.Key);
278  Write(":");
279  WriteSpacing();
280  Render(enumerator.Current.Value);
281 
282  hasNext = enumerator.MoveNext();
283 
284  if (hasNext)
285  {
286  WriteLine(",");
287  }
288  else
289  {
290  WriteLine();
291  }
292  }
293  }
294 
295  indent -= 1;
296 
297  Write("}");
298 
300  }
IEnumerator< KeyValuePair< string, JsonValue > > GetJsonObjectEnumerator(JsonObject jsonObject)
Gets a JsonObject enumarator based on the configuration of this JsonWriter. If JsonWriter.SortObjects is set to true, then a ordered enumerator is returned. Otherwise, a faster non-deterministic enumerator is returned.
Definition: JsonWriter.cs:308
void WriteEncodedString(string text)
Definition: JsonWriter.cs:113
void Render(JsonValue value)
Definition: JsonWriter.cs:203
void AddRenderingCollection(IEnumerable< JsonValue > value)
Definition: JsonWriter.cs:190
void RemoveRenderingCollection(IEnumerable< JsonValue > value)
Definition: JsonWriter.cs:198

◆ Serialize() [1/2]

static string LightJson.Serialization.JsonWriter.Serialize ( JsonValue  value)
static

Returns a string representation of the given JsonValue.

Parameters
valueThe JsonValue to serialize.

Referenced by LightJson.JsonArray.Clear(), LightJson.JsonValue.GetHashCode(), and LightJson.JsonObject.Rename().

Here is the caller graph for this function:

◆ Serialize() [2/2]

static string LightJson.Serialization.JsonWriter.Serialize ( JsonValue  value,
bool  pretty 
)
inlinestatic

Generates a string representation of the given value.

Parameters
valueThe value to serialize.
prettyIndicates whether the resulting string should be formatted for human-readability.

Definition at line 357 of file JsonWriter.cs.

References LightJson.Serialization.JsonWriter.Write().

358  {
359  using (StringWriter stringWriter = new StringWriter())
360  {
361  JsonWriter jsonWriter = new JsonWriter(stringWriter, pretty);
362 
363  jsonWriter.Write(value);
364 
365  return stringWriter.ToString();
366  }
367  }
JsonWriter(TextWriter innerWriter)
Initializes a new instance of JsonWriter.
Definition: JsonWriter.cs:45
Here is the call graph for this function:

◆ Write() [1/2]

void LightJson.Serialization.JsonWriter.Write ( string  text)
inlineprivate

Definition at line 64 of file JsonWriter.cs.

Referenced by LightJson.Serialization.JsonWriter.Serialize().

65  {
66  if (isNewLine)
67  {
68  isNewLine = false;
70  }
71 
72  InnerWriter.Write(text);
73  }
TextWriter InnerWriter
The TextWriter to which this JsonWriter writes.
Definition: JsonWriter.cs:39
Here is the caller graph for this function:

◆ Write() [2/2]

void LightJson.Serialization.JsonWriter.Write ( JsonValue  jsonValue)
inline

Writes the given value to the InnerWriter.

Parameters
jsonValueThe JsonValue to write.

Definition at line 333 of file JsonWriter.cs.

334  {
335  indent = 0;
336  isNewLine = true;
337 
338  Render(jsonValue);
339 
340  renderingCollections.Clear();
341  }
HashSet< IEnumerable< JsonValue > > renderingCollections
A set of containing all the collection objects (JsonObject/JsonArray) being rendered. It is used to prevent circular references; since collections that contain themselves will never finish rendering.
Definition: JsonWriter.cs:24
void Render(JsonValue value)
Definition: JsonWriter.cs:203

◆ WriteEncodedJsonValue()

void LightJson.Serialization.JsonWriter.WriteEncodedJsonValue ( JsonValue  value)
inlineprivate

Definition at line 75 of file JsonWriter.cs.

References LightJson.JsonValue.AsString, and LightJson.JsonValue.Type.

76  {
77  switch (value.Type)
78  {
79  case JsonValueType.Null:
80  Write("null");
81  break;
82 
83  case JsonValueType.Boolean:
84  Write(value.AsString);
85  break;
86 
87  case JsonValueType.Number:
88  if (!IsValidNumber(value))
89  {
90  throw new JsonSerializationException(ErrorType.InvalidNumber);
91  }
92 
93  Write(((double)value).ToString(CultureInfo.InvariantCulture));
94  break;
95 
96  case JsonValueType.String:
97  WriteEncodedString((string)value);
98  break;
99 
100  case JsonValueType.Object:
101  Write($"JsonObject[{value.AsJsonObject.Count}]");
102  break;
103 
104  case JsonValueType.Array:
105  Write($"JsonArray[{value.AsJsonArray.Count}]");
106  break;
107 
108  default:
109  throw new InvalidOperationException("Invalid value type.");
110  }
111  }
static bool IsValidNumber(double number)
JsonValueType
Enumerates the types of Json values.
Definition: JsonValueType.cs:8
void WriteEncodedString(string text)
Definition: JsonWriter.cs:113

◆ WriteEncodedString()

void LightJson.Serialization.JsonWriter.WriteEncodedString ( string  text)
inlineprivate

Definition at line 113 of file JsonWriter.cs.

114  {
115  Write("\"");
116 
117  for (int i = 0; i < text.Length; i += 1)
118  {
119  char currentChar = text[i];
120 
121  // Encoding special characters.
122  switch (currentChar)
123  {
124  case '\\':
125  InnerWriter.Write("\\\\");
126  break;
127 
128  case '\"':
129  InnerWriter.Write("\\\"");
130  break;
131 
132  case '/':
133  InnerWriter.Write("\\/");
134  break;
135 
136  case '\b':
137  InnerWriter.Write("\\b");
138  break;
139 
140  case '\f':
141  InnerWriter.Write("\\f");
142  break;
143 
144  case '\n':
145  InnerWriter.Write("\\n");
146  break;
147 
148  case '\r':
149  InnerWriter.Write("\\r");
150  break;
151 
152  case '\t':
153  InnerWriter.Write("\\t");
154  break;
155 
156  default:
157  InnerWriter.Write(currentChar);
158  break;
159  }
160  }
161 
162  InnerWriter.Write("\"");
163  }
TextWriter InnerWriter
The TextWriter to which this JsonWriter writes.
Definition: JsonWriter.cs:39

◆ WriteIndentation()

void LightJson.Serialization.JsonWriter.WriteIndentation ( )
inlineprivate

Definition at line 165 of file JsonWriter.cs.

166  {
167  for (int i = 0; i < indent; i += 1)
168  {
170  }
171  }
string IndentString
The string representing indentation in the output.
Definition: JsonWriter.cs:27

◆ WriteLine() [1/2]

void LightJson.Serialization.JsonWriter.WriteLine ( )
inlineprivate

Definition at line 178 of file JsonWriter.cs.

179  {
181  isNewLine = true;
182  }
string NewLineString
The string representing a new line on the output.
Definition: JsonWriter.cs:33

◆ WriteLine() [2/2]

void LightJson.Serialization.JsonWriter.WriteLine ( string  line)
inlineprivate

Definition at line 184 of file JsonWriter.cs.

185  {
186  Write(line);
187  WriteLine();
188  }

◆ WriteSpacing()

void LightJson.Serialization.JsonWriter.WriteSpacing ( )
inlineprivate

Definition at line 173 of file JsonWriter.cs.

174  {
176  }
string SpacingString
The string representing spacing in the output.
Definition: JsonWriter.cs:30

Field Documentation

◆ indent

int LightJson.Serialization.JsonWriter.indent
private

Definition at line 16 of file JsonWriter.cs.

◆ isNewLine

bool LightJson.Serialization.JsonWriter.isNewLine
private

Definition at line 17 of file JsonWriter.cs.

◆ renderingCollections

HashSet<IEnumerable<JsonValue> > LightJson.Serialization.JsonWriter.renderingCollections = new HashSet<IEnumerable<JsonValue>>()
private

A set of containing all the collection objects (JsonObject/JsonArray) being rendered. It is used to prevent circular references; since collections that contain themselves will never finish rendering.

Definition at line 24 of file JsonWriter.cs.

Property Documentation

◆ IndentString

string LightJson.Serialization.JsonWriter.IndentString
getset

The string representing indentation in the output.

Definition at line 27 of file JsonWriter.cs.

◆ InnerWriter

TextWriter LightJson.Serialization.JsonWriter.InnerWriter
getset

The TextWriter to which this JsonWriter writes.

Definition at line 39 of file JsonWriter.cs.

◆ NewLineString

string LightJson.Serialization.JsonWriter.NewLineString
getset

The string representing a new line on the output.

Definition at line 33 of file JsonWriter.cs.

◆ SortObjects

bool LightJson.Serialization.JsonWriter.SortObjects
getset

Whether JsonObject properties should be written in a deterministic order.

Definition at line 36 of file JsonWriter.cs.

◆ SpacingString

string LightJson.Serialization.JsonWriter.SpacingString
getset

The string representing spacing in the output.

Definition at line 30 of file JsonWriter.cs.


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