package json
import (
)
var errRawEmbedNotObject = errors.New("embedded raw value must be a JSON object")
var jsontextValueType = reflect.TypeFor[jsontext.Value]()
func ( *jsontext.Encoder, addressableValue, *jsonopts.Struct, *structField, func([]byte) bool) error {
:= addressableValue{.Field(.index0), .forcedAddr}
if len(.index) > 0 {
= .fieldByIndex(.index, false)
if !.IsValid() {
return nil
}
}
= .indirect(false)
if !.IsValid() {
return nil
}
if .Type() == jsontextValueType {
, := reflect.TypeAssert[jsontext.Value](.Value)
if len() == 0 {
return nil
}
:= export.GetBufferedDecoder()
defer export.PutBufferedDecoder()
:= export.Decoder()
.Flags.Set(jsonflags.AllowDuplicateNames | jsonflags.AllowInvalidUTF8 | 1)
, := .ReadToken()
if != nil {
if == io.EOF {
= io.ErrUnexpectedEOF
}
return newMarshalErrorBefore(, .Type(), )
}
if .Kind() != '{' {
return newMarshalErrorBefore(, .Type(), errRawEmbedNotObject)
}
for .PeekKind() != '}' {
var jsonwire.ValueFlags
, := .ReadValue(&)
if != nil {
return newMarshalErrorBefore(, .Type(), )
}
if != nil {
:= jsonwire.UnquoteMayCopy(, .IsVerbatim())
if !() {
return newDuplicateNameError(.StackPointer().Parent(), , .OutputOffset())
}
}
if := .WriteValue(); != nil {
return
}
, = .ReadValue(&)
if != nil {
return newMarshalErrorBefore(, .Type(), )
}
if := .WriteValue(); != nil {
return
}
}
if , := .ReadToken(); != nil {
return newMarshalErrorBefore(, .Type(), )
}
if := .CheckEOF(); != nil {
return newMarshalErrorBefore(, .Type(), )
}
return nil
} else {
:=
:= .Len()
if == 0 {
return nil
}
:= newAddressableValue(.Type().Key())
:= newAddressableValue(.Type().Elem())
:= func( addressableValue) error {
, := jsonwire.AppendQuote(.AvailableBuffer(), []byte(.String()), &.Flags)
if != nil {
return newMarshalErrorBefore(, .Type().Key(), )
}
if != nil {
:= bytes.IndexByte(, '\\') < 0
:= jsonwire.UnquoteMayCopy(, )
if !() {
return newDuplicateNameError(.StackPointer().Parent(), , .OutputOffset())
}
}
return .WriteValue()
}
:= .fncs.marshal
if .Marshalers != nil {
, _ = .Marshalers.(*Marshalers).lookup(, .Type())
}
if !.Flags.Get(jsonflags.Deterministic) || <= 1 {
for := .MapRange(); .Next(); {
.SetIterKey()
if := (); != nil {
return
}
.Set(.Value())
if := (, , ); != nil {
return
}
}
} else {
:= getStrings()
for , := 0, .Value.MapRange(); < && .Next(); ++ {
.SetIterKey()
(*)[] = .String()
}
slices.Sort(*)
for , := range * {
.SetString()
if := (); != nil {
return
}
.Set(.MapIndex(.Value))
if := (, , ); != nil {
return
}
}
putStrings()
}
return nil
}
}
func ( *jsontext.Decoder, addressableValue, *jsonopts.Struct, *structField, , []byte) error {
:= addressableValue{.Field(.index0), .forcedAddr}
if len(.index) > 0 {
= .fieldByIndex(.index, true)
}
= .indirect(true)
if .Type() == jsontextValueType {
, := reflect.TypeAssert[*jsontext.Value](.Addr())
if len(*) == 0 {
* = append(*, '{')
} else {
* = jsonwire.TrimSuffixWhitespace(*)
if jsonwire.HasSuffixByte(*, '}') {
* = jsonwire.TrimSuffixByte(*, '}')
* = jsonwire.TrimSuffixWhitespace(*)
if !jsonwire.HasSuffixByte(*, ',') && !jsonwire.HasSuffixByte(*, '{') {
* = append(*, ',')
}
} else {
return newUnmarshalErrorAfterWithSkipping(, .Type(), errRawEmbedNotObject)
}
}
* = append(*, ...)
* = append(*, ':')
, := .ReadValue()
if != nil {
return
}
* = append(*, ...)
* = append(*, '}')
return nil
} else {
:= string()
:=
if .IsNil() {
.Set(reflect.MakeMap(.Type()))
}
:= reflect.ValueOf()
if := .Type().Key(); != stringType {
= .Convert()
}
:= newAddressableValue(.Type().Elem())
if := .MapIndex(); .IsValid() {
.Set()
}
:= .fncs.unmarshal
if .Unmarshalers != nil {
, _ = .Unmarshalers.(*Unmarshalers).lookup(, .Type())
}
:= (, , )
.SetMapIndex(, .Value)
if != nil {
return
}
return nil
}
}