Exemplo n.º 1
0
// Create a new Value from a slice of bytes. (this need not be valid JSON)
func NewValueFromBytes(bytes []byte) Value {
	var parsedType int
	err := json.Validate(bytes)

	if err == nil {
		parsedType = identifyType(bytes)

		switch parsedType {
		case NUMBER, STRING, BOOLEAN:
			var p interface{}
			err := json.Unmarshal(bytes, &p)
			if err != nil {
				panic("Unexpected parse error on valid JSON.")
			}

			return NewValue(p)
		case NULL:
			return &_NULL_VALUE
		}
	}

	rv := parsedValue{
		raw: bytes,
	}

	if err != nil {
		rv.parsedType = NOT_JSON
	} else {
		rv.parsedType = parsedType
	}

	return &rv
}
Exemplo n.º 2
0
/*
Create a new Value from a slice of bytes. (this need not be valid JSON)
We first validate the input bytes to check if it is valid json encoded
data using a function, defined in the public repository dustin/gojson.
It returns a nil if the json is valid. Try to identify the type of json
value it is by calling identifyType. If a Number, string, Boolean or
null value we call Unmarshal to parse the json encoded data and store
result.  If an error was returned by the Unmarshal function, the data
was not valid json encoding. In the case it is of object or array,
create and initialize a new variable as a pointer to the structure
parsedValue, and assign raw to the bytes. In the event an err was
returned by the validate function, we set the parsedType variable in
the struct to BINARY. If there was no error we set it to the type we
identified before and return the pointer to the struct as a Value.
*/
func newValueFromBytes(bytes []byte) Value {
	parsedType := BINARY
	err := json.Validate(bytes)

	if err == nil {
		parsedType = identifyType(bytes)

		switch parsedType {
		case NUMBER, STRING, BOOLEAN, NULL:
			var p interface{}
			err := json.Unmarshal(bytes, &p)
			if err != nil {
				panic(fmt.Sprintf("Parse error on JSON data: %v", err))
			}

			return NewValue(p)
		case BINARY:
			return binaryValue(bytes)
		}
	}

	return &parsedValue{
		raw:        bytes,
		parsedType: parsedType,
	}
}
Exemplo n.º 3
0
func (this *parsedValue) parse() Value {
	if this.parsed == nil {
		if this.parsedType == NOT_JSON {
			return nil
		}

		var p interface{}
		err := json.Unmarshal(this.raw, &p)
		if err != nil {
			panic("Unexpected parse error on valid JSON.")
		}
		this.parsed = NewValue(p)
	}

	return this.parsed
}