From 1f467394b2797a4d24a73ca1b42202c6bf2131bc Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 2 Aug 2016 20:55:37 -0700 Subject: [PATCH] Json output --- jmdict.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/jmdict.go b/jmdict.go index e32df50..784a9b0 100644 --- a/jmdict.go +++ b/jmdict.go @@ -23,16 +23,24 @@ package main import ( + "encoding/json" + "fmt" "io" + "strings" "github.com/FooSoft/jmdict" ) +type dictJson struct { + Indices map[string]string `json:"i"` + Items [][]string `json:"d"` +} + type dictEntry struct { Expression string Reading string - Glossary []string Tags []string + Glossary []string } func (d *dictEntry) addTags(tags []string) { @@ -43,6 +51,46 @@ func (d *dictEntry) addTags(tags []string) { } } +func appendIndex(indices map[string]string, key string, value int) { + items, _ := indices[key] + if len(indices) == 0 { + items = fmt.Sprintf(" %d", value) + } else { + items = string(value) + } + indices[key] = items +} + +func buildDictJson(entries []dictEntry) dictJson { + dict := dictJson{Indices: make(map[string]string)} + + for index, entry := range entries { + item := []string{entry.Expression, entry.Reading, strings.Join(entry.Tags, " ")} + item = append(item, entry.Glossary...) + dict.Items = append(dict.Items, item) + + appendIndex(dict.Indices, entry.Expression, index) + if len(entry.Reading) > 0 { + appendIndex(dict.Indices, entry.Reading, index) + } + } + + return dict +} + +func outputJson(entries []dictEntry, writer io.Writer) error { + dict := buildDictJson(entries) + + bytes, err := json.MarshalIndent(dict, "", " ") + // bytes, err := json.Marshal(dict) + if err != nil { + return err + } + + _, err = writer.Write(bytes) + return err +} + func findString(needle string, haystack []string) int { for index, value := range haystack { if needle == value { @@ -168,7 +216,7 @@ func processEnamdict(reader io.Reader, writer io.Writer) error { entries = append(entries, convertEnamdictEntry(enamdictEntry)...) } - return nil + return outputJson(entries, writer) } func processEdict(reader io.Reader, writer io.Writer) error { @@ -182,5 +230,5 @@ func processEdict(reader io.Reader, writer io.Writer) error { entries = append(entries, convertEdictEntry(edictEntry)...) } - return nil + return outputJson(entries, writer) }