github.com/vmware/govmomi@v0.37.2/vim25/types/json.go (about)

     1  /*
     2  Copyright (c) 2023-2023 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package types
    18  
    19  import (
    20  	"bytes"
    21  	"io"
    22  	"reflect"
    23  	"time"
    24  
    25  	"github.com/vmware/govmomi/vim25/json"
    26  )
    27  
    28  const (
    29  	discriminatorMemberName  = "_typeName"
    30  	primitiveValueMemberName = "_value"
    31  )
    32  
    33  var discriminatorTypeRegistry = map[string]reflect.Type{
    34  	"boolean":  reflect.TypeOf(true),
    35  	"byte":     reflect.TypeOf(uint8(0)),
    36  	"short":    reflect.TypeOf(int16(0)),
    37  	"int":      reflect.TypeOf(int32(0)),
    38  	"long":     reflect.TypeOf(int64(0)),
    39  	"float":    reflect.TypeOf(float32(0)),
    40  	"double":   reflect.TypeOf(float64(0)),
    41  	"string":   reflect.TypeOf(""),
    42  	"binary":   reflect.TypeOf([]byte{}),
    43  	"dateTime": reflect.TypeOf(time.Now()),
    44  }
    45  
    46  // NewJSONDecoder creates JSON decoder configured for VMOMI.
    47  func NewJSONDecoder(r io.Reader) *json.Decoder {
    48  	res := json.NewDecoder(r)
    49  	res.SetDiscriminator(
    50  		discriminatorMemberName,
    51  		primitiveValueMemberName,
    52  		json.DiscriminatorToTypeFunc(func(name string) (reflect.Type, bool) {
    53  			if res, ok := TypeFunc()(name); ok {
    54  				return res, true
    55  			}
    56  			if res, ok := discriminatorTypeRegistry[name]; ok {
    57  				return res, true
    58  			}
    59  			return nil, false
    60  		}),
    61  	)
    62  	return res
    63  }
    64  
    65  // VMOMI primitive names
    66  var discriminatorNamesRegistry = map[reflect.Type]string{
    67  	reflect.TypeOf(true):       "boolean",
    68  	reflect.TypeOf(uint8(0)):   "byte",
    69  	reflect.TypeOf(int16(0)):   "short",
    70  	reflect.TypeOf(int32(0)):   "int",
    71  	reflect.TypeOf(int64(0)):   "long",
    72  	reflect.TypeOf(float32(0)): "float",
    73  	reflect.TypeOf(float64(0)): "double",
    74  	reflect.TypeOf(""):         "string",
    75  	reflect.TypeOf([]byte{}):   "binary",
    76  	reflect.TypeOf(time.Now()): "dateTime",
    77  }
    78  
    79  // NewJSONEncoder creates JSON encoder configured for VMOMI.
    80  func NewJSONEncoder(w *bytes.Buffer) *json.Encoder {
    81  	enc := json.NewEncoder(w)
    82  	enc.SetDiscriminator(
    83  		discriminatorMemberName,
    84  		primitiveValueMemberName,
    85  		json.DiscriminatorEncodeTypeNameRootValue|
    86  			json.DiscriminatorEncodeTypeNameAllObjects,
    87  	)
    88  	enc.SetTypeToDiscriminatorFunc(VmomiTypeName)
    89  	return enc
    90  }
    91  
    92  // VmomiTypeName computes the VMOMI type name of a go type. It uses a lookup
    93  // table for VMOMI primitive types and the default discriminator function for
    94  // other types.
    95  func VmomiTypeName(t reflect.Type) (discriminator string) {
    96  	// Look up primitive type names from VMOMI protocol
    97  	if name, ok := discriminatorNamesRegistry[t]; ok {
    98  		return name
    99  	}
   100  	name := json.DefaultDiscriminatorFunc(t)
   101  	return name
   102  }