forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSON parse and stringify API (rogchap#70)
* JSON parse and stringify API * move to functions rather than ctx methods to align closer to C++ API * value implement json.Marshaler interface * test parse error cases * can;t use errors.As in Go 12.x
- Loading branch information
Showing
7 changed files
with
225 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package v8go | ||
|
||
// #include <stdlib.h> | ||
// #include "v8go.h" | ||
import "C" | ||
import ( | ||
"errors" | ||
"unsafe" | ||
) | ||
|
||
// JSONParse tries to parse the string and returns it as *Value if successful. | ||
// Any JS errors will be returned as `JSError`. | ||
func JSONParse(ctx *Context, str string) (*Value, error) { | ||
if ctx == nil { | ||
return nil, errors.New("v8go: Context is required") | ||
} | ||
cstr := C.CString(str) | ||
defer C.free(unsafe.Pointer(cstr)) | ||
|
||
rtn := C.JSONParse(ctx.ptr, cstr) | ||
return getValue(ctx, rtn), getError(rtn) | ||
} | ||
|
||
// JSONStringify tries to stringify the JSON-serializable object value and returns it as string. | ||
func JSONStringify(ctx *Context, val *Value) (string, error) { | ||
if val == nil { | ||
return "", errors.New("v8go: Value is required") | ||
} | ||
// If a nil context is passed we'll use the context/isolate that created the value. | ||
var ctxPtr C.ContextPtr | ||
if ctx != nil { | ||
ctxPtr = ctx.ptr | ||
} | ||
|
||
str := C.JSONStringify(ctxPtr, val.ptr) | ||
defer C.free(unsafe.Pointer(str)) | ||
return C.GoString(str), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package v8go_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"rogchap.com/v8go" | ||
) | ||
|
||
func TestJSONParse(t *testing.T) { | ||
t.Parallel() | ||
|
||
if _, err := v8go.JSONParse(nil, "{}"); err == nil { | ||
t.Error("expected error but got <nil>") | ||
} | ||
ctx, _ := v8go.NewContext() | ||
_, err := v8go.JSONParse(ctx, "{") | ||
if err == nil { | ||
t.Error("expected error but got <nil>") | ||
return | ||
} | ||
|
||
if _, ok := err.(*v8go.JSError); !ok { | ||
t.Errorf("expected error to be of type JSError, got: %T", err) | ||
} | ||
|
||
} | ||
|
||
func ExampleJSONParse() { | ||
ctx, _ := v8go.NewContext() | ||
val, _ := v8go.JSONParse(ctx, `{"foo": "bar"}`) | ||
fmt.Println(val) | ||
// Output: | ||
// [object Object] | ||
} | ||
|
||
func ExampleJSONStringify() { | ||
ctx, _ := v8go.NewContext() | ||
val, _ := v8go.JSONParse(ctx, `{ | ||
"a": 1, | ||
"b": "foo" | ||
}`) | ||
jsonStr, _ := v8go.JSONStringify(ctx, val) | ||
fmt.Println(jsonStr) | ||
// Output: | ||
// {"a":1,"b":"foo"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters