Comments (7)
Hmm. Does this happend when you pass the single value array as a parameter, instead of piping it to the commandlet?
from powershell-yaml.
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.
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.
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.
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.
Let me know if this worked for you, so I can close this bug.
Thanks!
from powershell-yaml.
from powershell-yaml.
Related Issues (20)
- Add `Test-Yaml` cmdlet
- Class with PSCustomObject ConvertTo-Yaml fail HOT 1
- Copyright is out of date HOT 2
- This Module conflicts with Az.Aks HOT 3
- Fails when used from an Azure Function HOT 4
- an object with property name "keys" HOT 3
- `SerializationOptions` should be defined with `[Flags()]`
- Add `-Ordered` parameter info to documentation HOT 1
- Import-Module error HOT 4
- Yaml output is not respected when exporting Powershell nested classes.
- Could not find file '/usr/lib/powershell/ref/System.Private.CoreLib.dll' version 0.4.7 HOT 1
- Convetto-Yaml: Value starting with double quote is being enclosed by single quotes. HOT 5
- mscorlib is not referenced HOT 2
- Cannot find type [YamlDotNet.Core.Parser]
- powershell-yaml module is down HOT 1
- Powershell-Yaml incorrectly errors on duplicate key HOT 2
- MethodArgumentConversionInvalidCastArgument HOT 1
- Move add-type to compilation
- Found incompatible YAML Document
- Large number being parsed to a scientific notation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershell-yaml.