File: encoding_test.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 (118 lines) | stat: -rw-r--r-- 2,028 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
package basic_struct

import (
	"bytes"
	"database/sql"
	"testing"

	"github.com/francoispqt/gojay"
	"github.com/stretchr/testify/require"
)

var isTrue = true
var msg = &Message{
	Id:     1022,
	Name:   "name acc",
	Price:  13.3,
	Ints:   []int{1, 2, 5},
	Floats: []float32{2.3, 4.6, 7.4},
	SubMessageX: &SubMessage{
		Id:          102,
		Description: "abcd",
	},
	MessagesX: []*SubMessage{
		&SubMessage{
			Id:          2102,
			Description: "abce",
		},
	},
	SubMessageY: SubMessage{
		Id:          3102,
		Description: "abcf",
	},
	MessagesY: []SubMessage{
		SubMessage{
			Id:          5102,
			Description: "abcg",
		},
		SubMessage{
			Id:          5106,
			Description: "abcgg",
		},
	},
	IsTrue:  &isTrue,
	Payload: []byte(`"123"`),
	SQLNullString: &sql.NullString{
		String: "test",
		Valid:  true,
	},
}

var jsonData = `{
  "Id": 1022,
  "Name": "name acc",
  "Price": 13.3,
  "Ints": [
    1,
    2,
    5
  ],
  "Floats": [
    2.3,
    4.6,
    7.4
  ],
  "SubMessageX": {
    "Id": 102,
    "Description": "abcd",
	"StartTime": "0001-01-01T00:00:00Z"
  },
  "MessagesX": [
    {
      "Id": 2102,
      "Description": "abce", 
	  "StartTime": "0001-01-01T00:00:00Z"
	}
  ],
  "SubMessageY": {
    "Id": 3102,
    "Description": "abcf",
    "StartTime": "0001-01-01T00:00:00Z"
  },
  "MessagesY": [
    {
      "Id": 5102,
      "Description": "abcg", 
	  "StartTime": "0001-01-01T00:00:00Z"
	},
    {
      "Id": 5106,
      "Description": "abcgg",
	  "StartTime": "0001-01-01T00:00:00Z"
    }
  ],
  "IsTrue": true,
  "Payload": "123",
  "SQLNullString": "test"
}`

func TestMessage_Unmarshal(t *testing.T) {
	var err error
	var data = []byte(jsonData)
	message := &Message{}
	err = gojay.UnmarshalJSONObject(data, message)
	require.Nil(t, err)
	require.Equal(t, msg, message)
}

func TestMessage_Marshal(t *testing.T) {
	var writer = new(bytes.Buffer)

	encoder := gojay.NewEncoder(writer)
	var err = encoder.Encode(msg)

	require.Nil(t, err)
	var JSON = writer.String()

	require.JSONEq(t, jsonData, JSON)
}