export.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2023 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package jsontext
  5. import (
  6. "io"
  7. "github.com/go-json-experiment/json/internal"
  8. )
  9. // Internal is for internal use only.
  10. // This is exempt from the Go compatibility agreement.
  11. var Internal exporter
  12. type exporter struct{}
  13. // Export exposes internal functionality from "jsontext" to "json".
  14. // This cannot be dynamically called by other packages since
  15. // they cannot obtain a reference to the internal.AllowInternalUse value.
  16. func (exporter) Export(p *internal.NotForPublicUse) export {
  17. if p != &internal.AllowInternalUse {
  18. panic("unauthorized call to Export")
  19. }
  20. return export{}
  21. }
  22. // The export type exposes functionality to packages with visibility to
  23. // the internal.AllowInternalUse variable. The "json" package uses this
  24. // to modify low-level state in the Encoder and Decoder types.
  25. // It mutates the state directly instead of calling ReadToken or WriteToken
  26. // since this is more performant. The public APIs need to track state to ensure
  27. // that users are constructing a valid JSON value, but the "json" implementation
  28. // guarantees that it emits valid JSON by the structure of the code itself.
  29. type export struct{}
  30. // Encoder returns a pointer to the underlying encoderState.
  31. func (export) Encoder(e *Encoder) *encoderState { return &e.s }
  32. // Decoder returns a pointer to the underlying decoderState.
  33. func (export) Decoder(d *Decoder) *decoderState { return &d.s }
  34. func (export) GetBufferedEncoder(o ...Options) *Encoder {
  35. return getBufferedEncoder(o...)
  36. }
  37. func (export) PutBufferedEncoder(e *Encoder) {
  38. putBufferedEncoder(e)
  39. }
  40. func (export) GetStreamingEncoder(w io.Writer, o ...Options) *Encoder {
  41. return getStreamingEncoder(w, o...)
  42. }
  43. func (export) PutStreamingEncoder(e *Encoder) {
  44. putStreamingEncoder(e)
  45. }
  46. func (export) GetBufferedDecoder(b []byte, o ...Options) *Decoder {
  47. return getBufferedDecoder(b, o...)
  48. }
  49. func (export) PutBufferedDecoder(d *Decoder) {
  50. putBufferedDecoder(d)
  51. }
  52. func (export) GetStreamingDecoder(r io.Reader, o ...Options) *Decoder {
  53. return getStreamingDecoder(r, o...)
  54. }
  55. func (export) PutStreamingDecoder(d *Decoder) {
  56. putStreamingDecoder(d)
  57. }
  58. func (export) IsIOError(err error) bool {
  59. _, ok := err.(*ioError)
  60. return ok
  61. }