// Convert given marshal struct or map value to toml tree
423581
func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, error) {
	if mtype.Kind() == reflect.Ptr {
		return e.valueToTree(mtype.Elem(), mval.Elem())
	}
	tval := e.nextTree()
	switch mtype.Kind() {
	case reflect.Struct:
		for i := 0; i < mtype.NumField(); i++ {
			mtypef, mvalf := mtype.Field(i), mval.Field(i)
			opts := tomlOptions(mtypef, e.annotation)
			if opts.include && (!opts.omitempty || !isZero(mvalf)) {
				val, err := e.valueToToml(mtypef.Type, mvalf)
				if err != nil {
					return nil, err
				}

				tval.SetWithOptions(opts.name, SetOptions{
					Comment:   opts.comment,
					Commented: opts.commented,
					Multiline: opts.multiline,
				}, val)
			}
		}
	case reflect.Map:
		keys := mval.MapKeys()
		if e.order == OrderPreserve && len(keys) > 0 {
			// Sorting []reflect.Value is not straight forward.
			//
			// OrderPreserve will support deterministic results when string is used
			// as the key to maps.
			typ := keys[0].Type()
			kind := keys[0].Kind()
			if kind == reflect.String {
				ikeys := make([]string, len(keys))
				for i := range keys {
					ikeys[i] = keys[i].Interface().(string)
				}
				sort.Strings(ikeys)
				for i := range ikeys {
					keys[i] = reflect.ValueOf(ikeys[i]).Convert(typ)
				}
			}
		}
		for _, key := range keys {
			mvalf := mval.MapIndex(key)
			val, err := e.valueToToml(mtype.Elem(), mvalf)
			if err != nil {
				return nil, err
			}
			if e.quoteMapKeys {
				keyStr, err := tomlValueStringRepresentation(key.String(), "", e.arraysOneElementPerLine)
				if err != nil {
					return nil, err
				}
				tval.SetPath([]string{keyStr}, val)
			} else {
				tval.Set(key.String(), val)
			}
		}
	}
	return tval, nil
}
219074
func (b *FooJSONBuilder) Marshal(orig *Foo) ([]byte, error) {
	ret, err := b.Convert(orig)
	if err != nil {
		return nil, err
	}
	return json.Marshal(ret)
}
613819
func (m *Marshaller) Marshal(obj interface{}) ([]byte, error) {
	var buf bytes.Buffer
	if err := m.Encode(obj, &buf); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}
1311329
func structToMap(v reflect.Value) map[string]interface{} {
	out := make(map[string]interface{})
	t := v.Type()
	for i := 0; i < t.NumField(); i++ {
		field := t.Field(i)
		if field.PkgPath != "" {
			// unexported field. skip.
			continue
		}
		out[field.Name] = v.FieldByName(field.Name).Interface()
	}
	return out
}
1021238
func (e *Encoder) marshal(val reflect.Value, child bool) (cols []string, ok bool) {
	var s string
	switch val.Kind() {
	case reflect.Ptr:
		// for maps and slices, check that they are of supported types
		ok = supportedBaseKind(val)
		if !ok {
			return nil, false
		}
		vv := reflect.Indirect(val)
		switch vv.Kind() {
		case reflect.Invalid:
			// do nothing
		default:
			return e.marshal(vv, child)
		}
	case reflect.Struct:
		return e.marshalStruct(val.Interface(), true)
	case reflect.Map:
		s, ok = e.marshalMap(val, child)
		if !ok {
			return nil, false
		}
	case reflect.Array, reflect.Slice:
		s, ok = e.marshalSlice(val, child)
		if !ok {
			return nil, false
		}
	default:
		var ok bool
		s, ok = e.stringify(val, child)
		if !ok {
			return nil, false
		}
	}
	return append(cols, s), true
}
259150
func (b *InnerJSONBuilder) Marshal(orig *Inner) ([]byte, error) {
	ret, err := b.Convert(orig)
	if err != nil {
		return nil, err
	}
	return json.Marshal(ret)
}
257745
func (s *Serializer) Marshal(v interface{}) ([]byte, error) {
	pb, ok := v.(proto.Message)
	if !ok {
		return nil, constants.ErrWrongValueType
	}
	return proto.Marshal(pb)
}
925855
func (b *HogeJSONBuilder) Marshal(orig *Hoge) ([]byte, error) {
	ret, err := b.Convert(orig)
	if err != nil {
		return nil, err
	}
	return json.Marshal(ret)
}
728976
func Marshal(v interface{}) ([]byte, error) {
	rv := reflect.ValueOf(v)
	for rv.Kind() == reflect.Ptr {
		rv = rv.Elem()
	}
	pv := reflect.New(rv.Type())
	pv.Elem().Set(rv)

	if err := applyDefault(pv, true); err != nil {
		return nil, err
	}
	return toml.Marshal(pv.Interface())
}
1115025
func Marshal(v interface{}) ([]byte, error) {
	w := bytes.NewBuffer(nil)
	err := MarshalTo(w, v)
	return w.Bytes(), err
}
90283
func (t *Function) MarshalJSON() ([]byte, error) {
	repr := map[string]interface{}{
		"type": t.typeMarker(),
	}
	if len(t.args) > 0 {
		repr["args"] = t.args
	}
	if t.result != nil {
		repr["result"] = t.result
	}
	return json.Marshal(repr)
}
587709
func (n *Node) MarshalJSON() ([]byte, error) {
	out, err := convertToJSONIsh(n.obj)
	if err != nil {
		return nil, err
	}

	return json.Marshal(out)
}
395048
func Marshal(v interface{}) ([]byte, error) {
	if ImplementsPreJSONMarshaler(v) {
		err := v.(PreJSONMarshaler).PreMarshalJSON()
		if err != nil {
			return nil, err
		}
	}

	return json.Marshal(v)
}
446465
func (t *Tr) toBytes() []byte {
	buf, err := t.MarshalMsg(nil)
	if err != nil {
		panic(`unable to encode tree`)
	}

	return buf
}
1093980
func (sj *SerializerJSON) Marshal(message proto.Message) ([]byte, error) {
	if message == nil {
		return []byte("null"), nil
	}
	var buf bytes.Buffer
	if err := DefaultMarshaler.Marshal(&buf, message); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}
942150
func (f *QueryStringFilter) MarshalJSON() ([]byte, error) {
	root := map[string]interface{}{
		"query_string": map[string]interface{}{
			"query":            f.Query,
			"analyze_wildcard": f.AnalyzeWildcard,
		},
	}

	return json.Marshal(root)
}
991843
func (mmap MarshalMap) MarshalMap(key string) (MarshalMap, error) {
	val, exists := mmap.Value(key)
	if !exists {
		return nil, fmt.Errorf(`"%s" is not set`, key)
	}

	return ConvertToMarshalMap(val, nil)
}