Code Monkey home page Code Monkey logo

Comments (5)

gabriel-samfira avatar gabriel-samfira commented on September 14, 2024

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.

gaelcolas avatar gaelcolas commented on September 14, 2024

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.

gabriel-samfira avatar gabriel-samfira commented on September 14, 2024

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.

gaelcolas avatar gaelcolas commented on September 14, 2024

Ah perfect, thanks.
I did not spot that option ;)

from powershell-yaml.

gabriel-samfira avatar gabriel-samfira commented on September 14, 2024

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)

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.