Skip to content

Parsing negative numbers panics #1

@ghost

Description

package main

import (
	j "github.com/libujacob/jsone"
)

func main() {
	jsdoc := `{"number" : -1}`
	j.ParseJsonObject([]byte(jsdoc))
}

Results in

2021/04/23 12:33:19 Invalid numeric value [-1]!
panic: Invalid numeric value [-1]!

Internal json lib json.Valid or json.Unmarshall works.
Maybe use something like this:

func isNumericValue(s string) bool {
	if _, err := strconv.Atoi(s); err == nil {
		return true
	}
	containsDot := 0
	var negativeNumber bool
	for i, ch := range s {

		if ch == '.' {
			containsDot++
		} else if i == 0 && ch == '-' {
			if len(s) > 1 {
				negativeNumber = true
			}
		} else if ch < '0' || ch > '9' {
			return false
		}
	}
	if negativeNumber {
		return negativeNumber && (containsDot == 0 || containsDot == 1)
	}
	return containsDot == 0 || containsDot == 1
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions