Skip to content

Commit 7ca6517

Browse files
authored
Merge pull request #56 from rulasg/fixes-and-improvements
fix(database): enhance JSON serialization with complex object support
2 parents b635375 + 5f4409f commit 7ca6517

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

Test/public/databasev2.test.ps1

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,46 @@ function Test_Database_JSON{
9191
Assert-IsFalse -Condition $result
9292
}
9393

94+
function Test_Database_JSON_ComplexObject{
95+
96+
Reset-InvokeCommandMock
97+
Mock_Database -ResetDatabase
98+
99+
# Load include files needed to test database
100+
. $(Get-Ps1FullPath -Name "databaseV2.ps1" -FolderName "Include" -ModuleRootPath $MODULE_ROOT_PATH)
101+
102+
$complexObject = @{
103+
Name = "Test Object"
104+
Value = 123
105+
Nested = @{
106+
Property1 = "Value1"
107+
Property2 = "Value2"
108+
}
109+
List = @(1, 2, 3, 4, 5)
110+
}
111+
112+
# Save complex object to database
113+
Save-DatabaseKey -Key "testComplex" -Value $complexObject -DBFormat "JSON"
114+
115+
# Get complex object from database
116+
$result = Get-DatabaseKey -Key "testComplex" -DBFormat "JSON"
117+
Assert-AreEqual -Expected "PSCustomObject" -Presented $result.GetType().Name
118+
Assert-AreEqual -Expected $complexObject.Name -Presented $result.Name
119+
Assert-AreEqual -Expected $complexObject.Value -Presented $result.Value
120+
Assert-AreEqual -Expected $complexObject.Nested.Property1 -Presented $result.Nested.Property1
121+
Assert-AreEqual -Expected $complexObject.Nested.Property2 -Presented $result.Nested.Property2
122+
Assert-AreEqual -Expected $complexObject.List.Count -Presented $result.List.Count
123+
124+
# get complex object as hashtable AsHashtable
125+
$resultHashtable = Get-DatabaseKey -Key "testComplex" -DBFormat "JSON" -AsHashtable
126+
Assert-AreEqual -Expected "OrderedHashtable" -Presented $resultHashtable.GetType().Name
127+
Assert-AreEqual -Expected $complexObject.Name -Presented $resultHashtable["Name"]
128+
Assert-AreEqual -Expected $complexObject.Value -Presented $resultHashtable["Value"]
129+
Assert-AreEqual -Expected $complexObject.Nested.Property1 -Presented $resultHashtable["Nested"]["Property1"]
130+
Assert-AreEqual -Expected $complexObject.Nested.Property2 -Presented $resultHashtable["Nested"]["Property2"]
131+
Assert-AreEqual -Expected $complexObject.List.Count -Presented $resultHashtable["List"].Count
132+
}
133+
94134
function Test_Database_XML{
95135

96136
Reset-InvokeCommandMock

include/databaseV2.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ function Get-DatabaseKey{
102102
[CmdletBinding()]
103103
param(
104104
[Parameter(Mandatory, Position = 0)][string]$Key,
105-
[Parameter(Position = 1)][ValidateSet("JSON","XML","TXT")][string]$DBFormat = "JSON"
105+
[Parameter(Position = 1)][ValidateSet("JSON","XML","TXT")][string]$DBFormat = "JSON",
106+
[Parameter()][switch]$AsHashtable
106107
)
107108

108109
if(-Not (Test-DatabaseKey $Key -DBFormat $DBFormat)){
@@ -112,7 +113,7 @@ function Get-DatabaseKey{
112113
$path = GetDatabaseFile $Key -DBFormat $DBFormat
113114

114115
switch ($DBFormat) {
115-
"JSON" { $ret = Get-Content $path | ConvertFrom-Json ; Break }
116+
"JSON" { $ret = Get-Content $path | ConvertFrom-Json -AsHashtable:$AsHashtable ; Break }
116117
"XML" { $ret = Import-Clixml -Path $path ; Break }
117118
"TXT" { $ret = Get-Content $path ; Break }
118119
default { throw "Unsupported database format $DbFormat" }

0 commit comments

Comments
 (0)