Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ protected override void EndProcessing()
// values cannot be evaluated are treated as having the value null.
object preprocessedObject = ProcessValue(objectToProcess, 0);
#if CORECLR
string output = JsonConvert.SerializeObject(preprocessedObject, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 });
JsonSerializerSettings jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 };
if (!Compress)
{
jsonSettings.Formatting = Formatting.Indented;
}
string output = JsonConvert.SerializeObject(preprocessedObject, jsonSettings);
WriteObject(output);
#else
// In Full CLR, we use the JavaScriptSerializer for which RecursionLimit was set to the default value of 100 (the actual recursion limit is 99 since
// at 100 the exception is thrown). See https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.recursionlimit(v=vs.110).aspx
Expand All @@ -131,8 +137,8 @@ protected override void EndProcessing()
JavaScriptSerializer helper = new JavaScriptSerializer() { RecursionLimit = (maxDepthAllowed + 2) };
helper.MaxJsonLength = Int32.MaxValue;
string output = helper.Serialize(preprocessedObject);
#endif
WriteObject(Compress ? output : ConvertToPrettyJsonString(output));
#endif
}
}

Expand Down Expand Up @@ -296,7 +302,7 @@ private int ConvertDictionary(string json, int index, StringBuilder result, stri
bool headChar = true;
bool beforeQuote = true;
int newSpaceCount = 0;
const int spaceCountAfterQuoteMark = 2;
const int spaceCountAfterQuoteMark = 1;

for (int i = index; i < json.Length; i++)
{
Expand Down Expand Up @@ -327,15 +333,14 @@ private int ConvertDictionary(string json, int index, StringBuilder result, stri
int end = ConvertQuotedString(json, i + 1, result);
if (beforeQuote)
{
newSpaceCount += (end - i + 1);
newSpaceCount = 0;
}
i = end;
headChar = false;
break;
case ':':
result.Append(json[i]);
AddSpaces(spaceCountAfterQuoteMark, result);
newSpaceCount += 3;
headChar = false;
beforeQuote = false;
break;
Expand Down Expand Up @@ -373,7 +378,7 @@ private int ConvertDictionary(string json, int index, StringBuilder result, stri
/// <param name="result"></param>
private void AddIndentations(int numberOfTabsToReturn, StringBuilder result)
{
int realNumber = numberOfTabsToReturn * 4;
int realNumber = numberOfTabsToReturn * 2;
for (int i = 0; i < realNumber; i++)
{
result.Append(' ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,30 @@ Describe "Validate Json serialization" -Tags "CI" {
$actual | Should Be $expectedNoWhiteSpace
}
}


Context "Validate Json output is either Pretty or Compressed" {

It "Should print a pretty Array" {
$array = 'one', 'two', 'three'
$response = $array | ConvertTo-Json
($response -split "\r?\n")[1] | Should Be ' "one",'
}

It "Should print a pretty dictionary" {
$dictionary = [Ordered]@{
'one' = 1
'two' = 2
'three' = 3
}
$response2 = $dictionary | ConvertTo-Json
($response2 -split "\r?\n")[1] | Should Be ' "one": 1,'
}

It "Should minify Json with Compress switch" {
(@{ a = 1 } | ConvertTo-Json -Compress).Length | Should Be 7
}
}
}

Describe "Json Bug fixes" -Tags "Feature" {
Expand Down