Comments (5)
Hi @gaelcolas
The JsonCompatible
option, simply uses the json
emitter that ships with YamlDotNet
, which does not chain any other emitter class. It is pretty self contained.
Would you mind posting an example of the yaml you are trying to serialize? It would make it easier to understand where the issue is.
I tried the following:
PS /home/gabriel> $a = @{"true"= $true; "false"= $false}
PS /home/gabriel> ConvertTo-Yaml -JsonCompatible $a
{"true": true, "false": false}
PS /home/gabriel> ConvertTo-Yaml $a
"true": true
"false": false
PS /home/gabriel> ConvertTo-Yaml $a -Options EmitDefaults
"true": true
"false": false
PS /home/gabriel> ConvertTo-Yaml $a
"true": true
"false": false
from powershell-yaml.
Here's what I'm playing with:
#Requires -Module powershell-yaml
using namespace YamlDotNet.Serialization
class MyClass {
[YamlMemberAttribute(Order = 0)]
[string] $Prop1
[YamlMemberAttribute(Order = 1)]
[bool] $PropTrue = $true
[YamlMemberAttribute(Order = 2)]
[bool] $PropFalse = $false
MyClass() {
$this.Prop1 = "MyClassInstance"
}
[string] ToYaml() {
return ($this | ConvertTo-Yaml -Options EmitDefaults)
}
[string] ToYamlNoDefaults() {
return ($this | ConvertTo-Yaml)
}
[string] ToJSON() {
return ($this | ConvertTo-Yaml -JsonCompatible)
}
[string] ToString() {
return $this.ToYaml()
}
}
"==================================`r`n`r`n"
[MyClass]::new()
"==================================`r`n`r`n"
[MyClass]::new().ToYaml()
"==================================`r`n`r`n"
[MyClass]::new().ToYamlNoDefaults()
"==================================`r`n`r`n"
[MyClass]::new().ToJSON()
and the output is:
==================================
PropTrue PropFalse Prop1
-------- --------- -----
True False MyClassInstance
==================================
Prop1: MyClassInstance
PropTrue: true
PropFalse: false
==================================
Prop1: MyClassInstance
PropTrue: true
==================================
{"Prop1": "MyClassInstance", "PropTrue": true}
So you can see the JSON does not have the property PropFalse
.
Any way I should work around that?
from powershell-yaml.
Ahh yes. Makes more sense now.
Can you try this:
#Requires -Module powershell-yaml
using namespace YamlDotNet.Serialization
class MyClass {
[YamlMemberAttribute(Order = 0)]
[string] $Prop1
[YamlMemberAttribute(Order = 1)]
[bool] $PropTrue = $true
[YamlMemberAttribute(Order = 2)]
[bool] $PropFalse = $false
[YamlMemberAttribute(Order = 3)]
[int] $PropInt = 0
MyClass() {
$this.Prop1 = "MyClassInstance"
}
[string] ToYaml() {
return (ConvertTo-Yaml -Options EmitDefaults $this)
}
[string] ToYamlNoDefaults() {
return (ConvertTo-Yaml $this)
}
[string] ToJSON() {
return (ConvertTo-Yaml -Options JsonCompatible,EmitDefaults $this)
}
[string] ToString() {
return $this.ToYaml()
}
}
The standalone -JsonCompatible
flag is there for compatibility mostly, and predates the -Options
flag. I think it's time for some usability updates. Hopefully I will get some time soon to work on this.
from powershell-yaml.
Ah perfect, thanks.
I did not spot that option ;)
from powershell-yaml.
My pleasure. It is a usability issue at this point. You should be able to use both -JsonCompatible
and -Options EmitDefaults
. I will deprecate the -JsonCompatible
option in the next release, and ultimately remove it.
Thanks for reporting this.
from powershell-yaml.
Related Issues (20)
- Add `Test-Yaml` cmdlet
- Class with PSCustomObject ConvertTo-Yaml fail HOT 3
- 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. HOT 1
- Could not find file '/usr/lib/powershell/ref/System.Private.CoreLib.dll' version 0.4.7 HOT 2
- Convetto-Yaml: Value starting with double quote is being enclosed by single quotes. HOT 5
- mscorlib is not referenced HOT 6
- Cannot find type [YamlDotNet.Core.Parser] HOT 2
- powershell-yaml module is down HOT 1
- Powershell-Yaml incorrectly errors on duplicate key HOT 2
- MethodArgumentConversionInvalidCastArgument HOT 1
- Move add-type to compilation HOT 2
- Found incompatible YAML Document
- Large number being parsed to a scientific notation HOT 2
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.