Added JSON unmarshaling for nested data types from USF JSON.

This commit is contained in:
Benjamin A. Shelton 2026-09-22 17:06:45 -06:00
commit 45e2832526
Signed by: zancarius
GPG key ID: 8BD42DBD6C337E01

View file

@ -1,6 +1,7 @@
package bible package bible
import ( import (
"bytes"
"encoding/json" "encoding/json"
"os" "os"
) )
@ -30,15 +31,107 @@ type Book struct {
} }
type Chapter struct { type Chapter struct {
Book *Book Book *Book `json:"-"`
NumVerses int `json:"numberOfVerses"` NumVerses int `json:"numberOfVerses"`
ChapterDecl *ChapterDecl `json:"chapter"`
//Verses //Verses
} }
type ChapterDecl struct {
Number int `json:"number"`
Content []Content `json:"content"`
}
type Content struct { type Content struct {
Number int `json:"number"` Text string
Content []*ContentSpec `json:"content"` IsHeading bool
Footnotes []*NoteSpec `json:"footnotes"` IsLineBreak bool
IsNote bool
IsPoem bool
IsVerse bool
NoteId int
Number int
PoemOffset int
Content []Content
}
func (c *Content) UnmarshalJSON(d []byte) error {
d = bytes.TrimSpace(d)
if len(d) == 0 {
return nil
}
if d[0] == '"' {
json.Unmarshal(d, &c.Text)
}
if d[0] == '{' {
var peek map[string]json.RawMessage
if err := json.Unmarshal(d, &peek); err != nil {
return err
}
if t, ok := peek["type"]; ok {
var s string
if err := json.Unmarshal(t, &s); err != nil {
return err
}
switch s {
case "heading":
if ct, ok := peek["content"]; ok {
content := make([]Content, 0)
c.IsHeading = true
if err := json.Unmarshal(ct, &content); err != nil {
return err
} else {
c.Content = content
}
}
case "line_break":
c.IsLineBreak = true
case "verse":
if nb, ok := peek["number"]; ok {
if err := json.Unmarshal(nb, &c.Number); err != nil {
return err
}
}
if ct, ok := peek["content"]; ok {
content := make([]Content, 0)
c.IsVerse = true
if err := json.Unmarshal(ct, &content); err != nil {
return err
} else {
c.Content = content
}
}
}
}
if _, ok := peek["line_break"]; ok {
c.IsLineBreak = true
}
if nd, ok := peek["noteId"]; ok {
c.IsNote = true
json.Unmarshal(nd, &c.NoteId)
}
}
return nil
}
type HeadingSpec struct {
Type string `json:"type"`
Content []string `json:"content"`
}
type PoemSpec struct {
Text string `json:"text"`
Poem int `json:"poem"`
}
type VerseSpec struct {
Type string `json:"type"`
Number int `json:"number"`
Content []Content `json:"content"`
} }
type ContentSpec struct { type ContentSpec struct {