MCPcopy Create free account
hub / github.com/blues/note / newTypeEncoder

Function newTypeEncoder

jsonxt/encode.go:392–439  ·  view source on GitHub ↗

newTypeEncoder constructs an encoderFunc for a type. The returned encoder only checks CanAddr when allowAddr is true.

(t reflect.Type, allowAddr bool)

Source from the content-addressed store, hash-verified

390// newTypeEncoder constructs an encoderFunc for a type.
391// The returned encoder only checks CanAddr when allowAddr is true.
392func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
393 if t.Implements(marshalerType) {
394 return marshalerEncoder
395 }
396 if t.Kind() != reflect.Ptr && allowAddr {
397 if reflect.PointerTo(t).Implements(marshalerType) {
398 return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false))
399 }
400 }
401
402 if t.Implements(textMarshalerType) {
403 return textMarshalerEncoder
404 }
405 if t.Kind() != reflect.Ptr && allowAddr {
406 if reflect.PointerTo(t).Implements(textMarshalerType) {
407 return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false))
408 }
409 }
410
411 switch t.Kind() {
412 case reflect.Bool:
413 return boolEncoder
414 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
415 return intEncoder
416 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
417 return uintEncoder
418 case reflect.Float32:
419 return float32Encoder
420 case reflect.Float64:
421 return float64Encoder
422 case reflect.String:
423 return stringEncoder
424 case reflect.Interface:
425 return interfaceEncoder
426 case reflect.Struct:
427 return newStructEncoder(t)
428 case reflect.Map:
429 return newMapEncoder(t)
430 case reflect.Slice:
431 return newSliceEncoder(t)
432 case reflect.Array:
433 return newArrayEncoder(t)
434 case reflect.Ptr:
435 return newPtrEncoder(t)
436 default:
437 return unsupportedTypeEncoder
438 }
439}
440
441func invalidValueEncoder(e *encodeState, v reflect.Value, _ encOpts) {
442 e.WriteString("null")

Callers 1

typeEncoderFunction · 0.85

Calls 6

newCondAddrEncoderFunction · 0.85
newStructEncoderFunction · 0.85
newMapEncoderFunction · 0.85
newSliceEncoderFunction · 0.85
newArrayEncoderFunction · 0.85
newPtrEncoderFunction · 0.85

Tested by

no test coverage detected