package inout

import (
	"encoding/json"
	"fmt"
)

func ObjToJson(obj any) string {
	data, err := json.Marshal(obj)
	if err != nil {
		return fmt.Sprintf("Eroare: %+v\n", err)

	}
	return string(data)
}

func ObjToFile(filename string, obj any, beauty bool) error {
	if beauty {
		data, err := json.MarshalIndent(obj, "", "  ")
		if err != nil {
			return err
		}

		return SaveObjToFile(filename, data)
	}

	data, err := json.Marshal(obj)

	if err != nil {
		return err

	}
	return SaveObjToFile(filename, data)
}

func FileToObj(filename string, obj any) error {
	data, err := ReadFile(filename)
	if err != nil {
		return err
	}
	return json.Unmarshal(data, obj)
}
