Skip to content

Commit

Permalink
(chore) Clean up JSON and add Ensure for string validation (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
clsource authored Nov 16, 2021
1 parent 2a3d76a commit 72db7fc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/essentials.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ ModuleRegistry essentialRegistry[] =

MODULE(json)
CLASS(JSONStream)
METHOD("stream_begin(_)", jsonStreamBegin)
METHOD("stream_begin_(_)", jsonStreamBegin)
METHOD("stream_end()", jsonStreamEnd)
METHOD("next", jsonStreamNext)
METHOD("value", jsonStreamValue)
Expand Down
26 changes: 0 additions & 26 deletions src/modules/json.c
Original file line number Diff line number Diff line change
@@ -1,35 +1,9 @@
#include "json.h"
#include "utf8.h"
#include "pdjson.h"

// Extracted from DOME engine
#define VM_ABORT(vm, error) do {\
wrenSetSlotString(vm, 0, error);\
wrenAbortFiber(vm, 0); \
} while(false);

#define ASSERT_SLOT_TYPE(vm, slot, type, fieldName) \
if (wrenGetSlotType(vm, slot) != type) { \
VM_ABORT(vm, #fieldName " was not " #type); \
return; \
}

// Json API

// We have to use C functions for escaping chars
// because a bug in compiler throws error when using \ in strings
// inside Wren files.
// TODO: Check this in the future.
enum JsonOptions {
JSON_OPTS_NIL = 0,
JSON_OPTS_ESCAPE_SLASHES = 1,
JSON_OPTS_ABORT_ON_ERROR = 2
};

json_stream jsonStream[1];

void jsonStreamBegin(WrenVM * vm) {
ASSERT_SLOT_TYPE(vm, 1, WREN_TYPE_STRING, "value");
const char * value = wrenGetSlotString(vm, 1);
json_open_string(jsonStream, value);
json_set_streaming(jsonStream, 0);
Expand Down
30 changes: 20 additions & 10 deletions src/modules/json.wren
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Extracted from https://github.com/domeengine/dome/blob/develop/src/modules/json.wren
import "ensure" for Ensure

// Mainly based on https://github.com/domeengine/dome/blob/develop/src/modules/json.wren
// Some code based on https://github.com/brandly/wren-json/blob/master/json.wren

class JSONOptions {
static nil { 0 }
Expand Down Expand Up @@ -52,7 +55,13 @@ class Token {
}

class JSONStream {
foreign stream_begin(value)
// Ensure the stream is always a string
stream_begin(value) {
Ensure.string(value, "value")
stream_begin_(value)
}
foreign stream_begin_(value)

foreign stream_end()
foreign next
foreign value
Expand Down Expand Up @@ -142,8 +151,9 @@ class JSONStream {
}
}

// protocol for JSON encodable values
// So they can override how to
// Protocol for JSON encodable objects
// Prefer this protocol instead of toString
// Override toJSON in the child
class JSONEncodable {
toJSON {this.toString}
}
Expand Down Expand Up @@ -230,7 +240,6 @@ class JSONEncoder {
Fiber.abort("Circular JSON")
}

// Loosely based on https://github.com/brandly/wren-json/blob/master/json.wren
if (value is Num || value is Bool || value is Null) {
return value.toString
}
Expand Down Expand Up @@ -261,6 +270,7 @@ class JSONEncoder {
return "{" + substrings.join(",") + "}"
}

// Check if the object implements toJSON
if (value is JSONEncodable) {
return value.toJSON
}
Expand All @@ -278,10 +288,6 @@ class JSON {
return JSON.encode(value, JSONOptions.abortOnError)
}

static parse(value) {
return JSON.decode(value)
}

static stringify(value) {
return JSON.encode(value)
}
Expand All @@ -302,4 +308,8 @@ class JSON {
static decode(value) {
return JSON.decode(value, JSONOptions.abortOnError)
}
}

static parse(value) {
return JSON.decode(value)
}
}

0 comments on commit 72db7fc

Please sign in to comment.