Skip to content

Commit 871b506

Browse files
committed
Added ParsePrimitiveValue convenience function for users
1 parent 4b32de2 commit 871b506

File tree

2 files changed

+168
-75
lines changed

2 files changed

+168
-75
lines changed

parser.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,22 @@ func unsafeBytesToString(data []byte) string {
442442
sh := reflect.StringHeader{Data: h.Data, Len: h.Len}
443443
return *(*string)(unsafe.Pointer(&sh))
444444
}
445+
446+
// ParseBoolean parses a Boolean ValueType into a Go bool (not particularly useful, but here for completeness)
447+
func ParseBoolean(vbytes []byte) bool {
448+
return (vbytes[0] == 't') // assumes value is already validated by Get(), etc. as signaled by jtype == Boolean
449+
}
450+
451+
// ParseString parses a String ValueType into a Go string (the main parsing work is unescaping the JSON string)
452+
func ParseString(vbytes []byte) (string, error) {
453+
return "", nil
454+
}
455+
456+
// ParseNumber parses a Number ValueType into a Go float64
457+
func ParseNumber(vbytes []byte) (float64, error) {
458+
if v, err := strconv.ParseFloat(unsafeBytesToString(vbytes), 64); err != nil { // TODO: use better BytesParseFloat in PR #25
459+
return 0, MalformedValueError
460+
} else {
461+
return v, nil
462+
}
463+
}

0 commit comments

Comments
 (0)