Skip to content

Instantly share code, notes, and snippets.

View geraintluff's full-sized avatar

Geraint geraintluff

View GitHub Profile
@geraintluff
geraintluff / data.json
Last active March 25, 2023 07:14
Additional Relative JSON Pointer examples
{
"test": ["foo", "bar"],
"child": {
"grandchild": 12345
},
"sibling": "sibling value",
"awkwardly/named~variable": true
}
@geraintluff
geraintluff / generate.py
Last active August 29, 2017 22:39
Generate JSON Schemas from schema.org types
import urllib.request as request
import re
import os
import copy
import json
outputprefix = "schemas/"
outputsuffix = '.json'
urlprefix = "http://json-schema.org/schemas/"
urlsuffix = '.json'
@geraintluff
geraintluff / GolfCourse.json
Created July 8, 2013 19:37
Example schemas generated from schema.org
{
"id": "http://json-schema.org/schemas/GolfCourse.json",
"title": "Golf Course",
"description": "A golf course.",
"allOf": [{"$ref": "SportsActivityLocation.json"}],
"type": "object",
"format": "http://schema.org/GolfCourse",
"properties": {},
"definitions": {}
}
// Generic renderer for arrays
// Requires "render.table" and "render.generator" plugins
Jsonary.render.register(Jsonary.plugins.Generator({
// Part of the generator plugin - this function returns a renderer based on the data/schema requirements
rendererForData: function (data) {
var FancyTableRenderer = Jsonary.plugins.FancyTableRenderer;
var renderer = new FancyTableRenderer({
rowsPerPage: 15,
cellActionHtml: {
'remove': function (data, context, actionName) {
function prettyJson(data) {
var json = JSON.stringify(data, null, "\t");
function compactJson(json) {
try {
var compact = JSON.stringify(JSON.parse(json));
var parts = compact.split('"');
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
part = part.replace(/:/g, ': ');
part = part.replace(/,/g, ', ');
@geraintluff
geraintluff / addscript.txt
Created September 24, 2013 18:57
A bookmarklet that registers window.addScript(), for simply including JS scripts.
javascript:(function(){window.addScript=function(url){document.body.appendChild(document.createElement('script')).src=url;};})();
@geraintluff
geraintluff / copyStyle.js
Created September 24, 2013 18:59
Duplicates <link> and <style> elements in a new document (useful for spawning sub-windows)
function copyStyle(oldDoc, newDoc) {
var links = oldDoc.getElementsByTagName('link');
for (var i = 0; i < links.length; i++) {
var oldElement = links[i];
var newElement = newDoc.createElement('link');
newElement.rel = oldElement.rel;
newElement.href = oldElement.href;
newDoc.head.appendChild(newElement);
}
var styles = oldDoc.getElementsByTagName('style');
{
"title": "Ellipsis",
"description": "Placeholder for items that are not present in a preview list, but are expected to be present in the actual list.",
"type": "object",
"properties": {
"...": {
"type": ["integer", "null"],
"minimum": 1
},
"total": {
@geraintluff
geraintluff / Pretty JSON.md
Last active October 19, 2021 21:35
My personal opinion on what nice JSON looks like:

My personal opinion on what nice JSON looks like:

  • Scalars as per normal
  • Empty objects/arrays as {} or []
  • Single-entry objects on a single line if value is single-line
  • Single-entry arrays are just wrapped in [...] (even if value is not single-line)
  • Arrays <= 5 items on a single line if all values are single-line
  • JSON.stringify() style indenting otherwise

Indenting customisable, but defaults to tabs - I code in proportional font, so four-space looks a bit narrow, and two-space just looks ridiculous.

// Quick'n'dirty EventEmitter class
function EventEmitter() {
}
EventEmitter.prototype = {
on: function (event, listener) {
this._events = this._events || {};
this._events[event] = this._events[event] || [];
this._events[event].push(listener);
this.emit('newListener', event, listener);
return this;