2

I have the task of de-serializing a PSON (PowerShell Object Notation) file and am struggling to get it working. Apparently the PSON file is a variant of JSON used by Puppet which encodes the strings differently - according to this source

I have tried just using the standard JSON.Net DeserializeObject method:

using (var r = new StreamReader(psonFilePath))
{
    string json = r.ReadToEnd();
    dynamic jsonObject = JsonConvert.DeserializeObject(json);
}

The above code throws an exception:

"Unexpected character encountered while parsing value: @. Path '', line 0, position 0."

which makes sense as a standard JSON file wouldn't have an '@' at the start.

An example of the PSON file is below:

@{
    shapes = @{
        'SQ4297' = @{
            shapeid = 'SQ4297'
            shapetype = 'square'
            sides = 'four'
            colour = 'purple'
        }
        'SQ6281' = @{
            shapeid = 'SQ6281'
            shapetype = 'square'
            sides = 'four'
            colour = 'orange'
        }
        'TR14' = @{
            shapeid = 'TR14'
            shapetype = 'triange'
            sides = 'three'
            colour = 'green'
        }
    }
}

The link mentioned above states that

most parsers will produce usable output from PSON if they are instructed to interpret the input as Latin-1 encoding

I have tried using different encodings with the JSON but get similar errors whichever way I try. I also tried removing the '@' but then run into problems because the strings are not enclosed in quotes so the parser exceptions again.

neontapir
  • 4,698
  • 3
  • 37
  • 52
Chris B
  • 5,311
  • 11
  • 45
  • 57
  • 2
    Are you sure what you've posted is valid PSON as per that link? It says _"PSON does not differ from JSON in its representation of objects, arrays, numbers, booleans, and null values"_ and _"A PSON string... must start and end with " characters"_. What you've posted doesn't match either of those. – James Thorpe May 11 '15 at 14:28
  • Well that would explain why it doesn't work ;-) I had missed the bit about the quotes. Maybe I need to go back to the file source and get more information about how the file is supposed to be used. – Chris B May 11 '15 at 14:43
  • It's not just the quotes - the fact it's got `@` signs in there, no commas between any of the values, `=` instead of `:` etc etc – James Thorpe May 11 '15 at 14:50
  • @JamesThorpe Yeah - point taken, I'll get more information about the file and it's origins and update the question. Thanks – Chris B May 11 '15 at 15:09
  • 1
    The point of a valid PSON file is that it will de-serialize itself. If you run it in PowerShell as a scriptblock, it will recreate all objects that are in it. – Jan Chrbolka May 12 '15 at 06:33
  • Based on the previous comment, your file is a valid PSON file. – Jan Chrbolka May 12 '15 at 06:37
  • @JanChrbolka You are correct, it turns out the file is intended to be read by a PowerShell script. If you post your response as an answer I'll mark it as the correct one. – Chris B May 12 '15 at 09:27
  • It seems the PSON format mentioned in the link I found was a red herring and it has nothing to do with Puppet. It turns out that the file is just a serialized Powershell object. – Chris B May 12 '15 at 09:29

1 Answers1

2

I think that there are two different meanings of PSON here.

  1. PSON in relation to PowerShell, refers to PowerShell Object Notation. This is a format used to store PowerShell data objects in a file. One useful feature of a PSON file is that it can de-serialize itself when executed in PowerShell.

    For more information see this question: Save hash table in PowerShell object notation (PSON)

    The file in you question, is a valid PowerShell PSON file.

  2. The other PSON seems to be Protocol JSON . "A super efficient binary serialization format for JSON" This, I know nothing about.

Community
  • 1
  • 1
Jan Chrbolka
  • 4,184
  • 2
  • 29
  • 38
  • yes, this file appears to be #1 - a PowerShell object so it is never going to parse as JSON. Thanks – Chris B May 14 '15 at 16:14