Skip to content

Commit 8d7dff2

Browse files
committed
ADD example for simple usage. MOVE code from src to root.
1 parent e1d438f commit 8d7dff2

File tree

12 files changed

+232
-218
lines changed

12 files changed

+232
-218
lines changed

example/gpt-function-call/main.go

Whitespace-only changes.

example/simple-json-stream/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module simple-json-stream
2+
3+
go 1.21.5

example/simple-json-stream/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
streamingjson "github.com/karminski/streaming-json-go"
7+
)
8+
9+
func main() {
10+
jsonSegmentA := `{"a":` // will complete to `{"a":null}`
11+
lexer := streamingjson.NewLexer()
12+
lexer.AppendString(jsonSegmentA)
13+
completedJSON := lexer.CompleteJSON()
14+
fmt.Printf("completedJSON: %s\n", completedJSON)
15+
}
File renamed without changes.
File renamed without changes.

src/lexer.go renamed to lexer.go

Lines changed: 1 addition & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package streamingjsongo
22

33
import (
44
"fmt"
@@ -82,214 +82,6 @@ func (lexer *Lexer) skipJSONSegment(n int) {
8282
lexer.JSONSegment = lexer.JSONSegment[n:]
8383
}
8484

85-
// check if JSON stream stopped at an object properity's key start, like `{"`
86-
func (lexer *Lexer) streamStoppedInAnObjectKeyStart() bool {
87-
// `{`, `"` in stack, or `,`, `"` in stack
88-
case1 := []int{
89-
TOKEN_LEFT_BRACE,
90-
TOKEN_QUOTE,
91-
}
92-
case2 := []int{
93-
TOKEN_COMMA,
94-
TOKEN_QUOTE,
95-
}
96-
// `}` in mirror stack
97-
case3 := []int{
98-
TOKEN_RIGHT_BRACE,
99-
}
100-
return (matchStack(lexer.TokenStack, case1) || matchStack(lexer.TokenStack, case2)) && matchStack(lexer.MirrorTokenStack, case3)
101-
}
102-
103-
// check if JSON stream stopped in an object properity's key, like `{"field`
104-
func (lexer *Lexer) streamStoppedInAnObjectKeyEnd() bool {
105-
// `{`, `"`, `"` in stack, or `,`, `"`, `"` in stack
106-
case1 := []int{
107-
TOKEN_LEFT_BRACE,
108-
TOKEN_QUOTE,
109-
TOKEN_QUOTE,
110-
}
111-
case2 := []int{
112-
TOKEN_COMMA,
113-
TOKEN_QUOTE,
114-
TOKEN_QUOTE,
115-
}
116-
// `"`, `:`, `n`, `u`, `l`, `l`, `}` in mirror stack
117-
case3 := []int{
118-
TOKEN_RIGHT_BRACE,
119-
TOKEN_ALPHABET_LOWERCASE_L,
120-
TOKEN_ALPHABET_LOWERCASE_L,
121-
TOKEN_ALPHABET_LOWERCASE_U,
122-
TOKEN_ALPHABET_LOWERCASE_N,
123-
TOKEN_COLON,
124-
TOKEN_QUOTE,
125-
}
126-
return (matchStack(lexer.TokenStack, case1) || matchStack(lexer.TokenStack, case2)) && matchStack(lexer.MirrorTokenStack, case3)
127-
}
128-
129-
// check if JSON stream stopped in an object properity's value start, like `{"field": "`
130-
func (lexer *Lexer) streamStoppedInAnObjectStringValueStart() bool {
131-
// `:`, `"` in stack
132-
case1 := []int{
133-
TOKEN_COLON,
134-
TOKEN_QUOTE,
135-
}
136-
// `n`, `u`, `l`, `l`, `}` in mirror stack
137-
case2 := []int{
138-
TOKEN_RIGHT_BRACE,
139-
TOKEN_ALPHABET_LOWERCASE_L,
140-
TOKEN_ALPHABET_LOWERCASE_L,
141-
TOKEN_ALPHABET_LOWERCASE_U,
142-
TOKEN_ALPHABET_LOWERCASE_N,
143-
}
144-
return matchStack(lexer.TokenStack, case1) && matchStack(lexer.MirrorTokenStack, case2)
145-
}
146-
147-
// check if JSON stream stopped in an object properity's value finish, like `{"field": "value"`
148-
func (lexer *Lexer) streamStoppedInAnObjectValueEnd() bool {
149-
// `"`, `}` left
150-
tokens := []int{
151-
TOKEN_RIGHT_BRACE,
152-
TOKEN_QUOTE,
153-
}
154-
return matchStack(lexer.MirrorTokenStack, tokens)
155-
}
156-
157-
// check if JSON stream stopped in an object properity's value start by array, like `{"field":[`
158-
func (lexer *Lexer) streamStoppedInAnObjectArrayValueStart() bool {
159-
// `:`, `[` in stack
160-
case1 := []int{
161-
TOKEN_COLON,
162-
TOKEN_LEFT_BRACKET,
163-
}
164-
// `n`, `u`, `l`, `l`, `}` in mirror stack
165-
case2 := []int{
166-
TOKEN_RIGHT_BRACE,
167-
TOKEN_ALPHABET_LOWERCASE_L,
168-
TOKEN_ALPHABET_LOWERCASE_L,
169-
TOKEN_ALPHABET_LOWERCASE_U,
170-
TOKEN_ALPHABET_LOWERCASE_N,
171-
}
172-
return matchStack(lexer.TokenStack, case1) && matchStack(lexer.MirrorTokenStack, case2)
173-
}
174-
175-
// check if JSON stream stopped in an object properity's value start by array, like `{"field":{`
176-
func (lexer *Lexer) streamStoppedInAnObjectObjectValueStart() bool {
177-
// `:`, `{` in stack
178-
case1 := []int{
179-
TOKEN_COLON,
180-
TOKEN_LEFT_BRACE,
181-
}
182-
// `n`, `u`, `l`, `l`, `}` in mirror stack
183-
case2 := []int{
184-
TOKEN_RIGHT_BRACE,
185-
TOKEN_ALPHABET_LOWERCASE_L,
186-
TOKEN_ALPHABET_LOWERCASE_L,
187-
TOKEN_ALPHABET_LOWERCASE_U,
188-
TOKEN_ALPHABET_LOWERCASE_N,
189-
}
190-
return matchStack(lexer.TokenStack, case1) && matchStack(lexer.MirrorTokenStack, case2)
191-
}
192-
193-
// check if JSON stream stopped in an object properity's negative number value, like `:-`
194-
func (lexer *Lexer) streamStoppedInAnObjectNegativeNumberValueStart() bool {
195-
// `:`, `-` in stack
196-
case1 := []int{
197-
TOKEN_COLON,
198-
TOKEN_NEGATIVE,
199-
}
200-
return matchStack(lexer.TokenStack, case1)
201-
}
202-
203-
// check if JSON stream stopped in an object properity's negative number value, like `-`
204-
func (lexer *Lexer) streamStoppedInANegativeNumberValueStart() bool {
205-
// `-` in stack
206-
case1 := []int{
207-
TOKEN_NEGATIVE,
208-
}
209-
// `0`in mirror stack
210-
case2 := []int{
211-
TOKEN_NUMBER_0,
212-
}
213-
return matchStack(lexer.TokenStack, case1) && matchStack(lexer.MirrorTokenStack, case2)
214-
}
215-
216-
// check if JSON stream stopped in an array
217-
func (lexer *Lexer) streamStoppedInAnArray() bool {
218-
return lexer.getTopTokenOnMirrorStack() == TOKEN_RIGHT_BRACKET
219-
}
220-
221-
// check if JSON stream stopped in an array's string value end, like `["value"`
222-
func (lexer *Lexer) streamStoppedInAnArrayStringValueEnd() bool {
223-
// `"`, `"` in stack
224-
case1 := []int{
225-
TOKEN_QUOTE,
226-
TOKEN_QUOTE,
227-
}
228-
// `"`, `]` in mirror stack
229-
case2 := []int{
230-
TOKEN_RIGHT_BRACKET,
231-
TOKEN_QUOTE,
232-
}
233-
return matchStack(lexer.TokenStack, case1) && matchStack(lexer.MirrorTokenStack, case2)
234-
}
235-
236-
// check if JSON stream stopped in an object properity's value start by array, like `{"field":{`
237-
func (lexer *Lexer) streamStoppedInAnObjectNullValuePlaceholderStart() bool {
238-
// `n`, `u`, `l`, `l`, `}` in mirror stack
239-
case1 := []int{
240-
TOKEN_RIGHT_BRACE,
241-
TOKEN_ALPHABET_LOWERCASE_L,
242-
TOKEN_ALPHABET_LOWERCASE_L,
243-
TOKEN_ALPHABET_LOWERCASE_U,
244-
TOKEN_ALPHABET_LOWERCASE_N,
245-
}
246-
return matchStack(lexer.MirrorTokenStack, case1)
247-
}
248-
249-
// check if JSON stream stopped in a string, like `""`
250-
func (lexer *Lexer) streamStoppedInAString() bool {
251-
return lexer.getTopTokenOnStack() == TOKEN_QUOTE && lexer.getTopTokenOnMirrorStack() == TOKEN_QUOTE
252-
}
253-
254-
// check if JSON stream stopped in a string's unicode escape, like `\u????`
255-
func (lexer *Lexer) streamStoppedInAnStringUnicodeEscape() bool {
256-
// `\`, `u` in stack
257-
case1 := []int{
258-
TOKEN_ESCAPE_CHARACTER,
259-
TOKEN_ALPHABET_LOWERCASE_U,
260-
}
261-
// `"` in mirror stack
262-
case2 := []int{
263-
TOKEN_QUOTE,
264-
}
265-
return matchStack(lexer.TokenStack, case1) && matchStack(lexer.MirrorTokenStack, case2)
266-
}
267-
268-
// check if JSON stream stopped in a number, like `[0-9]`
269-
func (lexer *Lexer) streamStoppedInANumber() bool {
270-
return lexer.getTopTokenOnStack() == TOKEN_NUMBER
271-
}
272-
273-
// check if JSON stream stopped in a number first decimal place, like `.?`
274-
func (lexer *Lexer) streamStoppedInANumberDecimalPart() bool {
275-
return lexer.getTopTokenOnStack() == TOKEN_DOT
276-
}
277-
278-
// check if JSON stream stopped in a number other decimal place (except first place), like `.[0-9]?`
279-
func (lexer *Lexer) streamStoppedInANumberDecimalPartMiddle() bool {
280-
// `.`, TOKEN_NUMBER in stack
281-
case1 := []int{
282-
TOKEN_DOT,
283-
TOKEN_NUMBER,
284-
}
285-
return matchStack(lexer.TokenStack, case1)
286-
}
287-
288-
// check if JSON stream stopped in escape character, like `\`
289-
func (lexer *Lexer) streamStoppedWithLeadingEscapeCharacter() bool {
290-
return lexer.getTopTokenOnStack() == TOKEN_ESCAPE_CHARACTER
291-
}
292-
29385
// push escape character `\` into JSON content
29486
func (lexer *Lexer) pushEscapeCharacterIntoJSONContent() {
29587
lexer.JSONContent.WriteByte(TOKEN_ESCAPE_CHARACTER_SYMBOL)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package streamingjsongo
22

33
func isIgnoreToken(c byte) bool {
44
switch c {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package streamingjsongo
22

33
import (
44
"testing"

0 commit comments

Comments
 (0)