Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON parser fails to parse JSON generated from HCL #141

Open
vetinari opened this issue Jul 29, 2016 · 0 comments
Open

JSON parser fails to parse JSON generated from HCL #141

vetinari opened this issue Jul 29, 2016 · 0 comments
Labels
v1 Relates to the v1 line of releases

Comments

@vetinari
Copy link

The following code fails:

package main

import (
    "encoding/json"
    "reflect"

    "github.com/hashicorp/hcl"
)

var hclData = []byte(`match "first" {
    type = "test"
    value = "first"
}
match "second" {
    type = "test"
    value = "second"
    match "inner" {
        type = "test"
        value = "third"
    }
}
`)

type Data struct {
    Matches []*Match `json:"match" hcl:"match"`
}
type Match struct {
    Type    string   `json:"type" hcl:"type"`
    Value   string   `json:"value" hcl:"value"`
    Matches []*Match `json:"match,omitempty" hcl:"match"`
}

func main() {
    var d, e *Data
    err := hcl.Unmarshal(hclData, &d)
    if err != nil {
        panic("failed to unmarshal hclData: " + err.Error())
    }
    jData, err := json.MarshalIndent(d, "", "  ")
    if err != nil {
        panic("failed to marshal into jData: " + err.Error())
    }

    err = hcl.Decode(&e, string(jData))
    if err != nil {
        panic("failed to parse jData: " + err.Error())
    }
    if !reflect.DeepEqual(d, e) {
        jd, _ := json.Marshal(d)
        je, _ := json.Marshal(e)
        panic("parsed data differs: \n" + string(jd) + "\n" + string(je) + "\n")
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v1 Relates to the v1 line of releases
Projects
None yet
Development

No branches or pull requests

2 participants