json.scm 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. ;;; guile-openai --- An OpenAI API client for Guile
  2. ;;; Copyright © 2023 Andrew Whatson <whatson@tailcall.au>
  3. ;;;
  4. ;;; This file is part of guile-openai.
  5. ;;;
  6. ;;; guile-openai is free software: you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU Affero General Public License as
  8. ;;; published by the Free Software Foundation, either version 3 of the
  9. ;;; License, or (at your option) any later version.
  10. ;;;
  11. ;;; guile-openai is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;; Affero General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU Affero General Public
  17. ;;; License along with guile-openai. If not, see
  18. ;;; <https://www.gnu.org/licenses/>.
  19. (define-module (openai utils json)
  20. #:use-module (json)
  21. #:export (write-pretty-json))
  22. (define* (write-pretty-json str #:optional (port (current-output-port)))
  23. (or (and=> (false-if-exception
  24. (json-string->scm str #:ordered #t))
  25. (lambda (scm)
  26. (scm->json scm port #:pretty #t)
  27. #t))
  28. (display str port))
  29. (newline port))