Jettison – a JSON parser

Author Topic: Jettison – a JSON parser  (Read 9362 times)

Links
GitHub repository: https://github.com/Portify/jettison
Code: https://github.com/Portify/jettison/blob/master/jettison.cs



Jettison

What is Jettison?

Jettison is a "basic" JSON parser that supports every documented JSON data structure (object, array, string, int, float, false, true, null). Here's an example of using it:

Code: [Select]
==> $a = json_loads( "-5.3" );

==> echo( $a );
-5.3

==> echo( js_type( $a ) );
number

==> $a = json_loads( "[57, 33, 90, \"foo\"]" );

==> echo( $a );
9627

==> echo( js_type( $a ) );
array

==>echo( $a.describe() );
array[9627] - 4 items
 0: 57
 1: 33
 2: 90
 3: foo

==>echo( json_dumps( $a ) );
[57,33,90,"foo"]

Parse Output

Jettison represents JSON data as raw strings where possible. Objects and arrays, though, are stored as ScriptObjects with a specific class. Below is a complete listing of how every data structure would be represented.

  • object
    An object is stored as a "JSObject", which is a class that implements basic dictionary (key -> value) functionality.
  • array
    An array is stored as a "JSArray", which is a class that implements basic list (value, value) functionality.
  • string
    A string is stored as a raw string, such as "foo".
  • int/float
    A number is stored as a raw string, such as "-3.741".
  • false/true
    A boolean values is stored as "0" or "1".
  • null
    A null value is stored as "".

Function Reference

json_dump( data, file )
Wrapper to invoke json_dumps with data and save the output to file.

json_dumps( data )
Serializes data into a JSON string. Exact intended value types cannot be determined, it's based on guessing. See below.

json_load( file )
Wrapper that reads file and passes it to json_loads.

json_loads( json )
Parses json and returns the data (see "parse output" section) or -1 if an error occurs while parsing.

Note about json_dumps:
It cannot determine the exact type of value you're intending to be using. Instead, it inspects the value and does a few tests to try to find out what type of value it is. Here's the exact "order of tests":

  • If it's an object, treat it as the object type.
  • If it's a blank string, treat it as null.
  • If it can be represented as a number in JSON, treat it as a number.
  • If all else fails, treat it as a string.

Shared Object Methods

x.onAdd() (internal)
Initializes a few object fields to standard values.

x.describe()
Returns a description of the object as a string with the object ID, object type and value.

x.killTree()
Deletes itself and all objects it contains.

JSObject Method Reference

JSObject.size()
Returns the total number of unique keys stored in the object.

JSObject.clear()
Removes all keys (and their associated value  runs killTree on values that are parsed objects).

JSObject.hasKey( key )
Returns true or false, depending on whether key is stored on the object.

JSObject.removeKey( key )
Removes key from the object (and it's associated value  runs killTree on the value if it is a parsed object).

JSObject.get( key[, default] )
If key is stored on the object, returns the value. Otherwise, returns default (a blank string by default).

JSObject.set( key, value )
Adds key to the object if it was not already stored there and updates the value of it to value.

JSArray Method Reference

JSArray.size()
Returns the total number of values stored in the object.

JSArray.get( index )
Returns the value stored at index on the object (or a blank string if it's out of range).

JSArray.clear()
Removes all values (runs killTree on values if they are JSON objects).

JSArray.append( value )
Stores value on the object (duplicates allowed).

JSArray.remove( value )
Removes value from the object (runs killTree on the value if it is a parsed object).

JSArray.contains( value )
Returns true or false, depending on whether value is stored on the object.

Credits

  • Jettison  Port
  • JSON  Douglas Crockford



This post was generated with md-to-bbc.
« Last Edit: January 14, 2013, 07:55:08 AM by Port »

Nice. One thing, is echoDump recursive?

Yes. No, was thinking of killTree while answering.



Actually, another thing too, how would you tell whether a value is a real int/string, instead of an array/object?

To check if it's an actual JSON object, you can use if ( %value.json ).



Looks well structured and documented.

But I have to ask, was there an inadequacy with mine that caused you to make your own, or was there another reason? If the former, what didn't you like about it? I'd want to improve it where possible if you have feedback.

I didn't know you had made one, care to give me a link?
« Last Edit: November 14, 2012, 12:59:00 PM by Port »

Update

  • Fixed a few minor namespace mistakes (mixing JSArray and JSObject).
  • Functions that remove values from JSON data objects now run killTree on removed values if they are JSON data objects.

where did you get those quotes they are gone!

Came here expecting Jetson mod but heck looks pretty neat

where did you get those quotes they are gone!

This topic was originally in Coding Help. Those posts were made, but then I locked the topic to move it (Ephialtes deleted it shortly after) and reposted it here in Modification Discussion.

GitHub repository (includes a Markdown version of the OP).
« Last Edit: November 14, 2012, 01:45:11 PM by Port »

Looks neat but the examples you gave were bad. Might want to re-do the examples to make things a bit more clear.

Nice work nonetheless.

now make a HTML to GUI script
you will get
INSTANT WINNINGS

now make a HTML to GUI script
you will get
INSTANT WINNINGS
Do you realize how damn slow that would be?

Anyway, nice job. This will be handy for parsing JSON outputted by web APIs.
Can you adapt it for creating JSON out of inputted structure info?

Do you realize how damn slow that would be?

Anyway, nice job. This will be handy for parsing JSON outputted by web APIs.
Can you adapt it for creating JSON out of inputted structure info?
it's port don't worry


Do you realize how damn slow that would be?

Anyway, nice job. This will be handy for parsing JSON outputted by web APIs.
Can you adapt it for creating JSON out of inputted structure info?
Which is why you run it in advance and package the resultant GUI

Looks real great, I was actually thinking of working on a JSON parser. But I'd probably do mine differently anyway (simsets, simsets everywhere)

Added JSON serialization and updated the OP (and README.md).


Can you adapt it for creating JSON out of inputted structure info?

it's port don't worry

Added JSON serialization and updated the OP (and README.md).