...

Source file src/github.com/launchdarkly/go-sdk-common/v3/ldcontext/context_marshaling.go

Documentation: github.com/launchdarkly/go-sdk-common/v3/ldcontext

     1  package ldcontext
     2  
     3  import (
     4  	"github.com/launchdarkly/go-sdk-common/v3/ldattr"
     5  
     6  	"github.com/launchdarkly/go-jsonstream/v3/jwriter"
     7  )
     8  
     9  // JSONString returns the JSON representation of the Context.
    10  //
    11  // This is equivalent to calling [Context.MarshalJSON] and converting the result to a string.
    12  // An invalid Context cannot be represented in JSON and produces an empty string.
    13  func (c Context) JSONString() string {
    14  	bytes, _ := c.MarshalJSON()
    15  	return string(bytes)
    16  }
    17  
    18  // MarshalJSON provides JSON serialization for Context when using [encoding/json.MarshalJSON].
    19  //
    20  // LaunchDarkly's JSON schema for contexts is standardized across SDKs. There are two output formats,
    21  // depending on whether it is a single context or a multi-context. Unlike the unmarshaler,
    22  // the marshaler never uses the old-style user context schema from older SDKs.
    23  //
    24  // If the Context is invalid (that is, it has a non-nil [Context.Err]) then marshaling fails with the
    25  // same error.
    26  func (c Context) MarshalJSON() ([]byte, error) {
    27  	w := jwriter.NewWriter()
    28  	ContextSerialization.MarshalToJSONWriter(&w, &c)
    29  	return w.Bytes(), w.Error()
    30  }
    31  
    32  func (c *Context) writeToJSONWriterInternalSingle(w *jwriter.Writer, withinKind Kind, usingEventFormat bool) {
    33  	obj := w.Object()
    34  	if withinKind == "" {
    35  		obj.Name(ldattr.KindAttr).String(string(c.kind))
    36  	}
    37  
    38  	obj.Name(ldattr.KeyAttr).String(c.key)
    39  	if c.name.IsDefined() {
    40  		obj.Name(ldattr.NameAttr).String(c.name.StringValue())
    41  	}
    42  	keys := make([]string, 0, 50) // arbitrary size to preallocate on stack
    43  	for _, k := range c.attributes.Keys(keys) {
    44  		obj.Name(k)
    45  		c.attributes.Get(k).WriteToJSONWriter(w)
    46  	}
    47  	if c.anonymous {
    48  		obj.Name(ldattr.AnonymousAttr).Bool(true)
    49  	}
    50  
    51  	needMeta := len(c.privateAttrs) != 0
    52  	if needMeta {
    53  		metaJSON := obj.Name(jsonPropMeta).Object()
    54  		if len(c.privateAttrs) != 0 {
    55  			name := jsonPropPrivate
    56  			if usingEventFormat {
    57  				name = jsonPropRedacted
    58  			}
    59  			privateAttrsJSON := metaJSON.Name(name).Array()
    60  			for _, a := range c.privateAttrs {
    61  				privateAttrsJSON.String(a.String())
    62  			}
    63  			privateAttrsJSON.End()
    64  		}
    65  		metaJSON.End()
    66  	}
    67  
    68  	obj.End()
    69  }
    70  
    71  func (c Context) writeToJSONWriterInternalMulti(w *jwriter.Writer, usingEventFormat bool) {
    72  	obj := w.Object()
    73  	obj.Name(ldattr.KindAttr).String(string(MultiKind))
    74  
    75  	for _, mc := range c.multiContexts {
    76  		obj.Name(string(mc.Kind()))
    77  		mc.writeToJSONWriterInternalSingle(w, mc.Kind(), usingEventFormat)
    78  	}
    79  
    80  	obj.End()
    81  }
    82  

View as plain text