Is this an edge case in PowerShell's type resolution mechanism? -


here's situation. have powershell module imports powershell-json library; library converts json strings powershell objects. module provides interface couchdb, json strings obtained http calls. have function, send-couchdbrequest, makes http request couchdb , returns response data string (the response data obtained using streamreader.readtoend()).

in function, get-couchdbdocument, make call send-couchdbrequest, , save output in variable, $json. according $json.gettype() , $json | get-member, of type system.string. if feed string convertfrom-json, expect pscustomobject properties defined according json document supplied it; instead, string representation of powershell hashtable, i.e., @{name1=value; name2=value2; name3=value3}. object returned of type system.string based on same tests above, rather expected pscustomobject. seems me powershell doing kind of automatic (and unwanted/unneeded) type conversion here.

the bug isn't in powershell-json - i've discussed author, , have both managed same call convertfrom-json work in dummy module. therefore conclude error must in code somewhere, perhaps result of fact string has come in on http.

the code get-couchdbdocument follows:

function get-couchdbdocument {     param(         [string] $document = $(throw "document id required."),         [string] $database = $(throw "database name required."),         [string] $server = "127.0.01",         [int] $port = 5984     )      $json = send-couchdbrequest -dbhost $server -port $port -database $database -document $document -includedoc     $document = $json | convertfrom-json     write-output $document } 

the code send-couchdbrequest quite lengthy, , can found on github. sample json string, fails in scenario have described , works elsewhere, is:

{"_id":"f42d2e0c5be0a7ab7bdc1cba23fc1d73","_rev":"1-59414e77c768bc202142ac82c2f129de","key":"value"} 

any thoughts? many in advance.

wow, 1 quite tricky.

what change variable name stores object.

$doc = $json | convertfrom-json write-output $doc 

reason: specified in param block, type of $document variable should [string]. that's why powershell try convert output convertfrom-json string.

other possible solution specify $document parameter without type.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -