yaml2json

package module
v0.0.0-...-f6f64b7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2015 License: MIT Imports: 4 Imported by: 13

README

Codeship Status API Documentation MIT License

Convert YAML to JSON.

Installation

go get -u github.com/peter-edge/go-yaml2json

Import

import (
    "github.com/peter-edge/go-yaml2json"
)

Inspired by https://github.com/bronze1man/yaml2json, a command line tool with the same effect.

import (
	"encoding/json"
	"io/ioutil"
	"os"

	"github.com/peter-edge/go-yaml2json"
)

func ReadYamlToJson(yamlFilePath string) (interface{}, error) {
	yamlFile, err := os.Open(yamlFilePath)
	if err != nil {
		return nil, err
	}
	defer yamlFile.Close()
	yamlFileData, err := ioutil.ReadAll(yamlFile)
	if err != nil {
		return nil, err
	}
	jsonData, err := yaml2json.Convert(yamlFileData)
	if err != nil {
		return nil, err
	}
	var obj interface{}
	err = json.Unmarshal(jsonData, &obj)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

Usage

func Convert
func Convert(input []byte) ([]byte, error)

Documentation

Overview

Convert YAML to JSON.

Inspired by https://github.com/bronze1man/yaml2json, a command line tool with the same effect.

import (
	"encoding/json"
	"io/ioutil"
	"os"

	"github.com/peter-edge/go-yaml2json"
)

func ReadYamlToJson(yamlFilePath string) (interface{}, error) {
	yamlFile, err := os.Open(yamlFilePath)
	if err != nil {
		return nil, err
	}
	defer yamlFile.Close()
	yamlFileData, err := ioutil.ReadAll(yamlFile)
	if err != nil {
		return nil, err
	}
	jsonData, err := yaml2json.Convert(yamlFileData)
	if err != nil {
		return nil, err
	}
	var obj interface{}
	err = json.Unmarshal(jsonData, &obj)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(input []byte) ([]byte, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL