...

Source file src/github.com/launchdarkly/go-sdk-common/v3/ldvalue/value_json_easyjson_conversion.go

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

     1  //go:build launchdarkly_easyjson
     2  
     3  package ldvalue
     4  
     5  import (
     6  	"github.com/mailru/easyjson/jlexer"
     7  	ej_jwriter "github.com/mailru/easyjson/jwriter"
     8  )
     9  
    10  // This conditionally-compiled file provides custom marshal/unmarshal functions for ldvalue types
    11  // in EasyJSON.
    12  //
    13  // EasyJSON's code generator does recognize the same MarshalJSON and UnmarshalJSON methods used by
    14  // encoding/json, and will call them if present. But this mechanism is inefficient: when marshaling
    15  // it requires the allocation of intermediate byte slices, and when unmarshaling it causes the
    16  // JSON object to be parsed twice. It is preferable to have our marshal/unmarshal methods write to
    17  // and read from the EasyJSON Writer/Lexer directly. Also, since deserialization is a high-traffic
    18  // path in some LaunchDarkly code on the service side, the extra overhead of the go-jsonstream
    19  // abstraction is undesirable.
    20  
    21  // For more information, see: https://github.com/launchdarkly/go-jsonstream/v3
    22  
    23  func (v Value) MarshalEasyJSON(writer *ej_jwriter.Writer) {
    24  	switch v.valueType {
    25  	case NullType:
    26  		writer.Raw(nullAsJSONBytes, nil)
    27  	case BoolType:
    28  		writer.Bool(v.boolValue)
    29  	case NumberType:
    30  		writer.Float64(v.numberValue)
    31  	case StringType:
    32  		writer.String(v.stringValue)
    33  	case ArrayType:
    34  		v.arrayValue.MarshalEasyJSON(writer)
    35  	case ObjectType:
    36  		v.objectValue.MarshalEasyJSON(writer)
    37  	case RawType:
    38  		writer.Raw(v.rawValue, nil)
    39  	}
    40  }
    41  
    42  func (v *Value) UnmarshalEasyJSON(lexer *jlexer.Lexer) {
    43  	if lexer.IsDelim('[') {
    44  		var va ValueArray
    45  		va.UnmarshalEasyJSON(lexer)
    46  		*v = Value{valueType: ArrayType, arrayValue: va}
    47  	} else if lexer.IsDelim('{') {
    48  		var vm ValueMap
    49  		vm.UnmarshalEasyJSON(lexer)
    50  		*v = Value{valueType: ObjectType, objectValue: vm}
    51  	} else {
    52  		*v = CopyArbitraryValue(lexer.Interface())
    53  	}
    54  }
    55  
    56  func (a ValueArray) MarshalEasyJSON(writer *ej_jwriter.Writer) {
    57  	if a.data == nil {
    58  		writer.Raw(nullAsJSONBytes, nil)
    59  		return
    60  	}
    61  	writer.RawByte('[')
    62  	for i, value := range a.data {
    63  		if i != 0 {
    64  			writer.RawByte(',')
    65  		}
    66  		value.MarshalEasyJSON(writer)
    67  	}
    68  	writer.RawByte(']')
    69  }
    70  
    71  func (a *ValueArray) UnmarshalEasyJSON(lexer *jlexer.Lexer) {
    72  	if lexer.IsNull() {
    73  		lexer.Null()
    74  		*a = ValueArray{}
    75  		return
    76  	}
    77  	lexer.Delim('[')
    78  	a.data = make([]Value, 0, 4)
    79  	for !lexer.IsDelim(']') {
    80  		var value Value
    81  		value.UnmarshalEasyJSON(lexer)
    82  		a.data = append(a.data, value)
    83  		lexer.WantComma()
    84  	}
    85  	lexer.Delim(']')
    86  }
    87  
    88  func (m ValueMap) MarshalEasyJSON(writer *ej_jwriter.Writer) {
    89  	if m.data == nil {
    90  		writer.Raw(nullAsJSONBytes, nil) //COVERAGE: EasyJSON optimizations may prevent us from reaching this line
    91  		return
    92  	}
    93  	writer.RawByte('{')
    94  	first := true
    95  	for key, value := range m.data {
    96  		if !first {
    97  			writer.RawByte(',')
    98  		}
    99  		first = false
   100  		writer.String(key)
   101  		writer.RawByte(':')
   102  		value.MarshalEasyJSON(writer)
   103  	}
   104  	writer.RawByte('}')
   105  }
   106  
   107  func (m *ValueMap) UnmarshalEasyJSON(lexer *jlexer.Lexer) {
   108  	if lexer.IsNull() {
   109  		lexer.Null()
   110  		*m = ValueMap{}
   111  		return
   112  	}
   113  	m.data = make(map[string]Value)
   114  	lexer.Delim('{')
   115  	for !lexer.IsDelim('}') {
   116  		key := string(lexer.String())
   117  		lexer.WantColon()
   118  		var value Value
   119  		value.UnmarshalEasyJSON(lexer)
   120  		m.data[key] = value
   121  		lexer.WantComma()
   122  	}
   123  	lexer.Delim('}')
   124  }
   125  

View as plain text