|
10 | 10 | using System.Management.Automation.Language; |
11 | 11 | using System.Numerics; |
12 | 12 | using System.Reflection; |
13 | | -using System.Text.RegularExpressions; |
14 | 13 | using System.Threading; |
15 | 14 |
|
16 | 15 | using Newtonsoft.Json; |
@@ -289,11 +288,11 @@ private static PSObject PopulateFromJDictionary(JObject entries, DuplicateMember |
289 | 288 | return null; |
290 | 289 | } |
291 | 290 |
|
292 | | - // Array |
293 | 291 | switch (entry.Value) |
294 | 292 | { |
295 | 293 | case JArray list: |
296 | 294 | { |
| 295 | + // Array |
297 | 296 | var listResult = PopulateFromJArray(list, out error); |
298 | 297 | if (error != null) |
299 | 298 | { |
@@ -331,47 +330,41 @@ private static PSObject PopulateFromJDictionary(JObject entries, DuplicateMember |
331 | 330 | // This function is a clone of PopulateFromList using JArray as input. |
332 | 331 | private static ICollection<object> PopulateFromJArray(JArray list, out ErrorRecord error) |
333 | 332 | { |
334 | | - error = null; |
335 | 333 | var result = new object[list.Count]; |
| 334 | + var i = 0; |
336 | 335 |
|
337 | | - for (var index = 0; index < list.Count; index++) |
| 336 | + foreach (var element in list) |
338 | 337 | { |
339 | | - var element = list[index]; |
340 | 338 | switch (element) |
341 | 339 | { |
342 | 340 | case JArray subList: |
| 341 | + // Array |
| 342 | + result[i++] = PopulateFromJArray(subList, out error); |
| 343 | + if (error != null) |
343 | 344 | { |
344 | | - // Array |
345 | | - var listResult = PopulateFromJArray(subList, out error); |
346 | | - if (error != null) |
347 | | - { |
348 | | - return null; |
349 | | - } |
350 | | - |
351 | | - result[index] = listResult; |
352 | | - break; |
| 345 | + return null; |
353 | 346 | } |
| 347 | + break; |
354 | 348 | case JObject dic: |
| 349 | + // Dictionary |
| 350 | + result[i++] = PopulateFromJDictionary(dic, new DuplicateMemberHashSet(dic.Count), out error); |
| 351 | + if (error != null) |
355 | 352 | { |
356 | | - // Dictionary |
357 | | - var dicResult = PopulateFromJDictionary(dic, new DuplicateMemberHashSet(dic.Count), out error); |
358 | | - if (error != null) |
359 | | - { |
360 | | - return null; |
361 | | - } |
362 | | - |
363 | | - result[index] = dicResult; |
364 | | - break; |
| 353 | + return null; |
365 | 354 | } |
| 355 | + break; |
366 | 356 | case JValue value: |
| 357 | + if (value.Type != JTokenType.Comment) |
367 | 358 | { |
368 | | - result[index] = value.Value; |
369 | | - break; |
| 359 | + result[i++] = value.Value; |
370 | 360 | } |
| 361 | + break; |
371 | 362 | } |
372 | 363 | } |
373 | 364 |
|
374 | | - return result; |
| 365 | + error = null; |
| 366 | + // In the common case of not having any comments, return the original array, otherwise create a sliced copy. |
| 367 | + return i == list.Count ? result : result[..i]; |
375 | 368 | } |
376 | 369 |
|
377 | 370 | // This function is a clone of PopulateFromDictionary using JObject as an input. |
@@ -434,48 +427,41 @@ private static Hashtable PopulateHashTableFromJDictionary(JObject entries, out E |
434 | 427 | // This function is a clone of PopulateFromList using JArray as input. |
435 | 428 | private static ICollection<object> PopulateHashTableFromJArray(JArray list, out ErrorRecord error) |
436 | 429 | { |
437 | | - error = null; |
438 | 430 | var result = new object[list.Count]; |
| 431 | + var i = 0; |
439 | 432 |
|
440 | | - for (var index = 0; index < list.Count; index++) |
| 433 | + foreach (var element in list) |
441 | 434 | { |
442 | | - var element = list[index]; |
443 | | - |
444 | 435 | switch (element) |
445 | 436 | { |
446 | | - case JArray array: |
| 437 | + case JArray subList: |
| 438 | + // Array |
| 439 | + result[i++] = PopulateHashTableFromJArray(subList, out error); |
| 440 | + if (error != null) |
447 | 441 | { |
448 | | - // Array |
449 | | - var listResult = PopulateHashTableFromJArray(array, out error); |
450 | | - if (error != null) |
451 | | - { |
452 | | - return null; |
453 | | - } |
454 | | - |
455 | | - result[index] = listResult; |
456 | | - break; |
| 442 | + return null; |
457 | 443 | } |
| 444 | + break; |
458 | 445 | case JObject dic: |
| 446 | + // Dictionary |
| 447 | + result[i++] = PopulateHashTableFromJDictionary(dic, out error); |
| 448 | + if (error != null) |
459 | 449 | { |
460 | | - // Dictionary |
461 | | - var dicResult = PopulateHashTableFromJDictionary(dic, out error); |
462 | | - if (error != null) |
463 | | - { |
464 | | - return null; |
465 | | - } |
466 | | - |
467 | | - result[index] = dicResult; |
468 | | - break; |
| 450 | + return null; |
469 | 451 | } |
| 452 | + break; |
470 | 453 | case JValue value: |
| 454 | + if (value.Type != JTokenType.Comment) |
471 | 455 | { |
472 | | - result[index] = value.Value; |
473 | | - break; |
| 456 | + result[i++] = value.Value; |
474 | 457 | } |
| 458 | + break; |
475 | 459 | } |
476 | 460 | } |
477 | 461 |
|
478 | | - return result; |
| 462 | + error = null; |
| 463 | + // In the common case of not having any comments, return the original array, otherwise create a sliced copy. |
| 464 | + return i == list.Count ? result : result[..i]; |
479 | 465 | } |
480 | 466 |
|
481 | 467 | #endregion ConvertFromJson |
|
0 commit comments