File: main.go

package info (click to toggle)
golang-github-francoispqt-gojay 1.2.13-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,456 kB
  • sloc: makefile: 86
file content (158 lines) | stat: -rw-r--r-- 2,998 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main

import (
	"log"
	"net/http"

	"github.com/francoispqt/gojay"
)

func main() {
	log.Println("Listening on port 3000")
	log.Fatal(http.ListenAndServe(":3000", http.HandlerFunc(handler)))
}

func handler(w http.ResponseWriter, r *http.Request) {
	var body Body
	dec := gojay.BorrowDecoder(r.Body)
	defer dec.Release()
	err := dec.Decode(&body)
	if err != nil {
		panic(err)
	}
	w.Header().Set("Content-Type", "application/json")
	enc := gojay.BorrowEncoder(w)
	defer enc.Release()
	err = enc.Encode(&body)
	if err != nil {
		panic(err)
	}
}

type Body struct {
	Colors *colors `json:"colors"`
}

func (c *Body) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
	switch k {
	case "colors":
		cols := make(colors, 0)
		c.Colors = &cols
		return dec.Array(c.Colors)
	}
	return nil
}
func (b *Body) NKeys() int {
	return 1
}

func (b *Body) MarshalJSONObject(enc *gojay.Encoder) {
	enc.ArrayKey("colors", b.Colors)
}
func (b *Body) IsNil() bool {
	return b == nil
}

type colors []*Color

func (b *colors) UnmarshalJSONArray(dec *gojay.Decoder) error {
	color := &Color{}
	if err := dec.Object(color); err != nil {
		return err
	}
	*b = append(*b, color)
	return nil
}

func (b *colors) MarshalJSONArray(enc *gojay.Encoder) {
	for _, color := range *b {
		enc.Object(color)
	}
}

func (b *colors) IsNil() bool {
	return len(*b) == 0
}

type Color struct {
	Color    string `json:"color,omitempty"`
	Category string `json:"category,omitempty"`
	Type     string `json:"type,omitempty"`
	Code     *Code  `json:"code,omitempty"`
}

func (b *Color) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
	switch k {
	case "color":
		return dec.String(&b.Color)
	case "category":
		return dec.String(&b.Category)
	case "type":
		return dec.String(&b.Type)
	case "code":
		b.Code = &Code{}
		return dec.Object(b.Code)
	}
	return nil
}
func (b *Color) NKeys() int {
	return 4
}

func (b *Color) MarshalJSONObject(enc *gojay.Encoder) {
	enc.ObjectKey("code", b.Code)
	enc.StringKey("color", b.Color)
	enc.StringKey("category", b.Category)
	enc.StringKey("type", b.Type)
}
func (b *Color) IsNil() bool {
	return b == nil
}

type Code struct {
	RGBA *ints  `json:"rgba,omitempty"`
	Hex  string `json:"hex,omitempty"`
}

func (c *Code) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
	switch k {
	case "rgba":
		rgba := make(ints, 0)
		c.RGBA = &rgba
		return dec.Array(&rgba)
	case "hex":
		return dec.String(&c.Hex)
	}
	return nil
}
func (b *Code) NKeys() int {
	return 2
}

func (b *Code) MarshalJSONObject(enc *gojay.Encoder) {
	enc.ArrayKey("rgba", b.RGBA)
	enc.StringKey("hex", b.Hex)
}
func (b *Code) IsNil() bool {
	return b == nil
}

type ints []int

func (v *ints) UnmarshalJSONArray(dec *gojay.Decoder) error {
	var i int
	if err := dec.Int(&i); err != nil {
		return err
	}
	*v = append(*v, i)
	return nil
}

func (v *ints) MarshalJSONArray(enc *gojay.Encoder) {
	for _, i := range *v {
		enc.Int(i)
	}
}
func (v *ints) IsNil() bool {
	return v == nil || len(*v) == 0
}