// ToJSON transforms an YAML input and transforms it to JSON.
//
// Originally inspired by https://github.com/bronze1man/yaml2json.
541464
func ToJSON(p []byte, opts ToJSONOptions) ([]byte, error) {
	var yamlData interface{}
	if err := yaml.Unmarshal(p, &yamlData); err != nil {
		return nil, err
	}
	jsonData, err := toJSON(yamlData)
	if err != nil {
		return nil, err
	}
	if opts.Pretty {
		if opts.Indent != "" {
			return json.MarshalIndent(jsonData, "", opts.Indent)
		}
		return json.MarshalIndent(jsonData, "", defaultIndent)
	}
	return json.Marshal(jsonData)
}
603008
func ToJSON(caddyfile []byte) ([]byte, error) {
	var j EncodedCaddyfile

	serverBlocks, err := Parse(filename, bytes.NewReader(caddyfile), nil)
	if err != nil {
		return nil, err
	}

	for _, sb := range serverBlocks {
		block := EncodedServerBlock{
			Keys: sb.Keys,
			Body: [][]interface{}{},
		}

		// Extract directives deterministically by sorting them
		var directives = make([]string, len(sb.Tokens))
		for dir := range sb.Tokens {
			directives = append(directives, dir)
		}
		sort.Strings(directives)

		// Convert each directive's tokens into our JSON structure
		for _, dir := range directives {
			disp := NewDispenserTokens(filename, sb.Tokens[dir])
			for disp.Next() {
				block.Body = append(block.Body, constructLine(&disp))
			}
		}

		// tack this block onto the end of the list
		j = append(j, block)
	}

	result, err := json.Marshal(j)
	if err != nil {
		return nil, err
	}

	return result, nil
}
1266597
func (ml *MessageList) ToJSON() (json string) {
	tmp := make([]string, len(ml.list))
	for i, msg := range ml.list {
		tmp[i] = strconv.Quote(msg)
	}
	return "[" + strings.Join(tmp, ",") + "]"
}
657480
func (o DepAnalysisOutput) JSON(w io.Writer) error {
	o.sort()
	return JSON(w, o)
}
277367
func (f *Feed) ToJSON() (string, error) {
	j := &JSON{f}
	return j.ToJSON()
}
199208
func ConvertXMLToJSON(xml string) string {
	xmlReader := strings.NewReader(xml)
	json, _ := xj.Convert(xmlReader)
	return json.String()
}
831589
func (v Values) YAML() (string, error) {
	b, err := yaml.Marshal(v)
	return string(b), err
}
429468
func (section *Section) ToJSON() ([]byte, error) {
	data := section.ToMap()
	return json.Marshal(data)
}
930199
func JSON(w io.Writer, x interface{}) error {
	encoder := json.NewEncoder(w)
	encoder.SetIndent("", "  ")
	return encoder.Encode(x)
}
1148361
func (t Version) JSON() (output string, err error) {
	var raw []byte
	ver := Get()
	if raw, err = json.MarshalIndent(ver, "", "\t"); err != nil {
		return
	}
	return string(raw), nil
}
294033
func (m mirrorMessage) JSON() string {
	m.Status = "success"
	mirrorMessageBytes, e := json.MarshalIndent(m, "", " ")
	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")

	return string(mirrorMessageBytes)
}
1178663
func (this *Response) json(i interface{}) string {
	if len(this.Get("Content-Type")) == 0 {
		this.ContentType("application/json")
	}
	if len(this.Charset) == 0 {
		this.Charset = "utf-8"
	}
	var b []byte
	var e error
	if this.app.Get("env") == "development" {
		b, e = json.MarshalIndent(i, "", this.app.Get("json spaces"))
	} else {
		b, e = json.Marshal(i)
	}
	// If there was an error return an empty string.
	if e != nil {
		return ""
	}
	return string(b)
}
1246707
func (d *Dataset) JSON() (*Exportable, error) {
	back := d.Dict()

	b, err := json.Marshal(back)
	if err != nil {
		return nil, err
	}
	return newExportableFromBytes(b), nil
}
657096
func (g *Container) EncodeJSON(encodeOpts ...EncodeOpt) []byte {
	var b bytes.Buffer
	encoder := json.NewEncoder(&b)
	encoder.SetEscapeHTML(false) // Do not escape by default.
	for _, opt := range encodeOpts {
		opt(encoder)
	}
	if err := encoder.Encode(g.object); err != nil {
		return []byte("{}")
	}
	result := b.Bytes()
	if len(result) > 0 {
		result = result[:len(result)-1]
	}
	return result
}
398804
func ToFields(value interface{}) logrus.Fields {
	v := reflect.ValueOf(value)
	var hash map[string]interface{}
	var ok bool

	switch v.Kind() {
	case reflect.Struct:
		hash = structs.Map(value)
	case reflect.Map:
		hash, ok = value.(map[string]interface{})
		if !ok {
			panic("ToFields(): map kind must be of type map[string]interface{}")
		}
	default:
		panic("ToFields(): value must be of kind struct or map")
	}

	result := make(logrus.Fields, len(hash))
	for key, value := range hash {
		// Convert values the JSON marshaller doesn't know how to marshal
		v := reflect.ValueOf(value)
		switch v.Kind() {
		case reflect.Func:
			value = fmt.Sprintf("%+v", value)
		case reflect.Struct, reflect.Map:
			value = ToFields(value)
		}

		// Ensure the key is a string. convert it if not
		v = reflect.ValueOf(key)
		if v.Kind() != reflect.String {
			key = fmt.Sprintf("%+v", key)
		}
		result[key] = value
	}
	return result
}
863638
func (e Response) JSON(decoder interface{}) (*interface{}, error) {
	defer e.Res.Body.Close()
	resp := &decoder
	err := json.NewDecoder(e.Res.Body).Decode(resp)
	return resp, err
}
1071938
func (t *Table) ToJSON(w io.Writer) {
	t.mu.RLock()
	defer t.mu.RUnlock()
	w.Write([]byte(`{ "bindings": [`))

	if len(t.AvailableBindings) > 0 {
		w.Write([]byte(`"`))
		w.Write([]byte(strings.Join(t.AvailableBindings, `", "`)))
		w.Write([]byte(`"`))
	}

	w.Write([]byte(`], "rows": [`))

	rc := len(t.Data)
	for _, r := range t.Data {
		if len(r) > 0 {
			w.Write([]byte(`{ `))

			cc := len(t.AvailableBindings)
			for _, k := range t.AvailableBindings {
				if k != "" {
					c := r[k]
					w.Write([]byte(`"`))
					w.Write([]byte(k))
					w.Write([]byte(`": {"`))

					if c.S != nil {
						w.Write([]byte(`string": "`))
						w.Write([]byte(strings.Replace(*c.S, `"`, `\"`, -1)))
					} else if c.N != nil {
						w.Write([]byte(`node": "`))
						w.Write([]byte(strings.Replace(c.N.String(), `"`, `\"`, -1)))
					} else if c.P != nil {
						w.Write([]byte(`pred": "`))
						w.Write([]byte(strings.Replace(c.P.String(), `"`, `\"`, -1)))

					} else if c.L != nil {
						w.Write([]byte(`lit": "`))
						w.Write([]byte(strings.Replace(c.L.String(), `"`, `\"`, -1)))

					} else if c.T != nil {
						w.Write([]byte(`anchor": "`))
						w.Write([]byte(strings.Replace(c.T.Format(time.RFC3339Nano), `"`, `\"`, -1)))
					}

					w.Write([]byte(`"}`))
					if cc > 1 {
						w.Write([]byte(`,`))
					}
					w.Write([]byte(` `))
				}
				cc--
			}
			w.Write([]byte(` }`))
			if rc > 1 {
				w.Write([]byte(`, `))
			}
		}
		rc--
	}

	w.Write([]byte(`] }`))
}