Code Monkey home page Code Monkey logo

Comments (7)

gabriel-samfira avatar gabriel-samfira commented on August 10, 2024

Hmm. Does this happend when you pass the single value array as a parameter, instead of piping it to the commandlet?

from powershell-yaml.

gabriel-samfira avatar gabriel-samfira commented on August 10, 2024

If you're piping the single valued object in, then I am afraid there is not much that can be done. Unfortunately, the BEGIN section of "advanced" powershell functiond does not have access to the type of the object being piped in. So when you pipe something in, its the PROCESS block that takes care of iterating through the array.

Note, this behavior only happens with arrays. Any other object should be fine. The ConvertTo-Json commandlet behaves the same.

PS /tmp/powershell-yaml> @("this is a test") | ConvertTo-Json                             
"this is a test"

And here is ConvertTo-Yaml:

PS /tmp/powershell-yaml> @("this is a test") | ConvertTo-Yaml                             
this is a test
...

Doing the same thing, but passing as a parameter:

PS /tmp/powershell-yaml> ConvertTo-Json @("this is a test")                               
[
    "this is a test"
]

And ConvertTo-Yaml:

PS /tmp/powershell-yaml> ConvertTo-Yaml @("this is a test")                               
- this is a test

Hope this helps out.

from powershell-yaml.

yyudhi avatar yyudhi commented on August 10, 2024

Thank you for your support. Please try below and investigate the behaviour:
`
$obj = New-Object -TypeName System.Collections.Hashtable
$obj.Remove("myarray_attribute")
$obj.myarray_attribute = New-Object -TypeName System.Collections.ArrayList
$obj.myarray_attribute.Add("myarrayvalue")

#Let's convert to JSON, it should show as Array
ConvertTo-Json $obj

#Now use one of these command below, attribute no longer an Array
$obj | ConvertTo-Yaml
ConvertTo-Yaml $obj

#Let's check the JSON format again, it is now become string attribute
ConvertTo-Json $obj`

from powershell-yaml.

gabriel-samfira avatar gabriel-samfira commented on August 10, 2024

I may be missing something here, but here is what I get when running your example:

PS /tmp/powershell-yaml> $obj = New-Object -TypeName System.Collections.Hashtable                                                                                                    
PS /tmp/powershell-yaml> $obj.Remove("myarray_attribute")                                                                                                                            
PS /tmp/powershell-yaml> $obj.myarray_attribute = New-Object -TypeName System.Collections.ArrayList                                                                                  
PS /tmp/powershell-yaml> $obj.myarray_attribute.Add("myarrayvalue")                                                                                                                  
0
PS /tmp/powershell-yaml>                                                                                                                                                             
PS /tmp/powershell-yaml> #Let's convert to JSON, it should show as Array                                                                                                             
PS /tmp/powershell-yaml> ConvertTo-Json $obj                                                                                                                                         
{
    "myarray_attribute":  [
                              "myarrayvalue"
                          ]
}
PS /tmp/powershell-yaml>                                                                                                                                                             
PS /tmp/powershell-yaml> #Now use one of these command below, attribute no longer an Array                                                                                           
PS /tmp/powershell-yaml> $obj | ConvertTo-Yaml                                                                                                                                       
myarray_attribute:
- myarrayvalue

PS /tmp/powershell-yaml> ConvertTo-Yaml $obj                                                                                                                                         
myarray_attribute:
- myarrayvalue

PS /tmp/powershell-yaml>                                                                                                                                                             
PS /tmp/powershell-yaml> #Let's check the JSON format again, it is now become string attribute                                                                                       
PS /tmp/powershell-yaml> ConvertTo-Json $obj`                                                                                                                                        
"System.Collections.Hashtable`"

So it should work as expected when converting to Yaml. However, when unmarshaling you are indeed correct! there is a bug with single value arrays! pushed a fix.

Can you test it out and tell me if it fixed things for you?

from powershell-yaml.

gabriel-samfira avatar gabriel-samfira commented on August 10, 2024

And here is the output after the fix:

PS /tmp/powershell-yaml> $obj                                                                                                                                                        

Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
myarray_attribute              {myarrayvalue}                                                                                                                                       


PS /tmp/powershell-yaml> $obj | ConvertTo-Yaml                                                                                                                                       
myarray_attribute:
- myarrayvalue

PS /tmp/powershell-yaml> $obj | ConvertTo-yaml| ConvertFrom-Yaml                                                                                                                     

Name                           Value                                                                                                                                                
----                           -----                                                                                                                                                
myarray_attribute              {myarrayvalue}       

Make sure you are using the latest master from github.

from powershell-yaml.

gabriel-samfira avatar gabriel-samfira commented on August 10, 2024

Let me know if this worked for you, so I can close this bug.

Thanks!

from powershell-yaml.

yyudhi avatar yyudhi commented on August 10, 2024

from powershell-yaml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.