v0.0.470 Add GoextJsonMarshaller interface to call when marshalling json via gojson
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m51s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m51s
This commit is contained in:
parent
2504ef00a0
commit
7fc73f1e93
@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.469"
|
const GoextVersion = "0.0.470"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-06-11T12:10:49+0200"
|
const GoextVersionTimestamp = "2024-06-11T19:34:48+0200"
|
||||||
|
@ -769,6 +769,13 @@ type structFields struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (se structEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
|
func (se structEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
|
||||||
|
|
||||||
|
if v.CanAddr() && v.Addr().Type().Implements(goextJsonMarshallerType) {
|
||||||
|
if gejm, ok := v.Addr().Interface().(GoextJsonMarshaller); ok {
|
||||||
|
gejm.PreGoJsonMarshal()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
next := byte('{')
|
next := byte('{')
|
||||||
FieldLoop:
|
FieldLoop:
|
||||||
for i := range se.fields.list {
|
for i := range se.fields.list {
|
||||||
|
13
gojson/interfaces.go
Normal file
13
gojson/interfaces.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package json
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GoextJsonMarshaller interface {
|
||||||
|
PreGoJsonMarshal()
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
goextJsonMarshallerType = reflect.TypeOf((*GoextJsonMarshaller)(nil)).Elem()
|
||||||
|
)
|
37
gojson/interfaces_test.go
Normal file
37
gojson/interfaces_test.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package json
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type testStruct1 struct {
|
||||||
|
TS2 *testStruct2 `json:"ts2"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type testStruct2 struct {
|
||||||
|
A string `json:"a"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *testStruct2) PreGoJsonMarshal() {
|
||||||
|
t.A = "Hello"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGoextJsonMarshaller(t *testing.T) {
|
||||||
|
t1 := testStruct1{TS2: &testStruct2{}}
|
||||||
|
|
||||||
|
v, err := Marshal(t1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%s\n", v)
|
||||||
|
|
||||||
|
if string(v) != `{"ts2":{"a":"Hello"}}` {
|
||||||
|
t.Fatalf("PreGoJsonMarshal failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
if t1.TS2.A != "Hello" {
|
||||||
|
t.Fatalf("PreGoJsonMarshal failed")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user