gno
gno copied to clipboard
feat(p/json): optimize json
Description
Optimized the JSON package and simplified JSON node creation using the builder pattern.
- in
buffer.gno
andescape.gno
files are modified the use of map for lookup tables to use slice array instead. - refactor the
Unquote
function inescape.gno
file - modified the existing functions that parsed numbers to use
strconv
package, and deleted related files and functions- especially, the
eisel_lemire
andryu
packages were deleted since they were files that had been added to handleParseUint
andParseFloat
instrconv
package.
- especially, the
JSON Generate Example
Plain JSON
node := Builder().
WithString("name", "Alice").
WithNumber("age", 30).
WithBool("is_student", false).
Node()
value, err := Marshal(node)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Output:
{"name":"Alice","age":30,"is_student":false}
Nested Structure
node := Builder().
WriteString("name", "Alice").
WriteObject("address", func(b *NodeBuilder) {
b.WriteString("city", "New York").
WriteNumber("zipcode", 10001)
}).
Node()
// ...
Output:
{"name":"Alice","address":{"city":"New York","zipcode":10001}}
Benchmark Result for Unquote
Before
BenchmarkUnquote-8 12433488 98.06 ns/op 144 B/op 2 allocs/op
BenchmarkUnquoteWorstCase-8 24727736 50.46 ns/op 48 B/op 1 allocs/op
BenchmarkUnquoteBestCase-8 22542354 52.69 ns/op 48 B/op 1 allocs/op
BenchmarkUnquoteEmptyString-8 394868628 3.067 ns/op 0 B/op 0 allocs/op
After
BenchmarkUnquote-8 12464704 96.61 ns/op 144 B/op 2 allocs/op
BenchmarkUnquoteWorstCase-8 25084070 48.02 ns/op 48 B/op 1 allocs/op
BenchmarkUnquoteBestCase-8 23383227 52.66 ns/op 48 B/op 1 allocs/op
BenchmarkUnquoteEmptyString-8 400496838 2.968 ns/op 0 B/op 0 allocs/op