...
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
10
11
12
13 func (c Context) JSONString() string {
14 bytes, _ := c.MarshalJSON()
15 return string(bytes)
16 }
17
18
19
20
21
22
23
24
25
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)
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