Add Jobject serialization support to ConvertTo-Json (#5141)

This commit is contained in:
Chunqing Chen
2017-11-03 11:35:09 -07:00
committed by Travis Plunk
parent ed499ced53
commit 36b600d1ec
2 changed files with 21 additions and 0 deletions
@@ -426,6 +426,10 @@ namespace Microsoft.PowerShell.Commands
{
rv = obj;
}
else if (obj is Newtonsoft.Json.Linq.JObject jObject)
{
rv = jObject.ToObject<Dictionary<object,object>>();
}
else
{
TypeInfo t = obj.GetType().GetTypeInfo();
@@ -0,0 +1,17 @@
Describe 'ConvertTo-Json' -tags "CI" {
It 'Newtonsoft.Json.Linq.Jproperty should be converted to Json properly' {
$EgJObject = New-Object -TypeName Newtonsoft.Json.Linq.JObject
$EgJObject.Add("TestValue1", "123456")
$EgJObject.Add("TestValue2", "78910")
$EgJObject.Add("TestValue3", "99999")
$dict = @{}
$dict.Add('JObject', $EgJObject)
$dict.Add('StrObject', 'This is a string Object')
$properties = @{'DictObject' = $dict; 'RandomString' = 'A quick brown fox jumped over the lazy dog'}
$object = New-Object -TypeName psobject -Property $properties
$jsonFormat = ConvertTo-Json -InputObject $object
$jsonFormat | Should Match '"TestValue1": 123456'
$jsonFormat | Should Match '"TestValue2": 78910'
$jsonFormat | Should Match '"TestValue3": 99999'
}
}