Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skulpt json.sk package #989

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Added

- Support for json package in skulpt python runner (#989)
- Support to enable embedding iframes in HTML projects from in-house domains (#985)
- Unit tests for `pyodide` runner (#976)

Expand Down
22 changes: 22 additions & 0 deletions public/shims/json.sk/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2014 Trinket

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

72 changes: 72 additions & 0 deletions public/shims/json.sk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
json.sk
=======

JSON module for Skulpt

## Current Support

This module is an attempt to reproduce some of the functionality provided by the Python JSON module for use in [Skulpt](http://www.skulpt.org/).

### dump vs dumps

So far, only dumps and its loads counterpart are supported. dump and load deal with streams whereas dumps and loads operate on strings.


### dumps keyword arguments

While support of all arguments is far from complete this implementation passes many of the tests found in the Python distribution. Supported keyword arguments are:

* indent
* ensure_ascii
* separators
* sort_keys

### loads

No arguments for loads are currently supported. This implementation relies on the browser implementation of JSON.parse and Skulpt's ffi.remapToPy.

### Python docs

Complete spec of the Python 2.x JSON implementation can be found at https://docs.python.org/2/library/json.html.

## TODO

While there is much to do to fully support the Python implementation, transformation for basic objects works quite well.

### dumps

Remaining keyword arguments to support include:

* shipkeys
* check_circular
* allow_nan
* cls
* encoding
* default

## Getting Started

Create a basic html page:

```html
<!-- @TODO: replace with example markup... -->
```

Add the json.sk specific Skulpt configuration options
```js
// tell Skulpt where to find json.sk and its dependencies
Sk.externalLibraries = {
json : {
path : '/path/to/json.sk/__init__.js',
dependencies : [
'/path/to/json.sk/stringify.js'
]
}
};
```

Point your browser to your html page and have fun!

## Dependencies

Currently relies on a [browserify](https://github.com/substack/node-browserify)'d version of [json-stable-stringify](https://github.com/substack/json-stable-stringify).
98 changes: 98 additions & 0 deletions public/shims/json.sk/__init__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
var $builtinmodule = function(name) {
"use strict";
var mod = {};

// skipkeys=False,
// ensure_ascii=True,
// check_circular=True,
// allow_nan=True,
// cls=None,
// indent=None,
// separators=None,
// encoding="utf-8",
// default=None,
// sort_keys=False,
// **kw

var dumps_f = function(kwa) {
Sk.builtin.pyCheckArgs("dumps", arguments, 1, Infinity, true, false);

var args = Array.prototype.slice.call(arguments, 1),
kwargs = new Sk.builtins.dict(kwa),
sort_keys = false,
stringify_opts, default_, jsobj, str;

// default stringify options
stringify_opts = {
ascii : true,
separators : {
item_separator : ', ',
key_separator : ': '
}
};

kwargs = Sk.ffi.remapToJs(kwargs);
jsobj = Sk.ffi.remapToJs(args[0]);

// TODO: likely need to go through character by character to enable this
if (typeof(kwargs.ensure_ascii) === "boolean" && kwargs.ensure_ascii === false) {
stringify_opts.ascii = false;
}

// TODO: javascript sort isn't entirely compatible with python's
if (typeof(kwargs.sort_keys) === "boolean" && kwargs.sort_keys) {
sort_keys = true;
}

if (!sort_keys) {
// don't do any sorting unless sort_keys is true
// if sort_keys use stringify's default sort, which is alphabetical
stringify_opts.cmp = function(a, b) {
return 0;
};
}

// item_separator, key_separator) tuple. The default is (', ', ': ').
if (typeof(kwargs.separators) === "object" && kwargs.separators.length == 2) {
stringify_opts.separators.item_separator = kwargs.separators[0];
stringify_opts.separators.key_separator = kwargs.separators[1];
}

// TODO: if indent is 0 it should add newlines
if (kwargs.indent) {
stringify_opts.space = kwargs.indent;
}

// Sk.ffi.remapToJs doesn't map functions
if (kwargs.default) {
}

// may need to create a clone of this to have more control/options
str = stringify(jsobj, stringify_opts);

return new Sk.builtin.str(str);
};

dumps_f.co_kwargs = true;
mod.dumps = new Sk.builtin.func(dumps_f);

// encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]
var loads_f = function(kwa) {
Sk.builtin.pyCheckArgs("loads", arguments, 1, Infinity, true, false);

var args = Array.prototype.slice.call(arguments, 1),
kwargs = new Sk.builtins.dict(kwa),
str, obj;

kwargs = Sk.ffi.remapToJs(kwargs);
str = args[0].v;
obj = JSON.parse(str);

return Sk.ffi.remapToPy(obj);
};

loads_f.co_kwargs = true;
mod.loads = new Sk.builtin.func(loads_f);

return mod;
};
27 changes: 27 additions & 0 deletions public/shims/json.sk/bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "json.sk",
"version": "0.0.0",
"homepage": "https://github.com/trinketapp/json.sk",
"authors": [
"Brian Marks <[email protected]>"
],
"description": "JSON module for Skulpt.",
"main": "__init__.js",
"dependencies": {
"skulpt": "master"
},
"keywords": [
"skulpt",
"python",
"json",
"javascript"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
}
Loading
Loading