Source file src/encoding/json/jsontext/export.go

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

View as plain text