Giter Club home page Giter Club logo

jsonparse's Introduction

JSONParse.js

New Stuff!

JSONParse is much more pleasant to use now. Indeed, it's pleasant enough that you might enjoy using json objects for conveniently storing values for other purposes (since it gives you a lot of the nice qualities of a JavaScript dynamic object).

As before, you can simply do something like:

var json_value : json = json.fromString( .... );

In particular, there are convenience functions for grabbing values with no pesky underscores. These are:

  json_value.getNumber( index: int OR key: string ): double
  json_value.getString( index: int OR key: string ): String
  json_value.getBoolean( index: int OR key: string): boolean
  json_value.getArray( index: int OR key: string): json
  json_value.getObject( index: int OR key: string): json
  json_value.getRect( index: int OR key: string): Rect // expects x, y, width, height
  json_value.getVector2( index: int OR key: string): Vector2 // expects x, y
  json_value.getVector3( index: int OR key: string): Vector3 // expects x, y, z

All of these will throw Debug.LogErrors if the expected value is not present, making debugging easier.

There's also some other handy stuff such as:

  json_value.has( key: String ): boolean // does this key exist?
  json_value.length(): int // length of array or object property list, or 0
  json_value.indexOf( key: String ): int // index of the key, or -1 if not found

I've used this to rewrite my own GUI library and it's almost pleasant to use it.

What?

It's a library for parsing JSON, using UnityScript (the programming language used in Unity).

Tonio adds: and now it works for mobile and is written in more idiomatic code.

Why?

Because JSON is the fat-free XML, and because UnityScript is not JavaScript (even though everybody says it is.).

Tonio adds: and exactly how else are your mobile apps supposed to chat with servers?

Who?

Philip Peterson.

Tonio Loewald.

Demo

var s = "{ \"foo\": \"bar\", \"baz\" : [ 17, 18, 19, { \"fish\" : \"soup\" } ]}";

var j:json = json.fromString(s);
print( "tostring: " + j.toString() );
print( "stringified: " + j.stringify() );

print( "obj.foo: " + j._get("foo").toString() );
print( "obj.baz[2]: " + j._get("baz")._get(2).toString() );
print( "obj.baz[3].fish: " + j._get("baz")._get(3)._get("fish").toString() );

var json_obj = json._object(); // new empty object
json_obj._set("key", json._string("value")); // note that the string could have been passed "unwrapped"
print( json_obj.stringify() ); // {"key":"value"}

var json_array = json._array();
json_array._push(1)._push("two")._push( json._object()._set("foo","bar") ); // chaining, jQuery-style
print( json_array.stringify() ); // [ 1, "two", {"foo":"bar"} ];

jsonparse's People

Contributors

tonioloewald avatar ironmagma avatar

Watchers

James Cloos avatar Brian McBarron 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.