mirror of https://github.com/goccy/go-json.git
178 lines
31 KiB
Go
178 lines
31 KiB
Go
|
package benchmark
|
||
|
|
||
|
import (
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/francoispqt/gojay"
|
||
|
)
|
||
|
|
||
|
type DSUser struct {
|
||
|
Username string
|
||
|
}
|
||
|
|
||
|
func (m *DSUser) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
|
||
|
switch key {
|
||
|
case "username":
|
||
|
return dec.AddString(&m.Username)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
func (m *DSUser) NKeys() int {
|
||
|
return 1
|
||
|
}
|
||
|
func (m *DSUser) IsNil() bool {
|
||
|
return m == nil
|
||
|
}
|
||
|
func (m *DSUser) MarshalJSONObject(enc *gojay.Encoder) {
|
||
|
enc.AddStringKey("username", m.Username)
|
||
|
}
|
||
|
|
||
|
type DSTopic struct {
|
||
|
Id int
|
||
|
Slug string
|
||
|
}
|
||
|
|
||
|
func (m *DSTopic) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
|
||
|
switch key {
|
||
|
case "id":
|
||
|
return dec.AddInt(&m.Id)
|
||
|
case "slug":
|
||
|
return dec.AddString(&m.Slug)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
func (m *DSTopic) NKeys() int {
|
||
|
return 2
|
||
|
}
|
||
|
func (m *DSTopic) IsNil() bool {
|
||
|
return m == nil
|
||
|
}
|
||
|
func (m *DSTopic) MarshalJSONObject(enc *gojay.Encoder) {
|
||
|
enc.AddIntKey("id", m.Id)
|
||
|
enc.AddStringKey("slug", m.Slug)
|
||
|
}
|
||
|
|
||
|
type DSTopics []*DSTopic
|
||
|
|
||
|
func (t *DSTopics) UnmarshalJSONArray(dec *gojay.Decoder) error {
|
||
|
dsTopic := &DSTopic{}
|
||
|
*t = append(*t, dsTopic)
|
||
|
return dec.AddObject(dsTopic)
|
||
|
}
|
||
|
|
||
|
func (m *DSTopics) MarshalJSONArray(enc *gojay.Encoder) {
|
||
|
for _, e := range *m {
|
||
|
enc.AddObject(e)
|
||
|
}
|
||
|
}
|
||
|
func (m *DSTopics) IsNil() bool {
|
||
|
return m == nil
|
||
|
}
|
||
|
|
||
|
type DSTopicsList struct {
|
||
|
Topics DSTopics
|
||
|
MoreTopicsUrl string
|
||
|
}
|
||
|
|
||
|
func (m *DSTopicsList) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
|
||
|
switch key {
|
||
|
case "topics":
|
||
|
m.Topics = DSTopics{}
|
||
|
return dec.AddArray(&m.Topics)
|
||
|
case "more_topics_url":
|
||
|
return dec.AddString(&m.MoreTopicsUrl)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
func (m *DSTopicsList) NKeys() int {
|
||
|
return 2
|
||
|
}
|
||
|
|
||
|
func (m *DSTopicsList) IsNil() bool {
|
||
|
return m == nil
|
||
|
}
|
||
|
|
||
|
func (m *DSTopicsList) MarshalJSONObject(enc *gojay.Encoder) {
|
||
|
enc.AddArrayKey("users", &m.Topics)
|
||
|
enc.AddStringKey("more_topics_url", m.MoreTopicsUrl)
|
||
|
}
|
||
|
|
||
|
type DSUsers []*DSUser
|
||
|
|
||
|
func (t *DSUsers) UnmarshalJSONArray(dec *gojay.Decoder) error {
|
||
|
dsUser := DSUser{}
|
||
|
*t = append(*t, &dsUser)
|
||
|
return dec.AddObject(&dsUser)
|
||
|
}
|
||
|
|
||
|
func (m *DSUsers) MarshalJSONArray(enc *gojay.Encoder) {
|
||
|
for _, e := range *m {
|
||
|
enc.AddObject(e)
|
||
|
}
|
||
|
}
|
||
|
func (m *DSUsers) IsNil() bool {
|
||
|
return m == nil
|
||
|
}
|
||
|
|
||
|
type LargePayload struct {
|
||
|
Users DSUsers
|
||
|
Topics *DSTopicsList
|
||
|
}
|
||
|
|
||
|
func (m *LargePayload) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
|
||
|
switch key {
|
||
|
case "users":
|
||
|
return dec.AddArray(&m.Users)
|
||
|
case "topics":
|
||
|
m.Topics = &DSTopicsList{}
|
||
|
return dec.AddObject(m.Topics)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *LargePayload) NKeys() int {
|
||
|
return 2
|
||
|
}
|
||
|
|
||
|
//easyjson:json
|
||
|
func (m *LargePayload) MarshalJSONObject(enc *gojay.Encoder) {
|
||
|
enc.AddArrayKey("users", &m.Users)
|
||
|
enc.AddObjectKey("topics", m.Topics)
|
||
|
}
|
||
|
|
||
|
func (m *LargePayload) IsNil() bool {
|
||
|
return m == nil
|
||
|
}
|
||
|
|
||
|
var LargeFixture = []byte(`
|
||
|
{"users":[{"id":-1,"username":"system","avatar_template":"/user_avatar/discourse.metabase.com/system/{size}/6_1.png"},{"id":89,"username":"zergot","avatar_template":"https://avatars.discourse.org/v2/letter/z/0ea827/{size}.png"},{"id":1,"username":"sameer","avatar_template":"https://avatars.discourse.org/v2/letter/s/bbce88/{size}.png"},{"id":84,"username":"HenryMirror","avatar_template":"https://avatars.discourse.org/v2/letter/h/ecd19e/{size}.png"},{"id":73,"username":"fimp","avatar_template":"https://avatars.discourse.org/v2/letter/f/ee59a6/{size}.png"},{"id":14,"username":"agilliland","avatar_template":"/user_avatar/discourse.metabase.com/agilliland/{size}/26_1.png"},{"id":87,"username":"amir","avatar_template":"https://avatars.discourse.org/v2/letter/a/c37758/{size}.png"},{"id":82,"username":"waseem","avatar_template":"https://avatars.discourse.org/v2/letter/w/9dc877/{size}.png"},{"id":78,"username":"tovenaar","avatar_template":"https://avatars.discourse.org/v2/letter/t/9de0a6/{size}.png"},{"id":74,"username":"Ben","avatar_template":"https://avatars.discourse.org/v2/letter/b/df788c/{size}.png"},{"id":71,"username":"MarkLaFay","avatar_template":"https://avatars.discourse.org/v2/letter/m/3bc359/{size}.png"},{"id":72,"username":"camsaul","avatar_template":"/user_avatar/discourse.metabase.com/camsaul/{size}/70_1.png"},{"id":53,"username":"mhjb","avatar_template":"/user_avatar/discourse.metabase.com/mhjb/{size}/54_1.png"},{"id":58,"username":"jbwiv","avatar_template":"https://avatars.discourse.org/v2/letter/j/6bbea6/{size}.png"},{"id":70,"username":"Maggs","avatar_template":"https://avatars.discourse.org/v2/letter/m/bbce88/{size}.png"},{"id":69,"username":"andrefaria","avatar_template":"/user_avatar/discourse.metabase.com/andrefaria/{size}/65_1.png"},{"id":60,"username":"bencarter78","avatar_template":"/user_avatar/discourse.metabase.com/bencarter78/{size}/59_1.png"},{"id":55,"username":"vikram","avatar_template":"https://avatars.discourse.org/v2/letter/v/e47774/{size}.png"},{"id":68,"username":"edchan77","avatar_template":"/user_avatar/discourse.metabase.com/edchan77/{size}/66_1.png"},{"id":9,"username":"karthikd","avatar_template":"https://avatars.discourse.org/v2/letter/k/cab0a1/{size}.png"},{"id":23,"username":"arthurz","avatar_template":"/user_avatar/discourse.metabase.com/arthurz/{size}/32_1.png"},{"id":3,"username":"tom","avatar_template":"/user_avatar/discourse.metabase.com/tom/{size}/21_1.png"},{"id":50,"username":"LeoNogueira","avatar_template":"/user_avatar/discourse.metabase.com/leonogueira/{size}/52_1.png"},{"id":66,"username":"ss06vi","avatar_template":"https://avatars.discourse.org/v2/letter/s/3ab097/{size}.png"},{"id":34,"username":"mattcollins","avatar_template":"/user_avatar/discourse.metabase.com/mattcollins/{size}/41_1.png"},{"id":51,"username":"krmmalik","avatar_template":"/user_avatar/discourse.metabase.com/krmmalik/{size}/53_1.png"},{"id":46,"username":"odysseas","avatar_template":"https://avatars.discourse.org/v2/letter/o/5f8ce5/{size}.png"},{"id":5,"username":"jonthewayne","avatar_template":"/user_avatar/discourse.metabase.com/jonthewayne/{size}/18_1.png"},{"id":11,"username":"anandiyer","avatar_template":"/user_avatar/discourse.metabase.com/anandiyer/{size}/23_1.png"},{"id":25,"username":"alnorth","avatar_template":"/user_avatar/discourse.metabase.com/alnorth/{size}/34_1.png"},{"id":52,"username":"j_at_svg","avatar_template":"https://avatars.discourse.org/v2/letter/j/96bed5/{size}.png"},{"id":42,"username":"styts","avatar_template":"/user_avatar/discourse.metabase.com/styts/{size}/47_1.png"}],"topics":{"can_create_topic":false,"more_topics_url":"/c/uncategorized/l/latest?page=1","draft":null,"draft_key":"new_topic","draft_sequence":null,"per_page":30,"topics":[{"id":8,"title":"Welcome to Metabase's Discussion Forum","fancy_title":"Welcome to Metabase’s Discussion Forum","slug":"welcome-to-metabases-discussion-forum","posts_count":1,"reply_count":0,"highest_post_number":1,"image_url":"/images/welcome/discourse-edit-post-animated.gif","created_at":"2015-10-17T00:14:49.526Z","last_posted_at
|
||
|
`)
|
||
|
|
||
|
func NewLargePayload() *LargePayload {
|
||
|
dsUsers := DSUsers{}
|
||
|
dsTopics := DSTopics{}
|
||
|
for i := 0; i < 100; i++ {
|
||
|
str := "test" + strconv.Itoa(i)
|
||
|
dsUsers = append(
|
||
|
dsUsers,
|
||
|
&DSUser{
|
||
|
Username: str,
|
||
|
},
|
||
|
)
|
||
|
dsTopics = append(
|
||
|
dsTopics,
|
||
|
&DSTopic{
|
||
|
Id: i,
|
||
|
Slug: str,
|
||
|
},
|
||
|
)
|
||
|
}
|
||
|
return &LargePayload{
|
||
|
Users: dsUsers,
|
||
|
Topics: &DSTopicsList{
|
||
|
Topics: dsTopics,
|
||
|
MoreTopicsUrl: "http://test.com",
|
||
|
},
|
||
|
}
|
||
|
}
|