func toString(jv C.jv) string { // Dump it! dumped := C.jv_dump_string(jv, 0) defer C.jv_free(dumped) // Convert dump to string! strval := C.jv_string_value(dumped) return C.GoString(strval) }
func dumpString(buf *bytes.Buffer, x C.jv) { ptr := C.jv_string_value(x) ct := C.jv_string_length_bytes(C.jv_copy(x)) p, err := json.Marshal(C.GoStringN(ptr, ct)) if err != nil { panic(err) } buf.Write(p) }
// GetInvalidMessageAsString gets the error message for this Jv. If there is none it // will return ("", false). Otherwise it will return the message as a string and true, // converting non-string values if necessary. If you want the message in it's // native Jv type use `GetInvalidMessage()` // // Consumes the invocant. func (jv *Jv) GetInvalidMessageAsString() (string, bool) { msg := C.jv_invalid_get_msg(jv.jv) defer C.jv_free(msg) if C.jv_get_kind(msg) == C.JV_KIND_NULL { return "", false } else if C.jv_get_kind(msg) != C.JV_KIND_STRING { msg = C.jv_dump_string(msg, 0) } return C.GoString(C.jv_string_value(msg)), true }
func jvToGo(value C.jv) interface{} { switch C.jv_get_kind(value) { case C.JV_KIND_INVALID: return errors.New("invalid") case C.JV_KIND_NULL: return nil case C.JV_KIND_FALSE: return false case C.JV_KIND_TRUE: return true case C.JV_KIND_NUMBER: number := C.jv_number_value(value) if C.jv_is_integer(value) == 0 { return float64(number) } else { return int(number) } case C.JV_KIND_STRING: return C.GoString(C.jv_string_value(value)) case C.JV_KIND_ARRAY: length := C.jv_array_length(C.jv_copy(value)) arr := make([]interface{}, length) for i := range arr { arr[i] = jvToGo(C.jv_array_get(C.jv_copy(value), C.int(i))) } return arr case C.JV_KIND_OBJECT: result := make(map[string]interface{}) var k, v C.jv for jv_i := C.jv_object_iter(value); C.jv_object_iter_valid(value, jv_i) != 0; jv_i = C.jv_object_iter_next(value, jv_i) { k = C.jv_object_iter_key(value, jv_i) v = C.jv_object_iter_value(value, jv_i) result[C.GoString(C.jv_string_value(k))] = jvToGo(v) } return result default: return errors.New("unknown type") } }
func dumpJson(jv C.jv) string { strJv := C.jv_dump_string(jv, 0) result := C.jv_string_value(strJv) freeJv(strJv) return C.GoString(result) }
func (jv *Jv) _string() string { // Raw string value. If called on cs := C.jv_string_value(jv.jv) // Don't free cs - freed when the jv is return C.GoString(cs) }