cfjson

1.2.1 • Public • Published

compact and flat JSON

install:

npm install cfjson

interface:

cfjson.pack (object:Object) -> Object

cfjson.unpack (schema:Object, packed:Object) -> Object

cfjson.pack resulting object is subtype of JSON object, with each key is murmur2 hash of flatten representation of original JSON object

usage:

packing

var cfjson = require('cfjson');
var p = cfjson.pack ({
	foo: 1,
	'bar': 2,
	tar: [3,4],
	om: {
			o: ['m'],
			m: {a:1}
		}
	});

the value of the p will be:

{ '670491991': 2,
  '1591485485': 'm',
  '2253003985': 1,
  '2414502773': 1,
  '2851480594': 4,
  '3474143747': 3 }

unpacking

for getting value from packed object, you need schema of original object:

values in schema object doesen't matter

cfjson.unpack ({foo:null, 'om.o[0]':1},  p);
cfjson.unpack ({foo:null, om:{'o[0]':1}, p};
cfjson.unpack ({foo:null, om:{o:[0]}},   p);

the result of any line of this code are equal and will be:

{ foo: 1, om: { o: [ 'm' ] } }

utility:

actually, this representation of packed object is not so compact as original JSON data structure, but this is good trade-off with size of flatten JSON representation

in this case, flatten JSON will be:

{ foo: 1,
  bar: 2,
  'tar[0]': 3,
  'tar[1]': 4,
  'om.o[0]': 'm',
  'om.m.a': 1 }

the algorithm then calculate murmur2 hash of each key and store it in resulting object:

// for example,
murmur2('tar[0]') === 3474143747

the string representation of numbers as keys in JSON still not so compact, but there will be much more gain with more larger objects with nested arrays inside

for example, from json.org:

var example = {
    "glossary": {
        "title": "example glossary",
		"GlossDiv": {
            "title": "S",
			"GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Standard Generalized Markup Language",
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
						"GlossSeeAlso": ["GML", "XML"]
                    },
					"GlossSee": "markup"
                }
            }
        }
    }
}

the flatten representation of example will be:

var example_flatten = { 'glossary.title': 'example glossary',
  'glossary.GlossDiv.title': 'S',
  'glossary.GlossDiv.GlossList.GlossEntry.ID': 'SGML',
  'glossary.GlossDiv.GlossList.GlossEntry.SortAs': 'SGML',
  'glossary.GlossDiv.GlossList.GlossEntry.GlossTerm': 'Standard Generalized Markup Language',
  'glossary.GlossDiv.GlossList.GlossEntry.Acronym': 'SGML',
  'glossary.GlossDiv.GlossList.GlossEntry.Abbrev': 'ISO 8879:1986',
  'glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para': 'A meta-markup language, used to create markup languages such as DocBook.',
  'glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso[0]': 'GML',
  'glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso[1]': 'XML',
  'glossary.GlossDiv.GlossList.GlossEntry.GlossSee': 'markup' };

the packed representation of example will be:

var example_packed = { '199606198': 'GML',
  '433686941': 'XML',
  '465142241': 'SGML',
  '781880977': 'A meta-markup language, used to create markup languages such as DocBook.',
  '1585212263': 'markup',
  '2402232994': 'Standard Generalized Markup Language',
  '2505025751': 'example glossary',
  '2961532197': 'SGML',
  '3124296380': 'SGML',
  '3469242823': 'ISO 8879:1986',
  '4121788333': 'S' };

save each three object in files by names example.json (orginal json.org example JSON), example_flatten.json, and example_packed.json,

example.json — 582 bytes

example_flatten.json — 759 bytes

example_packed.json — 378 bytes

Readme

Keywords

none

Package Sidebar

Install

npm i cfjson

Weekly Downloads

9

Version

1.2.1

License

none

Last publish

Collaborators

  • ambilight