You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: json.go
    path of file: ./repos/uptrace/pkg/httputil
    
    the code of the file until where you have to start completion: package httputil

import (
	"bytes"
	"io"
	"net"
	"net/http"
	"os"
	"syscall"

	"github.com/segmentio/encoding/json"
	"github.com/uptrace/bunrouter"
	"github.com/uptrace/uptrace/pkg/httperror"
)

func JSON(w http.ResponseWri
    
func JSON(w http.ResponseWriter, res any) error {
	if res == nil {
		return nil
	}

	w.Header().Set("Content-Type", "application/json")

	switch res := res.(type) {
	case string:
		_, _ = io.WriteString(w, res)
		return nil
	case []byte:
		_, _ = w.Write(res)
		return nil
	}

	enc := newEncoder(w)
	if err := enc.Encode(res); err != nil {
		return err
	}

	return nil
}