2015-10-31 00:15:19 +01:00
|
|
|
# While Core PowerShell does not support the JSON cmdlets, a third
|
|
|
|
# party C# library, [Json.NET](http://www.newtonsoft.com/json), can be
|
|
|
|
# loaded into PowerShell and used directly.
|
|
|
|
|
|
|
|
# http://www.newtonsoft.com/json/help/html/ParsingLINQtoJSON.htm
|
|
|
|
|
|
|
|
Describe "Json.NET LINQ Parsing" {
|
|
|
|
# load third party Json.NET library
|
2016-02-04 21:01:41 +01:00
|
|
|
if ([string]::IsNullOrEmpty($env:CORE_ROOT)) {
|
2016-03-04 23:52:27 +01:00
|
|
|
$base = [System.AppContext]::BaseDirectory
|
2016-02-04 21:01:41 +01:00
|
|
|
} else {
|
2016-03-04 23:52:27 +01:00
|
|
|
$base = $env:CORE_ROOT
|
2016-02-04 21:01:41 +01:00
|
|
|
}
|
|
|
|
|
2015-12-30 19:46:54 +01:00
|
|
|
$path = Join-Path $base Newtonsoft.Json.dll
|
2015-10-31 00:15:19 +01:00
|
|
|
[Microsoft.PowerShell.CoreCLR.AssemblyExtensions]::LoadFrom($path)
|
|
|
|
|
|
|
|
BeforeEach {
|
2016-03-04 23:52:27 +01:00
|
|
|
$jsonFile = Join-Path -Path (Join-Path $PSScriptRoot -ChildPath assets) -ChildPath TestJson.json
|
|
|
|
$jsonData = (Get-Content $jsonFile | Out-String)
|
|
|
|
$json = [Newtonsoft.Json.Linq.JObject]::Parse($jsonData)
|
2015-10-31 00:15:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should return data via Item()" {
|
2016-03-04 23:52:27 +01:00
|
|
|
[string]$json.Item("Name") | Should Be "Zaphod Beeblebrox"
|
2015-10-31 00:15:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should return data via []" {
|
2016-03-04 23:52:27 +01:00
|
|
|
[string]$json["Planet"] | Should Be "Betelgeuse"
|
2015-10-31 00:15:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should return nested data via Item().Item()" {
|
2016-03-04 23:52:27 +01:00
|
|
|
[int]$json.Item("Appendages").Item("Heads") | Should Be 2
|
2015-10-31 00:15:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should return nested data via [][]" {
|
2016-03-04 23:52:27 +01:00
|
|
|
[int]$json["Appendages"]["Arms"] | Should Be 3
|
2015-10-31 00:15:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should return correct array count" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$json["Achievements"].Count | Should Be 4
|
2015-10-31 00:15:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should return array data via [n]" {
|
2016-03-04 23:52:27 +01:00
|
|
|
[string]$json["Achievements"][3] | Should Be "One hoopy frood"
|
2015-10-31 00:15:19 +01:00
|
|
|
}
|
|
|
|
}
|