Skip to content

Commit e393564

Browse files
committed
Sparse JSON type prefix #15.
1 parent 26fc1f1 commit e393564

10 files changed

+326
-175
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ build:
22
go build
33
make -C cmd build
44

5-
test: build
5+
test:
66
make -C cmd test
77
go test -race -timeout 4000s -test.run=. -test.bench=xxx -test.benchmem=true
88
go test -timeout 4000s -test.run=xxx -test.bench=. -test.benchmem=true

cmd/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ build:
22
go build -o gson
33
go build -tags n1ql -o gsonn1ql
44

5-
test: build
5+
test:
66
go test -race -test.run=. -test.bench=. -test.benchmem=true
77
./sort.sh
88
./transforms.sh

cmd/cmd_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ func TestCmdArgs(t *testing.T) {
4040
{
4141
[]string{"-inpfile", "example.json", "-json2collate"},
4242
[]byte("Json: \"hello world\"\n" +
43-
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
44-
"Coll: [16 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
43+
"Coll: \"Zhello world\\x00\\x00\"\n" +
44+
"Coll: [90 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
4545
},
4646
{
4747
[]string{"-inptxt", `"hello world"`, "-json2collate"},
4848
[]byte("Json: \"hello world\"\n" +
49-
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
50-
"Coll: [16 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
49+
"Coll: \"Zhello world\\x00\\x00\"\n" +
50+
"Coll: [90 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
5151
},
5252
// json options
5353
{
@@ -78,20 +78,20 @@ func TestCmdArgs(t *testing.T) {
7878
{
7979
[]string{"-inpfile", "example.cbor", "-cbor2collate"},
8080
[]byte("Cbor: \"khello world\"\n" +
81-
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
82-
"Coll: [16 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
81+
"Coll: \"Zhello world\\x00\\x00\"\n" +
82+
"Coll: [90 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
8383
},
8484
{
8585
[]string{"-inptxt", "khello world", "-cbor2collate"},
8686
[]byte("Cbor: \"khello world\"\n" +
87-
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
88-
"Coll: [16 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
87+
"Coll: \"Zhello world\\x00\\x00\"\n" +
88+
"Coll: [90 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
8989
},
9090
{
9191
[]string{"-inptxt", "khello world", "-cbor2collate"},
9292
[]byte("Cbor: \"khello world\"\n" +
93-
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
94-
"Coll: [16 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
93+
"Coll: \"Zhello world\\x00\\x00\"\n" +
94+
"Coll: [90 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
9595
},
9696
// cbor options
9797
{
@@ -113,16 +113,16 @@ func TestCmdArgs(t *testing.T) {
113113
// collate transformations
114114
{
115115
[]string{"-inpfile", "example.coll", "-collate2value"},
116-
[]byte("Coll: \"\\x10hello world\\x00\\x00\"\nValu: hello world\n"),
116+
[]byte("Coll: \"Zhello world\\x00\\x00\"\nValu: hello world\n"),
117117
},
118118
{
119119
[]string{"-inpfile", "example.coll", "-collate2json"},
120-
[]byte("Coll: \"\\x10hello world\\x00\\x00\"\nJson: \"hello world\"\n"),
120+
[]byte("Coll: \"Zhello world\\x00\\x00\"\nJson: \"hello world\"\n"),
121121
},
122122
{
123123
[]string{"-inpfile", "example.coll", "-collate2cbor"},
124124
[]byte(
125-
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
125+
"Coll: \"Zhello world\\x00\\x00\"\n" +
126126
"Cbor: \"khello world\"\n" +
127127
"Cbor: [107 104 101 108 108 111 32 119 111 114 108 100]\n"),
128128
},
@@ -139,8 +139,8 @@ func TestCmdArgs(t *testing.T) {
139139
{
140140
[]string{"-inptxt", `"hello world"`, "-value2collate"},
141141
[]byte("Valu: hello world\n" +
142-
"Coll: \"\\x10hello world\\x00\\x00\"\n" +
143-
"Coll: [16 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
142+
"Coll: \"Zhello world\\x00\\x00\"\n" +
143+
"Coll: [90 104 101 108 108 111 32 119 111 114 108 100 0 0]\n"),
144144
},
145145
}
146146
for _, testcase := range testcases {

cmd/example.coll

0 Bytes
Binary file not shown.

cmd/transforms.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ echo "cbor2collate ..."
3838
echo
3939

4040
echo "collate2cbor ..."
41-
./gson -quote -collate2cbor -inptxt '"\x13\x11>3\x00\x10perl\x00\x00\x10ugly\x00\x00\x10php\x00\x00\x10bad\x00\x00\x10python\x00\x00\x10good\x00\x00\x00"'
41+
./gson -quote -collate2cbor -inptxt '"xd>3\x00Zperl\x00\x00Zugly\x00\x00Zphp\x00\x00Zbad\x00\x00Zpython\x00\x00Zgood\x00\x00\x00"'
4242
echo
4343

4444
echo "value2cbor ..."
@@ -54,5 +54,5 @@ echo "json2collate ..."
5454
echo
5555

5656
echo "collate2json ..."
57-
./gson -quote -collate2json -inptxt '"\x13\x11>3\x00\x10perl\x00\x00\x10ugly\x00\x00\x10php\x00\x00\x10bad\x00\x00\x10python\x00\x00\x10good\x00\x00\x00"'
57+
./gson -quote -collate2json -inptxt '"xd>3\x00Zperl\x00\x00Zugly\x00\x00Zphp\x00\x00Zbad\x00\x00Zpython\x00\x00Zgood\x00\x00\x00"'
5858
echo

collate.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ package gson
33
import "encoding/json"
44
import "bytes"
55

6-
// Collation order for supported types, to change the order set these
7-
// values in your init() function.
6+
// Collation order for supported types. Applications desiring different
7+
// ordering between types can initialize these bytes values before
8+
// using this package.
89
var (
910
Terminator byte = 0
10-
TypeMissing byte = 11
11-
TypeNull byte = 12
12-
TypeFalse byte = 13
13-
TypeTrue byte = 14
14-
TypeNumber byte = 15
15-
TypeString byte = 16
16-
TypeLength byte = 17
17-
TypeArray byte = 18
18-
TypeObj byte = 19
19-
TypeBinary byte = 20
11+
TypeMissing byte = 49
12+
TypeNull byte = 50
13+
TypeFalse byte = 60
14+
TypeTrue byte = 70
15+
TypeNumber byte = 80
16+
TypeString byte = 90
17+
TypeLength byte = 100
18+
TypeArray byte = 110
19+
TypeObj byte = 120
20+
TypeBinary byte = 130
2021
)
2122

2223
// Missing denotes a special type for an item that evaluates to _nothing_.

collate_cbor_test.go

+47-50
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "bytes"
55
import "fmt"
66

77
func TestCbor2CollateNil(t *testing.T) {
8-
inp, ref, config := "null", `\f\x00`, NewDefaultConfig()
8+
inp, ref, config := "null", `2\x00`, NewDefaultConfig()
99
clt := config.NewCollate(make([]byte, 1024), 0)
1010
cbr := config.NewCbor(make([]byte, 1024), 0)
1111
cltback := config.NewCollate(make([]byte, 1024), 0)
@@ -19,7 +19,7 @@ func TestCbor2CollateNil(t *testing.T) {
1919
}
2020

2121
func TestCbor2CollateTrue(t *testing.T) {
22-
inp, ref, config := "true", `\x0e\x00`, NewDefaultConfig()
22+
inp, ref, config := "true", `F\x00`, NewDefaultConfig()
2323
clt := config.NewCollate(make([]byte, 1024), 0)
2424
cbr := config.NewCbor(make([]byte, 1024), 0)
2525
cltback := config.NewCollate(make([]byte, 1024), 0)
@@ -33,7 +33,7 @@ func TestCbor2CollateTrue(t *testing.T) {
3333
}
3434

3535
func TestCbor2CollateFalse(t *testing.T) {
36-
inp, ref, config := "false", `\r\x00`, NewDefaultConfig()
36+
inp, ref, config := "false", `<\x00`, NewDefaultConfig()
3737
clt := config.NewCollate(make([]byte, 1024), 0)
3838
cbr := config.NewCbor(make([]byte, 1024), 0)
3939
cltback := config.NewCollate(make([]byte, 1024), 0)
@@ -48,34 +48,33 @@ func TestCbor2CollateFalse(t *testing.T) {
4848

4949
func TestCbor2CollateNumber(t *testing.T) {
5050
testcases := [][3]interface{}{
51-
{"10.2", `\x0f>>2102-\x00`, FloatNumber},
52-
{"10", `\x0f>>21-\x00`, FloatNumber},
53-
{"10.2", `\x0f>>2102-\x00`, SmartNumber},
54-
{"10", `\x0f>>21-\x00`, SmartNumber},
55-
{"10", `\x0f>>21-\x00`, FloatNumber},
56-
{"-10", `\x0f--78>\x00`, SmartNumber},
57-
{"-10", `\x0f--78>\x00`, FloatNumber},
58-
{"200", `\x0f>>32-\x00`, SmartNumber},
59-
{"200", `\x0f>>32-\x00`, FloatNumber},
60-
{"-200", `\x0f--67>\x00`, SmartNumber},
61-
{"-200", `\x0f--67>\x00`, FloatNumber},
51+
{"10.2", `P>>2102-\x00`, FloatNumber},
52+
{"10", `P>>21-\x00`, FloatNumber},
53+
{"10.2", `P>>2102-\x00`, SmartNumber},
54+
{"10", `P>>21-\x00`, SmartNumber},
55+
{"10", `P>>21-\x00`, FloatNumber},
56+
{"-10", `P--78>\x00`, SmartNumber},
57+
{"-10", `P--78>\x00`, FloatNumber},
58+
{"200", `P>>32-\x00`, SmartNumber},
59+
{"200", `P>>32-\x00`, FloatNumber},
60+
{"-200", `P--67>\x00`, SmartNumber},
61+
{"-200", `P--67>\x00`, FloatNumber},
6262
{
63-
"4294967297", `\x0f>>>2104294967297-\x00`, FloatNumber},
63+
"4294967297", `P>>>2104294967297-\x00`, FloatNumber},
6464
{
65-
"-4294967297", `\x0f---7895705032702>\x00`, FloatNumber},
65+
"-4294967297", `P---7895705032702>\x00`, FloatNumber},
6666
{
67-
"4294967297", `\x0f>>>2104294967297-\x00`, SmartNumber},
67+
"4294967297", `P>>>2104294967297-\x00`, SmartNumber},
6868
{
69-
"-4294967297", `\x0f---7895705032702>\x00`, SmartNumber},
69+
"-4294967297", `P---7895705032702>\x00`, SmartNumber},
7070
{
71-
"9007199254740992", `\x0f>>>2169007199254740992-\x00`, FloatNumber},
71+
"9007199254740992", `P>>>2169007199254740992-\x00`, FloatNumber},
7272
{
73-
"-9007199254740993", `\x0f---7830992800745259007>\x00`, FloatNumber},
73+
"-9007199254740993", `P---7830992800745259007>\x00`, FloatNumber},
7474
{
75-
"9007199254740992", `\x0f>>>2169007199254740992-\x00`, SmartNumber},
76-
75+
"9007199254740992", `P>>>2169007199254740992-\x00`, SmartNumber},
7776
{
78-
"-9007199254740993", `\x0f---7830992800745259006>\x00`, SmartNumber},
77+
"-9007199254740993", `P---7830992800745259006>\x00`, SmartNumber},
7978
}
8079

8180
for _, tcase := range testcases {
@@ -101,9 +100,9 @@ func TestCbor2CollateNumber(t *testing.T) {
101100

102101
func TestCbor2CollateString(t *testing.T) {
103102
testcases := [][2]string{
104-
{`""`, `\x10\x00\x00`},
105-
{`"hello world"`, `\x10hello world\x00\x00`},
106-
{fmt.Sprintf(`"%s"`, MissingLiteral), `\v\x00`},
103+
{`""`, `Z\x00\x00`},
104+
{`"hello world"`, `Zhello world\x00\x00`},
105+
{fmt.Sprintf(`"%s"`, MissingLiteral), `1\x00`},
107106
}
108107

109108
config := NewDefaultConfig()
@@ -128,36 +127,36 @@ func TestCbor2CollateString(t *testing.T) {
128127
}
129128

130129
// missing string without doMissing configuration
131-
inp := fmt.Sprintf(`"%s"`, MissingLiteral)
132-
refcode := `\x10~[]{}falsenilNA~\x00\x00`
130+
inp := []byte(fmt.Sprintf(`"%s"`, MissingLiteral))
131+
refcode := `Z~[]{}falsenilNA~\x00\x00`
133132
config = NewDefaultConfig().UseMissing(false)
134133
clt = config.NewCollate(make([]byte, 1024), 0)
135134
cbr = config.NewCbor(make([]byte, 1024), 0)
136135
cltback = config.NewCollate(make([]byte, 1024), 0)
137136

138-
config.NewJson([]byte(inp), -1).Tocollate(clt).Tocbor(cbr).Tocollate(cltback)
137+
config.NewJson(inp, -1).Tocollate(clt).Tocbor(cbr).Tocollate(cltback)
139138
seqn := fmt.Sprintf("%q", cltback.Bytes())
140139
if seqn = seqn[1 : len(seqn)-1]; seqn != refcode {
141140
t.Errorf("expected %v, got %v", refcode, seqn)
142141
}
143142

144143
// utf8 string
145-
inp = `"汉语 / 漢語; Hàn\b \t\uef24yǔ "`
144+
inp = []byte(`"汉语 / 漢語; Hàn\b \t\uef24yǔ "`)
146145

147146
config = NewDefaultConfig()
148147
clt = config.NewCollate(make([]byte, 1024), 0)
149148
cbr = config.NewCbor(make([]byte, 1024), 0)
150149
cltback = config.NewCollate(make([]byte, 1024), 0)
151150

152-
config.NewJson([]byte(inp), -1).Tocollate(clt).Tocbor(cbr).Tocollate(cltback)
151+
config.NewJson(inp, -1).Tocollate(clt).Tocbor(cbr).Tocollate(cltback)
153152

154153
if bytes.Compare(clt.Bytes(), cltback.Bytes()) != 0 {
155154
t.Errorf("expected %v, got %v", clt.Bytes(), cltback.Bytes())
156155
}
157156
}
158157

159158
func TestCbor2CollateBytes(t *testing.T) {
160-
inp, refcode := []byte("hello world"), `\x14hello world\x00`
159+
inp, refcode := []byte("hello world"), `\x82hello world\x00`
161160
config := NewDefaultConfig()
162161
clt := config.NewCollate(make([]byte, 1024), 0)
163162
cbr := config.NewCbor(make([]byte, 1024), 0)
@@ -175,20 +174,18 @@ func TestCbor2CollateArray(t *testing.T) {
175174
// without length prefix
176175
testcases := [][4]string{
177176
{`[]`,
178-
`\x12\x00`,
179-
`\x12\x110\x00\x00`,
177+
`n\x00`,
178+
`nd0\x00\x00`,
180179
`[]`},
181180
{`[null,true,false,10.0,"hello"]`,
182-
`\x12\f\x00\x0e\x00\r\x00\x0f>>21-\x00\x10hello\x00\x00\x00`,
183-
`\x12\x11>5\x00\f\x00\x0e\x00\r\x00\x0f>>21-\x00\x10hello\x00` +
184-
`\x00\x00`,
181+
`n2\x00F\x00<\x00P>>21-\x00Zhello\x00\x00\x00`,
182+
`nd>5\x002\x00F\x00<\x00P>>21-\x00Zhello\x00\x00\x00`,
185183
`[null,true,false,+0.1e+2,"hello"]`},
186184
{`[null,true,10.0,10.2,[],{"key":{}}]`,
187-
`\x12\f\x00\x0e\x00\x0f>>21-\x00\x0f>>2102-\x00\x12\x00` +
188-
`\x13\x11>1\x00\x10key\x00\x00\x13\x110\x00\x00\x00\x00`,
189-
`\x12\x11>6\x00\f\x00\x0e\x00\x0f>>21-\x00\x0f>>2102-\x00` +
190-
`\x12\x110\x00\x00\x13\x11>1\x00\x10key\x00\x00\x13\x110` +
191-
`\x00\x00\x00\x00`,
185+
`n2\x00F\x00P>>21-\x00P>>2102-\x00n\x00xd>1\x00Zkey\x00` +
186+
`\x00xd0\x00\x00\x00\x00`,
187+
`nd>6\x002\x00F\x00P>>21-\x00P>>2102-\x00nd0\x00\x00xd>1` +
188+
`\x00Zkey\x00\x00xd0\x00\x00\x00\x00`,
192189
`[null,true,+0.1e+2,+0.102e+2,[],{"key":{}}]`},
193190
}
194191

@@ -258,17 +255,17 @@ func TestCbor2CollateMap(t *testing.T) {
258255
testcases := [][4]string{
259256
{
260257
`{}`,
261-
`\x13\x110\x00\x00`,
262-
`\x13\x00`,
258+
`xd0\x00\x00`,
259+
`x\x00`,
263260
`{}`},
264261
{
265262
`{"a":null,"b":true,"c":false,"d":10.0,"e":"hello","f":["wo"]}`,
266-
`\x13\x11>6\x00\x10a\x00\x00\f\x00\x10b\x00\x00\x0e\x00\x10c` +
267-
`\x00\x00\r\x00\x10d\x00\x00\x0f>>21-\x00\x10e\x00\x00\x10hello` +
268-
`\x00\x00\x10f\x00\x00\x12\x10wo\x00\x00\x00\x00`,
269-
`\x13\x10a\x00\x00\f\x00\x10b\x00\x00\x0e\x00\x10c\x00\x00` +
270-
`\r\x00\x10d\x00\x00\x0f>>21-\x00\x10e\x00\x00\x10hello` +
271-
`\x00\x00\x10f\x00\x00\x12\x10wo\x00\x00\x00\x00`,
263+
`xd>6\x00Za\x00\x002\x00Zb\x00\x00F\x00Zc\x00\x00<\x00Zd\x00` +
264+
`\x00P>>21-\x00Ze\x00\x00Zhello\x00\x00Zf\x00\x00nZwo\x00` +
265+
`\x00\x00\x00`,
266+
`xZa\x00\x002\x00Zb\x00\x00F\x00Zc\x00\x00<\x00Zd\x00\x00P` +
267+
`>>21-\x00Ze\x00\x00Zhello\x00\x00Zf\x00\x00nZwo\x00\x00` +
268+
`\x00\x00`,
272269
`{"a":null,"b":true,"c":false,"d":+0.1e+2,"e":"hello","f":["wo"]}`},
273270
}
274271

0 commit comments

Comments
 (0)