aboutsummaryrefslogtreecommitdiff
path: root/example/json/main.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2021-03-07 23:44:10 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2021-03-07 23:44:10 +0300
commit521e6da1f6c9c964abca5bac22ce62823c276c1f (patch)
treef6605a64a8511192e7331f2443083cf7f780acb0 /example/json/main.go
parent3e6a54d506fb5c18ae0aec8cfe01578b40edbc9b (diff)
Added JSON example
Diffstat (limited to 'example/json/main.go')
-rw-r--r--example/json/main.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/example/json/main.go b/example/json/main.go
new file mode 100644
index 0000000..842ab82
--- /dev/null
+++ b/example/json/main.go
@@ -0,0 +1,99 @@
+// +build ignore
+
+package main
+
+import (
+ "fmt"
+
+ "github.com/neonxp/unilex"
+)
+
+func main() {
+ testJson := `
+ {
+ "key1": "value1",
+ "key2": {
+ "key3" : "value 3"
+ },
+ "key4": 123.321
+ }`
+ l := unilex.New(testJson)
+ go l.Run(initJson)
+ for ll := range l.Output {
+ fmt.Println(ll)
+ }
+}
+
+const (
+ lObjectStart unilex.LexType = "lObjectStart"
+ lObjectEnd unilex.LexType = "lObjectEnd"
+ lObjectKey unilex.LexType = "lObjectKey"
+ lObjectValueString unilex.LexType = "lObjectValueString"
+ lObjectValueNumber unilex.LexType = "lObjectValueNumber"
+)
+
+func initJson(l *unilex.Lexer) unilex.StateFunc {
+ ignoreWhiteSpace(l)
+ switch {
+ case l.Accept("{"):
+ l.Emit(lObjectStart)
+ return stateInObject(true)
+ case l.Peek() == unilex.EOF:
+ return nil
+ }
+ return l.Errorf("Unknown token: %s", l.Peek())
+}
+
+func stateInObject(initial bool) unilex.StateFunc {
+ return func(l *unilex.Lexer) unilex.StateFunc {
+ // we in object, so we expect field keys and values
+ ignoreWhiteSpace(l)
+ if l.Accept("}") {
+ l.Emit(lObjectEnd)
+ if initial {
+ return initJson
+ }
+ ignoreWhiteSpace(l)
+ l.Accept(",")
+ ignoreWhiteSpace(l)
+ return stateInObject(initial)
+ }
+ if l.Peek() == unilex.EOF {
+ return nil
+ }
+ if !unilex.ScanQuotedString(l, '"') {
+ return l.Errorf("Unknown token: %s", l.Peek())
+ }
+ l.Emit(lObjectKey)
+ ignoreWhiteSpace(l)
+ if !l.Accept(":") {
+ return l.Errorf("Expected ':'")
+ }
+ ignoreWhiteSpace(l)
+ switch {
+ case unilex.ScanQuotedString(l, '"'):
+ l.Emit(lObjectValueString)
+ ignoreWhiteSpace(l)
+ l.Accept(",")
+ l.Ignore()
+ ignoreWhiteSpace(l)
+ return stateInObject(initial)
+ case unilex.ScanNumber(l):
+ l.Emit(lObjectValueNumber)
+ ignoreWhiteSpace(l)
+ l.Accept(",")
+ l.Ignore()
+ ignoreWhiteSpace(l)
+ return stateInObject(initial)
+ case l.Accept("{"):
+ l.Emit(lObjectStart)
+ return stateInObject(false)
+ }
+ return l.Errorf("Unknown token")
+ }
+}
+
+func ignoreWhiteSpace(l *unilex.Lexer) {
+ l.AcceptWhile(" \n\t") //ignore whitespaces
+ l.Ignore()
+}