0.3.2 • Published 4 years ago

jsonjs v0.3.2

Weekly downloads
365
License
MIT
Repository
github
Last release
4 years ago

npm version

Introduction

This library is inspired by jsonj, the java library for parsing and manipulating json structures in java that we developed at Inbot. While javascript of course supports json natively, it misses a few of the features present in jsonj. This small library attempts to rectify this.

Install

$ npm install jsonjs

Usage

var obj = jsonjs.decorate({
  foo: 'baa',
  arr: []
});

expect(obj.get('foo')).toEqual('baa');
expect(obj.get('arr')).toEqual(jasmine.any(Array));

obj.put('cool', 'deep', 'nested', 'object', true);
expect(obj.get('cool', 'deep', 'nested', 'object')).toEqual(true);

var realObject = obj.object();
expect(realObject.cool.deep.nested.object).toEqual(true);

var newObject = obj.getOrCreateObject('im', 'new');
expect(obj.object()).toEqual(jasmine.objectContaining({ im: { new: {} } }));
expect(newObject).toEqual({});

newObject.newProperty = 'new value';
expect(newObject).toEqual(jasmine.objectContaining({ newProperty: 'new value' }));
expect(obj.object().im.new).toEqual(jasmine.objectContaining({ newProperty: 'new value' }));
expect(obj.get('im', 'new', 'newProperty')).toEqual("new value");
expect(obj.data.im.new.newProperty).toEqual("new value");

var arr = obj.getOrCreateArray('arr');
expect(arr).toEqual(jasmine.any(Array));
expect(arr.length).toBe(0);
expect(arr).toEqual([]);

arr.push('x');
expect(arr.length).toBe(1);
expect(arr).toEqual(['x']);
expect(obj.get('arr')).toEqual(['x']);

var decoratedCopy = obj.copy();
expect(decoratedCopy).not.toBe(obj);
expect(decoratedCopy.object()).toEqual(obj.object());

var originalObjectClone = obj.clone();
expect(originalObjectClone).not.toBe(obj.object());
expect(originalObjectClone).toEqual(obj.object());

var decoratedArray = obj.getOrCreateDecoratedArray('items');
expect(decoratedArray.get(0).foo).toEqual('foobaa');
var item = decoratedArray.getObject(0);
item.put('foo', 'baa');
expect(item.get('foo')).toEqual('baa');
expect(decoratedArray.get(0).foo).toEqual('baa');
expect(obj.get('items', 0, 'foo')).toEqual('baa');

Check tests for more examples.

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago