Giter VIP home page Giter VIP logo

jsonify's Introduction

jsonify

A Dart library to access the webstorage API like an (almost) full JSON client-side storage.

  • window.localStorage and
  • window.sessionStorage

have the restriction to store only Strings as keys and values. So, if you have to store an highscore of 456431 points you have to convert the integer into a string of "456431" and store it to webstorage.

int highscore = 456431;
window.localStorage['highscore'] = highscore.toString();

If you want to read this highscore later-on you have to convert it into an integer value again.

int highscore = int.parse(window.localstorage['highscore']);

These from-to-into-string-conversions are cumbersome and error-prone. It would be more convenient to do something like that:

int score = 456431;
storage.highscore = 456431;

// Do something with the highscore.
score = storage.highscore;

But this is not possible using webstorage. If you want to store even more complex states it gets even trickier. One option would be to use JSON.

Map<String, Object> dayscore = {
    'name': 'Max Musterman',
    'score': 456431,
    'date': '2018-06-06'
}
List<Map> scores = JSON.decode(window.localstorage['scores']);
scores.add(dayscore);
window.localstorage['scores'] = JSON.encode(scores);

But now we have a even more complex JSON-from-to-into-conversion.

That is where jsonify comes into play. It is merely more than a JSON wrapper for window.sessionstorage and window.localstorage (and every other Map<String, String>).

Usage

A simple usage example of jsonify may look like that:

import 'package:jsonify/jsonify.dart';

main() {
  final jstorage = jsonify(window.localStorage);   // jsonify persistent storage
  final jsession = jsonify(window.sessionStorage); // jsonify session storage
}

Doing that you can now use local/session-storage as an (almost) full featured JSON store.

No we can store more complex data structures. Check the following example.

jstorage['get'] = {
    'this': 'is',
    'a': [
        'more',
        'complex',
        'example',
        {
            'answer': 42,
            'question': null
            'usefulness': 'unknown'
        }
    ]
};

And we can access this object and its parts like that:

jstorage['get']['a'].last['answer'] == 42

If we have keys that are valid Dart identifiers, (and thanks to noSuchMethod()) we can even use the following getter/setter based-notation (which is often used by Ruby libraries):

jstorage.get.a.last.answer == 42

It is possible to use the same notation to write into the webstorage:

jstorage.is = { 'astonishing?': true };

However, if a key is not a valid Dart identifier (or a reserved Dart keyword), we must (and always can) fallback to the more classical Map notation

jstorage.is['astonishing?']

That is basically all ... not less, not more. However, it might be helpful.

Known limitations

Not all methods known for Dart Maps and Lists are currently supported. Only, the most essential ones. Currently the following methods and properties are supported:

Map (currently supported methods and properties)

  • isEmpty
  • isNotEmpty
  • length
  • operator[]
  • operator[]=
  • addAll()
  • clear()
  • remove()

List

  • length
  • reversed
  • iterator
  • first
  • last
  • isEmpty
  • isNotEmpty
  • operator[]
  • operator[]=
  • add()
  • addAll()
  • clear()
  • remove()
  • removeAt()
  • sublist

Please, feel free to post features and bugs.

Features and bugs

Please file feature requests and bugs at the issue tracker.

jsonify's People

Contributors

nkratzke avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.