-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(p/json): remove unnecessary code and optimize (#2939)
# Description Optimized the JSON package and simplified JSON node creation using the builder pattern. - in `buffer.gno` and `escape.gno` files are modified the use of map for lookup tables to use slice array instead. - refactor the `Unquote` function in `escape.gno` file - modified the existing functions that parsed numbers to use `strconv` package, and deleted related files and functions - especially, the `eisel_lemire` and `ryu` packages were deleted since they were files that had been added to handle `ParseUint` and `ParseFloat` in `strconv` package. ## JSON Generate Example **Plain JSON** ```go 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** ```go 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** ```plain 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** ```plain 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 ```
- Loading branch information
Showing
23 changed files
with
383 additions
and
2,561 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package json | ||
|
||
type NodeBuilder struct { | ||
node *Node | ||
} | ||
|
||
func Builder() *NodeBuilder { | ||
return &NodeBuilder{node: ObjectNode("", nil)} | ||
} | ||
|
||
func (b *NodeBuilder) WriteString(key, value string) *NodeBuilder { | ||
b.node.AppendObject(key, StringNode("", value)) | ||
return b | ||
} | ||
|
||
func (b *NodeBuilder) WriteNumber(key string, value float64) *NodeBuilder { | ||
b.node.AppendObject(key, NumberNode("", value)) | ||
return b | ||
} | ||
|
||
func (b *NodeBuilder) WriteBool(key string, value bool) *NodeBuilder { | ||
b.node.AppendObject(key, BoolNode("", value)) | ||
return b | ||
} | ||
|
||
func (b *NodeBuilder) WriteNull(key string) *NodeBuilder { | ||
b.node.AppendObject(key, NullNode("")) | ||
return b | ||
} | ||
|
||
func (b *NodeBuilder) WriteObject(key string, fn func(*NodeBuilder)) *NodeBuilder { | ||
nestedBuilder := &NodeBuilder{node: ObjectNode("", nil)} | ||
fn(nestedBuilder) | ||
b.node.AppendObject(key, nestedBuilder.node) | ||
return b | ||
} | ||
|
||
func (b *NodeBuilder) WriteArray(key string, fn func(*ArrayBuilder)) *NodeBuilder { | ||
arrayBuilder := &ArrayBuilder{nodes: []*Node{}} | ||
fn(arrayBuilder) | ||
b.node.AppendObject(key, ArrayNode("", arrayBuilder.nodes)) | ||
return b | ||
} | ||
|
||
func (b *NodeBuilder) Node() *Node { | ||
return b.node | ||
} | ||
|
||
type ArrayBuilder struct { | ||
nodes []*Node | ||
} | ||
|
||
func (ab *ArrayBuilder) WriteString(value string) *ArrayBuilder { | ||
ab.nodes = append(ab.nodes, StringNode("", value)) | ||
return ab | ||
} | ||
|
||
func (ab *ArrayBuilder) WriteNumber(value float64) *ArrayBuilder { | ||
ab.nodes = append(ab.nodes, NumberNode("", value)) | ||
return ab | ||
} | ||
|
||
func (ab *ArrayBuilder) WriteInt(value int) *ArrayBuilder { | ||
return ab.WriteNumber(float64(value)) | ||
} | ||
|
||
func (ab *ArrayBuilder) WriteBool(value bool) *ArrayBuilder { | ||
ab.nodes = append(ab.nodes, BoolNode("", value)) | ||
return ab | ||
} | ||
|
||
func (ab *ArrayBuilder) WriteNull() *ArrayBuilder { | ||
ab.nodes = append(ab.nodes, NullNode("")) | ||
return ab | ||
} | ||
|
||
func (ab *ArrayBuilder) WriteObject(fn func(*NodeBuilder)) *ArrayBuilder { | ||
nestedBuilder := &NodeBuilder{node: ObjectNode("", nil)} | ||
fn(nestedBuilder) | ||
ab.nodes = append(ab.nodes, nestedBuilder.node) | ||
return ab | ||
} | ||
|
||
func (ab *ArrayBuilder) WriteArray(fn func(*ArrayBuilder)) *ArrayBuilder { | ||
nestedArrayBuilder := &ArrayBuilder{nodes: []*Node{}} | ||
fn(nestedArrayBuilder) | ||
ab.nodes = append(ab.nodes, ArrayNode("", nestedArrayBuilder.nodes)) | ||
return ab | ||
} |
Oops, something went wrong.