-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Support StopProcessing in ConvertTo-Json #6392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
fb52406
a84bfcb
5bd2fec
3c076d4
8c2dc94
38a3115
4a141b1
ce92da6
823d6f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,20 +108,18 @@ protected override void EndProcessing() | |
| // Pre-process the object so that it serializes the same, except that properties whose | ||
| // values cannot be evaluated are treated as having the value null. | ||
| object preprocessedObject = ProcessValue(objectToProcess, 0); | ||
| if (!_stopping) | ||
| CheckStopping(); | ||
| JsonSerializerSettings jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 }; | ||
| if (EnumsAsStrings) | ||
| { | ||
| JsonSerializerSettings jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 }; | ||
| if (EnumsAsStrings) | ||
| { | ||
| jsonSettings.Converters.Add(new StringEnumConverter()); | ||
| } | ||
| if (!Compress) | ||
| { | ||
| jsonSettings.Formatting = Formatting.Indented; | ||
| } | ||
| string output = JsonConvert.SerializeObject(preprocessedObject, jsonSettings); | ||
| WriteObject(output); | ||
| jsonSettings.Converters.Add(new StringEnumConverter()); | ||
| } | ||
| if (!Compress) | ||
| { | ||
| jsonSettings.Formatting = Formatting.Indented; | ||
| } | ||
| string output = JsonConvert.SerializeObject(preprocessedObject, jsonSettings); | ||
| WriteObject(output); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -177,11 +175,7 @@ private string ConvertToPrettyJsonString(string json) | |
| /// <returns></returns> | ||
| private int ConvertList(string json, int index, StringBuilder result, string padString, int numberOfSpaces) | ||
| { | ||
| if (_stopping) | ||
| { | ||
| throw new StopUpstreamCommandsException(this); | ||
| } | ||
|
|
||
| CheckStopping(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be either inside in for loop or mirrored there. #Closed |
||
| result.Append("\r\n"); | ||
| StringBuilder newPadString = new StringBuilder(); | ||
| newPadString.Append(padString); | ||
|
|
@@ -251,11 +245,7 @@ private int ConvertList(string json, int index, StringBuilder result, string pad | |
| /// <returns></returns> | ||
| private int ConvertQuotedString(string json, int index, StringBuilder result) | ||
| { | ||
| if (_stopping) | ||
| { | ||
| throw new StopUpstreamCommandsException(this); | ||
| } | ||
|
|
||
| CheckStopping(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like above, consider moving this inside the for loop. #Closed |
||
| for (int i = index; i < json.Length; i++) | ||
| { | ||
| result.Append(json[i]); | ||
|
|
@@ -296,11 +286,7 @@ private int ConvertQuotedString(string json, int index, StringBuilder result) | |
| /// <returns></returns> | ||
| private int ConvertDictionary(string json, int index, StringBuilder result, string padString, int numberOfSpaces) | ||
| { | ||
| if (_stopping) | ||
| { | ||
| throw new StopUpstreamCommandsException(this); | ||
| } | ||
|
|
||
| CheckStopping(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, inside the for loop. #Closed |
||
| result.Append("\r\n"); | ||
| StringBuilder newPadString = new StringBuilder(); | ||
| newPadString.Append(padString); | ||
|
|
@@ -429,11 +415,7 @@ private ErrorRecord NewError() | |
| /// <returns>An object suitable for serializing to JSON</returns> | ||
| private object ProcessValue(object obj, int depth) | ||
| { | ||
| if (_stopping) | ||
| { | ||
| throw new StopUpstreamCommandsException(this); | ||
| } | ||
|
|
||
| CheckStopping(); | ||
| PSObject pso = obj as PSObject; | ||
|
|
||
| if (pso != null) | ||
|
|
@@ -619,11 +601,7 @@ private void AppendPsProperties(PSObject psobj, IDictionary receiver, int depth, | |
| /// <returns></returns> | ||
| private object ProcessDictionary(IDictionary dict, int depth) | ||
| { | ||
| if (_stopping) | ||
| { | ||
| throw new StopUpstreamCommandsException(this); | ||
| } | ||
|
|
||
| CheckStopping(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider inside the for loop. #Closed |
||
| Dictionary<string, object> result = new Dictionary<string, object>(dict.Count); | ||
|
|
||
| foreach (DictionaryEntry entry in dict) | ||
|
|
@@ -658,10 +636,7 @@ private object ProcessEnumerable(IEnumerable enumerable, int depth) | |
|
|
||
| foreach (object o in enumerable) | ||
| { | ||
| if (_stopping) | ||
| { | ||
| throw new StopUpstreamCommandsException(this); | ||
| } | ||
| CheckStopping(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. For each item in the enumerable,
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will remove |
||
| result.Add(ProcessValue(o, depth + 1)); | ||
| } | ||
|
|
||
|
|
@@ -686,7 +661,7 @@ private object ProcessCustomObject<T>(object o, int depth) | |
|
|
||
| foreach (FieldInfo info in t.GetFields(BindingFlags.Public | BindingFlags.Instance)) | ||
| { | ||
| if (!info.IsDefined(typeof(T), true) && !_stopping) | ||
| if (!info.IsDefined(typeof(T), true)) | ||
| { | ||
| object value; | ||
| try | ||
|
|
@@ -704,7 +679,7 @@ private object ProcessCustomObject<T>(object o, int depth) | |
|
|
||
| foreach (PropertyInfo info2 in t.GetProperties(BindingFlags.Public | BindingFlags.Instance)) | ||
| { | ||
| if (!info2.IsDefined(typeof(T), true) && !_stopping) | ||
| if (!info2.IsDefined(typeof(T), true)) | ||
| { | ||
| MethodInfo getMethod = info2.GetGetMethod(); | ||
| if ((getMethod != null) && (getMethod.GetParameters().Length <= 0)) | ||
|
|
@@ -725,5 +700,16 @@ private object ProcessCustomObject<T>(object o, int depth) | |
| } | ||
| return result; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Check if StopProcessing() was called and throw special exception to stop | ||
| /// </summary> | ||
| private void CheckStopping() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems we should remove the the help method - used one time.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, with the removal of the unused code, this helper is no longer needed. Will remove. |
||
| { | ||
| if (_stopping) | ||
| { | ||
| throw new StopUpstreamCommandsException(this); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should use this exception. It's supposed to be thrown from a I suggest you use a custom exception instead, can catch the exception in object preprocessedObject = null;
try
{
preprocessedObject = ProcessValue(objectToProcess, 0);
}
catch (MyCustomException ex)
{
return;
}
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, will update. |
||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a little confused by this. The StoppingException is caught and ignored in one case but allowed to throw if ProcessValue() completes via CheckStopping(). What is the user experience? Is the user intended to see the exception or does the cmdlet just stop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to check for stopping again here. You may argue that the user can have a final chance to stop the command before the
JsonConvert.SerializeObjecthas the control, though :)If you really want to check for stop another time here, then
if (Stopping) { return }should be sufficient.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, will change.