@@ -25,21 +25,26 @@ type OrderSchemaItems []OrderSchemaItem
2525// of the OrderSchemaItems slice, keeping the original order of the slice.
2626func (items OrderSchemaItems ) MarshalJSON () ([]byte , error ) {
2727 buf := bytes .NewBuffer (nil )
28- buf .WriteString ("{" )
29- for i := range items {
30- if i > 0 {
31- buf .WriteString ("," )
32- }
33- buf .WriteString ("\" " )
34- buf .WriteString (items [i ].Name )
35- buf .WriteString ("\" :" )
36- bs , err := json .Marshal (& items [i ].Schema )
37- if err != nil {
28+ buf .WriteByte ('{' )
29+
30+ if len (items ) == 0 {
31+ buf .WriteByte ('}' )
32+
33+ return buf .Bytes (), nil
34+ }
35+
36+ if err := items .marshalJSONItem (items [0 ], buf ); err != nil {
37+ return nil , err
38+ }
39+
40+ for _ , item := range items [1 :] {
41+ buf .WriteByte (',' )
42+ if err := items .marshalJSONItem (item , buf ); err != nil {
3843 return nil , err
3944 }
40- buf .Write (bs )
4145 }
42- buf .WriteString ("}" )
46+ buf .WriteByte ('}' )
47+
4348 return buf .Bytes (), nil
4449}
4550
@@ -69,6 +74,22 @@ func (items OrderSchemaItems) Less(i, j int) (ret bool) {
6974 return items [i ].Name < items [j ].Name
7075}
7176
77+ func (items OrderSchemaItems ) marshalJSONItem (item OrderSchemaItem , output * bytes.Buffer ) error {
78+ nameJSON , err := json .Marshal (item .Name )
79+ if err != nil {
80+ return err
81+ }
82+ output .Write (nameJSON )
83+ output .WriteByte (':' )
84+ schemaJSON , err := json .Marshal (& item .Schema )
85+ if err != nil {
86+ return err
87+ }
88+ output .Write (schemaJSON )
89+
90+ return nil
91+ }
92+
7293// SchemaProperties is a map representing the properties of a Schema object.
7394// It knows how to transform its keys into an ordered slice.
7495type SchemaProperties map [string ]Schema
0 commit comments