ptrstringval is essentially the same as ptrval but quotes strings if not nil
(conv func(unsafe.Pointer, *Buffer))
| 458 | |
| 459 | // ptrstringval is essentially the same as ptrval but quotes strings if not nil |
| 460 | func (e *StructEncoder) ptrstringval(conv func(unsafe.Pointer, *Buffer)) { |
| 461 | e.flunk() // flush any chunk data we've buffered |
| 462 | |
| 463 | // avoids allocs at runtime |
| 464 | null := []byte("null") |
| 465 | |
| 466 | f := e.f |
| 467 | e.appendInstructionFun(func(v unsafe.Pointer, w *Buffer) { |
| 468 | |
| 469 | p := unsafe.Pointer(*(*unsafe.Pointer)(unsafe.Pointer(uintptr(v) + f.Offset))) |
| 470 | if p == unsafe.Pointer(nil) { |
| 471 | w.Write(null) |
| 472 | return |
| 473 | } |
| 474 | |
| 475 | // quotes need to be at runtime here because we don't know if we're going to have to null the field |
| 476 | w.WriteByte('"') |
| 477 | conv(p, w) |
| 478 | w.WriteByte('"') |
| 479 | }) |
| 480 | } |
| 481 | |
| 482 | // JSONEncoder works with the `.encoder` option. Fields can implement this to encode their own JSON string straight |
| 483 | // into the working buffer. This can be useful if you're working with interface fields at runtime. |
no test coverage detected